Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

feat(arch001): add Export/Import region to BaseCollectionController

KrzysztofPajak589dd68c28bc
diff --git a/src/Web/Grand.Web.AdminShared/Controllers/BaseCollectionController.cs b/src/Web/Grand.Web.AdminShared/Controllers/BaseCollectionController.cs
index 108716970..db63250d0 100644
--- a/src/Web/Grand.Web.AdminShared/Controllers/BaseCollectionController.cs
+++ b/src/Web/Grand.Web.AdminShared/Controllers/BaseCollectionController.cs
@@ -1,8 +1,10 @@
+using Grand.Business.Core.Dto;
using Grand.Business.Core.Extensions;
using Grand.Business.Core.Interfaces.Catalog.Collections;
using Grand.Business.Core.Interfaces.Catalog.Products;
using Grand.Business.Core.Interfaces.Common.Localization;
using Grand.Business.Core.Interfaces.Common.Stores;
+using Grand.Business.Core.Interfaces.ExportImport;
using Grand.Domain.Catalog;
using Grand.Domain.Permissions;
using Grand.Web.AdminShared.Extensions.Mapping;
@@ -13,6 +15,7 @@ using Grand.Web.Common.Controllers;
using Grand.Web.Common.DataSource;
using Grand.Web.Common.Filters;
using Grand.Web.Common.Security.Authorization;
+using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
@@ -240,4 +243,50 @@ public abstract class BaseCollectionController(
}
#endregion
+
+ #region Export / Import
+
+ [PermissionAuthorizeAction(PermissionActionName.Export)]
+ public async Task<IActionResult> ExportXlsx([FromServices] IExportManager<Collection> exportManager)
+ {
+ try
+ {
+ var bytes = await exportManager.Export(await collectionService.GetAllCollections(collectionName: "", storeId: scope.DefaultStoreId ?? "", showHidden: true));
+ return File(bytes, "text/xls", "collections.xlsx");
+ }
+ catch (Exception exc)
+ {
+ Error(exc);
+ return RedirectToAction("List");
+ }
+ }
+
+ [PermissionAuthorizeAction(PermissionActionName.Import)]
+ [HttpPost]
+ public async Task<IActionResult> ImportFromXlsx(IFormFile importexcelfile,
+ [FromServices] IImportManager<CollectionDto> importManager)
+ {
+ try
+ {
+ if (importexcelfile is { Length: > 0 })
+ {
+ await importManager.Import(importexcelfile.OpenReadStream());
+ }
+ else
+ {
+ Error(translationService.GetResource("Admin.Common.UploadFile"));
+ return RedirectToAction("List");
+ }
+
+ Success(translationService.GetResource("Admin.Catalog.Collection.Imported"));
+ return RedirectToAction("List");
+ }
+ catch (Exception exc)
+ {
+ Error(exc);
+ return RedirectToAction("List");
+ }
+ }
+
+ #endregion
}