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 Create/Edit/Delete region to BaseCategoryController

KrzysztofPajake758c8db633b
diff --git a/src/Tests/Grand.Web.Admin.Tests/Controllers/BaseCategoryControllerTests.cs b/src/Tests/Grand.Web.Admin.Tests/Controllers/BaseCategoryControllerTests.cs
index 9b4e90dba..be441a748 100644
--- a/src/Tests/Grand.Web.Admin.Tests/Controllers/BaseCategoryControllerTests.cs
+++ b/src/Tests/Grand.Web.Admin.Tests/Controllers/BaseCategoryControllerTests.cs
@@ -110,4 +110,119 @@ public class BaseCategoryControllerTests
Assert.AreEqual("store-1", model.SearchStoreId);
}
+
+ // --- Edit (GET) --------------------------------------------------------------------------------
+
+ [TestMethod]
+ public async Task EditGet_CategoryNotFound_RedirectsToList()
+ {
+ _categoryServiceMock.Setup(c => c.GetCategoryById("missing")).ReturnsAsync((Category)null);
+
+ var result = await _controller.Edit("missing");
+
+ var redirect = result as RedirectToActionResult;
+ Assert.IsNotNull(redirect);
+ Assert.AreEqual("List", redirect.ActionName);
+ _scopeMock.Verify(s => s.CanView(It.IsAny<Category>()), Times.Never);
+ }
+
+ [TestMethod]
+ public async Task EditGet_ScopeDeniesView_RedirectsToList()
+ {
+ var category = new Category { Id = "c1" };
+ _categoryServiceMock.Setup(c => c.GetCategoryById("c1")).ReturnsAsync(category);
+ _scopeMock.Setup(s => s.CanView(category)).ReturnsAsync(false);
+
+ var result = await _controller.Edit("c1");
+
+ var redirect = result as RedirectToActionResult;
+ Assert.IsNotNull(redirect);
+ Assert.AreEqual("List", redirect.ActionName);
+ }
+
+ [TestMethod]
+ public async Task EditGet_ScopeAllowsView_ReturnsViewWithModel()
+ {
+ var category = new Category { Id = "c1", Name = "Widgets" };
+ _categoryServiceMock.Setup(c => c.GetCategoryById("c1")).ReturnsAsync(category);
+ _scopeMock.Setup(s => s.CanView(category)).ReturnsAsync(true);
+ var languageServiceMock = new Mock<ILanguageService>();
+ _categoryViewModelServiceMock
+ .Setup(v => v.PrepareCategoryModel(It.IsAny<CategoryModel>(), category, null))
+ .ReturnsAsync((CategoryModel m, Category c, string s) => m);
+
+ var result = await _controller.Edit("c1");
+
+ var view = result as ViewResult;
+ Assert.IsNotNull(view);
+ Assert.AreEqual("Widgets", ((CategoryModel)view.Model).Name);
+ }
+
+ // --- Edit (POST) -------------------------------------------------------------------------------
+
+ [TestMethod]
+ public async Task EditPost_ScopeDeniesAccess_RedirectsToEdit()
+ {
+ var category = new Category { Id = "c1" };
+ _categoryServiceMock.Setup(c => c.GetCategoryById("c1")).ReturnsAsync(category);
+ _scopeMock.Setup(s => s.HasAccess(category)).ReturnsAsync(false);
+
+ var result = await _controller.Edit(new CategoryModel { Id = "c1" }, false);
+
+ var redirect = result as RedirectToActionResult;
+ Assert.IsNotNull(redirect);
+ Assert.AreEqual("Edit", redirect.ActionName);
+ _categoryViewModelServiceMock.Verify(v => v.UpdateCategoryModel(It.IsAny<Category>(), It.IsAny<CategoryModel>()), Times.Never);
+ }
+
+ // --- Delete --------------------------------------------------------------------------------------
+
+ [TestMethod]
+ public async Task Delete_ScopeDeniesAccess_RedirectsToEditWithoutDeleting()
+ {
+ var category = new Category { Id = "c1" };
+ _categoryServiceMock.Setup(c => c.GetCategoryById("c1")).ReturnsAsync(category);
+ _scopeMock.Setup(s => s.HasAccess(category)).ReturnsAsync(false);
+
+ var result = await _controller.Delete("c1");
+
+ var redirect = result as RedirectToActionResult;
+ Assert.IsNotNull(redirect);
+ Assert.AreEqual("Edit", redirect.ActionName);
+ Assert.AreEqual("c1", redirect.RouteValues["id"]);
+ _categoryViewModelServiceMock.Verify(v => v.DeleteCategory(It.IsAny<Category>()), Times.Never);
+ }
+
+ // --- Create (POST) ------------------------------------------------------------------------------
+
+ [TestMethod]
+ public async Task CreatePost_StoreScoped_ForcesModelStores()
+ {
+ _scopeMock.Setup(s => s.DefaultStoreId).Returns("store-1");
+ var inserted = new Category { Id = "new-1" };
+ _categoryViewModelServiceMock
+ .Setup(v => v.InsertCategoryModel(It.IsAny<CategoryModel>()))
+ .ReturnsAsync(inserted)
+ .Callback<CategoryModel>(m => Assert.AreSequenceEqual(new[] { "store-1" }, m.Stores));
+
+ await _controller.Create(new CategoryModel { Name = "N" }, false);
+
+ _categoryViewModelServiceMock.Verify(v => v.InsertCategoryModel(It.IsAny<CategoryModel>()), Times.Once);
+ }
+
+ [TestMethod]
+ public async Task CreatePost_GlobalScoped_LeavesModelStoresUntouched()
+ {
+ _scopeMock.Setup(s => s.DefaultStoreId).Returns((string)null);
+ var inserted = new Category { Id = "new-1" };
+ var submitted = new CategoryModel { Name = "N", Stores = ["explicit-store"] };
+ _categoryViewModelServiceMock
+ .Setup(v => v.InsertCategoryModel(It.IsAny<CategoryModel>()))
+ .ReturnsAsync(inserted)
+ .Callback<CategoryModel>(m => Assert.AreSequenceEqual(new[] { "explicit-store" }, m.Stores));
+
+ await _controller.Create(submitted, false);
+
+ _categoryViewModelServiceMock.Verify(v => v.InsertCategoryModel(It.IsAny<CategoryModel>()), Times.Once);
+ }
}
diff --git a/src/Web/Grand.Web.AdminShared/Controllers/BaseCategoryController.cs b/src/Web/Grand.Web.AdminShared/Controllers/BaseCategoryController.cs
index e21f934aa..b0c07cd03 100644
--- a/src/Web/Grand.Web.AdminShared/Controllers/BaseCategoryController.cs
+++ b/src/Web/Grand.Web.AdminShared/Controllers/BaseCategoryController.cs
@@ -1,12 +1,15 @@
+using Grand.Business.Core.Extensions;
using Grand.Business.Core.Interfaces.Catalog.Categories;
using Grand.Business.Core.Interfaces.Catalog.Products;
using Grand.Business.Core.Interfaces.Common.Localization;
using Grand.Domain.Catalog;
using Grand.Domain.Permissions;
+using Grand.Web.AdminShared.Extensions.Mapping;
using Grand.Web.AdminShared.Interfaces;
using Grand.Web.AdminShared.Models.Catalog;
using Grand.Web.Common.Controllers;
using Grand.Web.Common.DataSource;
+using Grand.Web.Common.Filters;
using Grand.Web.Common.Security.Authorization;
using Microsoft.AspNetCore.Mvc;
@@ -63,4 +66,105 @@ public abstract class BaseCategoryController(
}
#endregion
+
+ #region Create / Edit / Delete
+
+ [PermissionAuthorizeAction(PermissionActionName.Create)]
+ public async Task<IActionResult> Create()
+ {
+ var model = await categoryViewModelService.PrepareCategoryModel(scope.DefaultStoreId);
+ await AddLocales(languageService, model.Locales);
+ return View(model);
+ }
+
+ [PermissionAuthorizeAction(PermissionActionName.Edit)]
+ [HttpPost]
+ [ArgumentNameFilter(KeyName = "save-continue", Argument = "continueEditing")]
+ public async Task<IActionResult> Create(CategoryModel model, bool continueEditing)
+ {
+ if (ModelState.IsValid)
+ {
+ if (scope.DefaultStoreId is not null) model.Stores = [scope.DefaultStoreId];
+ var category = await categoryViewModelService.InsertCategoryModel(model);
+ Success(translationService.GetResource("Admin.Catalog.Categories.Added"));
+ return continueEditing ? RedirectToAction("Edit", new { id = category.Id }) : RedirectToAction("List");
+ }
+
+ model = await categoryViewModelService.PrepareCategoryModel(model, null, scope.DefaultStoreId);
+ return View(model);
+ }
+
+ [PermissionAuthorizeAction(PermissionActionName.Preview)]
+ public async Task<IActionResult> Edit(string id)
+ {
+ var category = await categoryService.GetCategoryById(id);
+ if (category == null) return RedirectToAction("List");
+
+ EditWarningCheck(category);
+ // CanView, not HasAccess: viewing a shared/global category is allowed on Store (with a
+ // warning from EditWarningCheck above); only mutating one is restricted to the exclusive
+ // single-store owner. See IAdminDataScope<TEntity>.CanView's doc comment.
+ if (!await scope.CanView(category)) return RedirectToAction("List");
+
+ var model = category.ToModel();
+ await AddLocales(languageService, model.Locales, (locale, languageId) =>
+ {
+ locale.Name = category.GetTranslation(x => x.Name, languageId, false);
+ locale.Description = category.GetTranslation(x => x.Description, languageId, false);
+ locale.BottomDescription = category.GetTranslation(x => x.BottomDescription, languageId, false);
+ locale.MetaKeywords = category.GetTranslation(x => x.MetaKeywords, languageId, false);
+ locale.MetaDescription = category.GetTranslation(x => x.MetaDescription, languageId, false);
+ locale.MetaTitle = category.GetTranslation(x => x.MetaTitle, languageId, false);
+ locale.SeName = category.GetSeName(languageId, false);
+ locale.Flag = category.GetTranslation(x => x.Flag, languageId, false);
+ });
+ model = await categoryViewModelService.PrepareCategoryModel(model, category, scope.DefaultStoreId);
+
+ return View(model);
+ }
+
+ [PermissionAuthorizeAction(PermissionActionName.Edit)]
+ [HttpPost]
+ [ArgumentNameFilter(KeyName = "save-continue", Argument = "continueEditing")]
+ public async Task<IActionResult> Edit(CategoryModel model, bool continueEditing)
+ {
+ var category = await categoryService.GetCategoryById(model.Id);
+ if (category == null) return RedirectToAction("List");
+ if (!await scope.HasAccess(category)) return RedirectToAction("Edit", new { id = category.Id });
+
+ if (ModelState.IsValid)
+ {
+ if (scope.DefaultStoreId is not null) model.Stores = [scope.DefaultStoreId];
+ category = await categoryViewModelService.UpdateCategoryModel(category, model);
+ Success(translationService.GetResource("Admin.Catalog.Categories.Updated"));
+ if (continueEditing)
+ {
+ await SaveSelectedTabIndex();
+ return RedirectToAction("Edit", new { id = category.Id });
+ }
+ return RedirectToAction("List");
+ }
+
+ model = await categoryViewModelService.PrepareCategoryModel(model, category, scope.DefaultStoreId);
+ return View(model);
+ }
+
+ [PermissionAuthorizeAction(PermissionActionName.Delete)]
+ [HttpPost]
+ public async Task<IActionResult> Delete(string id)
+ {
+ var category = await categoryService.GetCategoryById(id);
+ if (category == null) return RedirectToAction("List");
+ if (!await scope.HasAccess(category)) return RedirectToAction("Edit", new { id = category.Id });
+
+ if (ModelState.IsValid)
+ {
+ await categoryViewModelService.DeleteCategory(category);
+ Success(translationService.GetResource("Admin.Catalog.Categories.Deleted"));
+ }
+
+ return RedirectToAction("List");
+ }
+
+ #endregion
}