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 Picture region to BaseCollectionController

KrzysztofPajakfd3225d758b6
diff --git a/src/Tests/Grand.Web.Admin.Tests/Controllers/BaseCollectionControllerTests.cs b/src/Tests/Grand.Web.Admin.Tests/Controllers/BaseCollectionControllerTests.cs
index c91e790af..a86dbf88c 100644
--- a/src/Tests/Grand.Web.Admin.Tests/Controllers/BaseCollectionControllerTests.cs
+++ b/src/Tests/Grand.Web.Admin.Tests/Controllers/BaseCollectionControllerTests.cs
@@ -259,4 +259,116 @@ public class BaseCollectionControllerTests
_collectionViewModelServiceMock.Verify(v => v.InsertCollectionModel(It.IsAny<CollectionModel>()), Times.Once);
}
+
+ // --- PicturePopup --------------------------------------------------------------------------------
+
+ [TestMethod]
+ public async Task PicturePopupGet_ScopeDeniesAccess_ReturnsDeniedContent()
+ {
+ var collection = new Collection { Id = "c1", PictureId = "pic-1" };
+ _collectionServiceMock.Setup(c => c.GetCollectionById("c1")).ReturnsAsync(collection);
+ _scopeMock.Setup(s => s.HasAccess(collection)).ReturnsAsync(false);
+
+ var result = await _controller.PicturePopup("c1");
+
+ var content = result as ContentResult;
+ Assert.IsNotNull(content);
+ Assert.AreEqual("This is not your collection", content.Content);
+ }
+
+ [TestMethod]
+ public async Task PicturePopupGet_CollectionHasNoPicture_ReturnsNotExistContent()
+ {
+ var collection = new Collection { Id = "c1", PictureId = null };
+ _collectionServiceMock.Setup(c => c.GetCollectionById("c1")).ReturnsAsync(collection);
+ _scopeMock.Setup(s => s.HasAccess(collection)).ReturnsAsync(true);
+
+ var result = await _controller.PicturePopup("c1");
+
+ var content = result as ContentResult;
+ Assert.IsNotNull(content);
+ Assert.AreEqual("Picture not exist", content.Content);
+ }
+
+ [TestMethod]
+ public async Task PicturePopupGet_CollectionNotFound_ReturnsNotExistContent()
+ {
+ _collectionServiceMock.Setup(c => c.GetCollectionById("missing")).ReturnsAsync((Collection)null);
+
+ var result = await _controller.PicturePopup("missing");
+
+ var content = result as ContentResult;
+ Assert.IsNotNull(content);
+ Assert.AreEqual("Collection not exist", content.Content);
+ _scopeMock.Verify(s => s.HasAccess(It.IsAny<Collection>()), Times.Never);
+ }
+
+ [TestMethod]
+ public async Task PicturePopupPost_ScopeDeniesAccess_ReturnsDeniedContent()
+ {
+ var collection = new Collection { Id = "c1", PictureId = "pic-1" };
+ _collectionServiceMock.Setup(c => c.GetCollectionById("c1")).ReturnsAsync(collection);
+ _scopeMock.Setup(s => s.HasAccess(collection)).ReturnsAsync(false);
+
+ var model = new Grand.Web.AdminShared.Models.Common.PictureModel { ObjectId = "c1", Id = "pic-1" };
+ var result = await _controller.PicturePopup(model);
+
+ var content = result as ContentResult;
+ Assert.IsNotNull(content);
+ Assert.AreEqual("This is not your collection", content.Content);
+ }
+
+ [TestMethod]
+ public async Task PicturePopupPost_CollectionNotFound_ThrowsArgumentException()
+ {
+ _collectionServiceMock.Setup(c => c.GetCollectionById("missing")).ReturnsAsync((Collection)null);
+
+ var model = new Grand.Web.AdminShared.Models.Common.PictureModel { ObjectId = "missing", Id = "pic-1" };
+
+ var exception = await Assert.ThrowsExactlyAsync<ArgumentException>(
+ async () => await _controller.PicturePopup(model));
+
+ Assert.AreEqual("No collection found with the specified id", exception.Message);
+ }
+
+ [TestMethod]
+ public async Task PicturePopupPost_PictureIdMismatch_ThrowsArgumentException()
+ {
+ var collection = new Collection { Id = "c1", PictureId = "pic-1" };
+ _collectionServiceMock.Setup(c => c.GetCollectionById("c1")).ReturnsAsync(collection);
+ _scopeMock.Setup(s => s.HasAccess(collection)).ReturnsAsync(true);
+
+ var model = new Grand.Web.AdminShared.Models.Common.PictureModel { ObjectId = "c1", Id = "pic-2" };
+
+ var exception = await Assert.ThrowsExactlyAsync<ArgumentException>(
+ async () => await _controller.PicturePopup(model));
+
+ Assert.AreEqual("Picture ident doesn't fit with collection", exception.Message);
+ }
+
+ [TestMethod]
+ public async Task PicturePopupPost_ValidRequest_CallsUpdatePicture()
+ {
+ var pictureViewModelServiceMock = new Mock<IPictureViewModelService>();
+ var collection = new Collection { Id = "c1", PictureId = "pic-1" };
+ _collectionServiceMock.Setup(c => c.GetCollectionById("c1")).ReturnsAsync(collection);
+ _scopeMock.Setup(s => s.HasAccess(collection)).ReturnsAsync(true);
+ pictureViewModelServiceMock.Setup(p => p.UpdatePicture(It.IsAny<Grand.Web.AdminShared.Models.Common.PictureModel>())).Returns(Task.CompletedTask);
+
+ var controller = new TestCollectionController(
+ _collectionViewModelServiceMock.Object, _collectionServiceMock.Object, _storeServiceMock.Object,
+ new Mock<ILanguageService>().Object, _translationServiceMock.Object,
+ pictureViewModelServiceMock.Object, new Mock<IProductService>().Object, _scopeMock.Object);
+ controller.ControllerContext = _controller.ControllerContext;
+ controller.TempData = _controller.TempData;
+
+ var model = new Grand.Web.AdminShared.Models.Common.PictureModel { ObjectId = "c1", Id = "pic-1" };
+
+ var result = await controller.PicturePopup(model);
+
+ var content = result as ContentResult;
+ Assert.IsNotNull(content);
+ Assert.AreEqual("", content.Content);
+ pictureViewModelServiceMock.Verify(p => p.UpdatePicture(model), Times.Once);
+ }
}
diff --git a/src/Web/Grand.Web.AdminShared/Controllers/BaseCollectionController.cs b/src/Web/Grand.Web.AdminShared/Controllers/BaseCollectionController.cs
index 30f94e360..108716970 100644
--- a/src/Web/Grand.Web.AdminShared/Controllers/BaseCollectionController.cs
+++ b/src/Web/Grand.Web.AdminShared/Controllers/BaseCollectionController.cs
@@ -8,6 +8,7 @@ using Grand.Domain.Permissions;
using Grand.Web.AdminShared.Extensions.Mapping;
using Grand.Web.AdminShared.Interfaces;
using Grand.Web.AdminShared.Models.Catalog;
+using Grand.Web.AdminShared.Models.Common;
using Grand.Web.Common.Controllers;
using Grand.Web.Common.DataSource;
using Grand.Web.Common.Filters;
@@ -200,4 +201,43 @@ public abstract class BaseCollectionController(
}
#endregion
+
+ #region Picture
+
+ [PermissionAuthorizeAction(PermissionActionName.Preview)]
+ public async Task<IActionResult> PicturePopup(string collectionId)
+ {
+ var collection = await collectionService.GetCollectionById(collectionId);
+ if (collection == null) return Content("Collection not exist");
+ if (!await scope.HasAccess(collection)) return Content("This is not your collection");
+ if (string.IsNullOrEmpty(collection.PictureId)) return Content("Picture not exist");
+
+ return View("Partials/PicturePopup",
+ await pictureViewModelService.PreparePictureModel(collection.PictureId, collection.Id));
+ }
+
+ [PermissionAuthorizeAction(PermissionActionName.Edit)]
+ [HttpPost]
+ public async Task<IActionResult> PicturePopup(PictureModel model)
+ {
+ if (ModelState.IsValid)
+ {
+ var collection = await collectionService.GetCollectionById(model.ObjectId);
+ if (collection == null)
+ throw new ArgumentException("No collection found with the specified id");
+ if (!await scope.HasAccess(collection)) return Content("This is not your collection");
+ if (string.IsNullOrEmpty(collection.PictureId))
+ throw new ArgumentException("No picture found with the specified id");
+ if (collection.PictureId != model.Id)
+ throw new ArgumentException("Picture ident doesn't fit with collection");
+
+ await pictureViewModelService.UpdatePicture(model);
+ return Content("");
+ }
+
+ Error(ModelState);
+ return View("Partials/PicturePopup", model);
+ }
+
+ #endregion
}