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.

Backend API - Refactor layout controllers and query handlers

KrzysztofPajak1aadb363b8b0
diff --git a/src/API/Grand.Api/Controllers/OData/BrandLayoutController.cs b/src/API/Grand.Api/Controllers/OData/BrandLayoutController.cs
index cbb734521..ddde40ae8 100644
--- a/src/API/Grand.Api/Controllers/OData/BrandLayoutController.cs
+++ b/src/API/Grand.Api/Controllers/OData/BrandLayoutController.cs
@@ -1,4 +1,5 @@
-using Grand.Api.Queries.Models.Common;
+using Grand.Api.DTOs.Common;
+using Grand.Api.Queries.Models.Common;
using Grand.Business.Core.Interfaces.Common.Security;
using Grand.Business.Core.Utilities.Common.Security;
using Grand.Domain.Catalog;
@@ -33,7 +34,7 @@ public class BrandLayoutController : BaseODataController
{
if (!await _permissionService.Authorize(PermissionSystemName.Maintenance)) return Forbid();
- var layout = await _mediator.Send(new GetLayoutQuery { Id = key, LayoutName = typeof(BrandLayout).Name });
+ var layout = await _mediator.Send(new GetGenericQuery<LayoutDto, BrandLayout>(key));
if (!layout.Any()) return NotFound();
return Ok(layout.FirstOrDefault());
@@ -48,6 +49,6 @@ public class BrandLayoutController : BaseODataController
{
if (!await _permissionService.Authorize(PermissionSystemName.Maintenance)) return Forbid();
- return Ok(await _mediator.Send(new GetLayoutQuery { LayoutName = typeof(BrandLayout).Name }));
+ return Ok(await _mediator.Send(new GetGenericQuery<LayoutDto, BrandLayout>()));
}
}
\ No newline at end of file
diff --git a/src/API/Grand.Api/Controllers/OData/CategoryLayoutController.cs b/src/API/Grand.Api/Controllers/OData/CategoryLayoutController.cs
index 455f79ee2..63ef2a13d 100644
--- a/src/API/Grand.Api/Controllers/OData/CategoryLayoutController.cs
+++ b/src/API/Grand.Api/Controllers/OData/CategoryLayoutController.cs
@@ -1,4 +1,5 @@
-using Grand.Api.Queries.Models.Common;
+using Grand.Api.DTOs.Common;
+using Grand.Api.Queries.Models.Common;
using Grand.Business.Core.Interfaces.Common.Security;
using Grand.Business.Core.Utilities.Common.Security;
using Grand.Domain.Catalog;
@@ -35,7 +36,7 @@ public class CategoryLayoutController : BaseODataController
{
if (!await _permissionService.Authorize(PermissionSystemName.Maintenance)) return Forbid();
- var layout = await _mediator.Send(new GetLayoutQuery { Id = key, LayoutName = typeof(CategoryLayout).Name });
+ var layout = await _mediator.Send(new GetGenericQuery<LayoutDto, CategoryLayout>(key));
if (!layout.Any()) return NotFound();
return Ok(layout.FirstOrDefault());
@@ -50,6 +51,6 @@ public class CategoryLayoutController : BaseODataController
{
if (!await _permissionService.Authorize(PermissionSystemName.Maintenance)) return Forbid();
- return Ok(await _mediator.Send(new GetLayoutQuery { LayoutName = typeof(CategoryLayout).Name }));
+ return Ok(await _mediator.Send(new GetGenericQuery<LayoutDto, CategoryLayout>()));
}
}
\ No newline at end of file
diff --git a/src/API/Grand.Api/Controllers/OData/CollectionLayoutController.cs b/src/API/Grand.Api/Controllers/OData/CollectionLayoutController.cs
index 41ba933f8..4b483fb8f 100644
--- a/src/API/Grand.Api/Controllers/OData/CollectionLayoutController.cs
+++ b/src/API/Grand.Api/Controllers/OData/CollectionLayoutController.cs
@@ -1,4 +1,5 @@
-using Grand.Api.Queries.Models.Common;
+using Grand.Api.DTOs.Common;
+using Grand.Api.Queries.Models.Common;
using Grand.Business.Core.Interfaces.Common.Security;
using Grand.Business.Core.Utilities.Common.Security;
using Grand.Domain.Catalog;
@@ -33,7 +34,7 @@ public class CollectionLayoutController : BaseODataController
{
if (!await _permissionService.Authorize(PermissionSystemName.Maintenance)) return Forbid();
- var layout = await _mediator.Send(new GetLayoutQuery { Id = key, LayoutName = typeof(CollectionLayout).Name });
+ var layout = await _mediator.Send(new GetGenericQuery<LayoutDto, CollectionLayout>(key));
if (!layout.Any()) return NotFound();
return Ok(layout.FirstOrDefault());
@@ -48,6 +49,6 @@ public class CollectionLayoutController : BaseODataController
{
if (!await _permissionService.Authorize(PermissionSystemName.Maintenance)) return Forbid();
- return Ok(await _mediator.Send(new GetLayoutQuery { LayoutName = typeof(CollectionLayout).Name }));
+ return Ok(await _mediator.Send(new GetGenericQuery<LayoutDto, CollectionLayout>()));
}
}
\ No newline at end of file
diff --git a/src/API/Grand.Api/Controllers/OData/ProductLayoutController.cs b/src/API/Grand.Api/Controllers/OData/ProductLayoutController.cs
index 5980c9e46..0b5791dcd 100644
--- a/src/API/Grand.Api/Controllers/OData/ProductLayoutController.cs
+++ b/src/API/Grand.Api/Controllers/OData/ProductLayoutController.cs
@@ -1,4 +1,5 @@
-using Grand.Api.Queries.Models.Common;
+using Grand.Api.DTOs.Common;
+using Grand.Api.Queries.Models.Common;
using Grand.Business.Core.Interfaces.Common.Security;
using Grand.Business.Core.Utilities.Common.Security;
using Grand.Domain.Catalog;
@@ -33,7 +34,7 @@ public class ProductLayoutController : BaseODataController
{
if (!await _permissionService.Authorize(PermissionSystemName.Maintenance)) return Forbid();
- var layout = await _mediator.Send(new GetLayoutQuery { Id = key, LayoutName = typeof(ProductLayout).Name });
+ var layout = await _mediator.Send(new GetGenericQuery<LayoutDto, ProductLayout>(key));
if (!layout.Any()) return NotFound();
return Ok(layout.FirstOrDefault());
@@ -47,7 +48,6 @@ public class ProductLayoutController : BaseODataController
public async Task<IActionResult> Get()
{
if (!await _permissionService.Authorize(PermissionSystemName.Maintenance)) return Forbid();
-
- return Ok(await _mediator.Send(new GetLayoutQuery { LayoutName = typeof(ProductLayout).Name }));
+ return Ok(await _mediator.Send(new GetGenericQuery<LayoutDto, ProductLayout>()));
}
}
\ No newline at end of file
diff --git a/src/API/Grand.Api/Infrastructure/ODataStartup.cs b/src/API/Grand.Api/Infrastructure/ODataStartup.cs
index 85afd18cf..3400064bd 100644
--- a/src/API/Grand.Api/Infrastructure/ODataStartup.cs
+++ b/src/API/Grand.Api/Infrastructure/ODataStartup.cs
@@ -135,7 +135,11 @@ public class ODataStartup : IStartupApplication
(typeof(CustomerGroupDto), typeof(CustomerGroup)),
(typeof(StoreDto), typeof(Store)),
(typeof(LanguageDto), typeof(Language)),
- (typeof(PictureDto), typeof(Picture))
+ (typeof(PictureDto), typeof(Picture)),
+ (typeof(LayoutDto), typeof(BrandLayout)),
+ (typeof(LayoutDto), typeof(CollectionLayout)),
+ (typeof(LayoutDto), typeof(CategoryLayout)),
+ (typeof(LayoutDto), typeof(ProductLayout))
};
foreach (var (dto, entity) in handlerTypes)
diff --git a/src/API/Grand.Api/Queries/Handlers/Common/GetGenericQueryHandler.cs b/src/API/Grand.Api/Queries/Handlers/Common/GetGenericQueryHandler.cs
index d5a4ab96a..42f9e5187 100644
--- a/src/API/Grand.Api/Queries/Handlers/Common/GetGenericQueryHandler.cs
+++ b/src/API/Grand.Api/Queries/Handlers/Common/GetGenericQueryHandler.cs
@@ -10,17 +10,16 @@ public class GetGenericQueryHandler<T, C> : IRequestHandler<GetGenericQuery<T, C
where T : BaseApiEntityModel
where C : BaseEntity
{
- private readonly IDatabaseContext _dbContext;
+ private readonly IRepository<C> _repository;
- public GetGenericQueryHandler(IDatabaseContext dbContext)
+ public GetGenericQueryHandler(IRepository<C> repository)
{
- _dbContext = dbContext;
+ _repository = repository;
}
public async Task<IQueryable<T>> Handle(GetGenericQuery<T, C> request, CancellationToken cancellationToken)
{
- var query = _dbContext.Table<T>(typeof(C).Name);
-
+ var query = _repository.TableCollection<T>();
if (string.IsNullOrEmpty(request.Id))
return query;
return await Task.FromResult(query.Where(x => x.Id == request.Id));
diff --git a/src/API/Grand.Api/Queries/Handlers/Common/GetLayoutQueryHandler.cs b/src/API/Grand.Api/Queries/Handlers/Common/GetLayoutQueryHandler.cs
deleted file mode 100644
index 61c2dda5f..000000000
--- a/src/API/Grand.Api/Queries/Handlers/Common/GetLayoutQueryHandler.cs
+++ /dev/null
@@ -1,25 +0,0 @@
-using Grand.Api.DTOs.Common;
-using Grand.Api.Queries.Models.Common;
-using Grand.Data;
-using MediatR;
-
-namespace Grand.Api.Queries.Handlers.Common;
-
-public class GetLayoutQueryHandler : IRequestHandler<GetLayoutQuery, IQueryable<LayoutDto>>
-{
- private readonly IDatabaseContext _dbContext;
-
- public GetLayoutQueryHandler(IDatabaseContext dbContext)
- {
- _dbContext = dbContext;
- }
-
- public async Task<IQueryable<LayoutDto>> Handle(GetLayoutQuery request, CancellationToken cancellationToken)
- {
- var query = _dbContext.Table<LayoutDto>(request.LayoutName);
-
- if (string.IsNullOrEmpty(request.Id))
- return query;
- return await Task.FromResult(query.Where(x => x.Id == request.Id));
- }
-}
\ No newline at end of file
diff --git a/src/API/Grand.Api/Queries/Models/Common/GetLayoutQuery.cs b/src/API/Grand.Api/Queries/Models/Common/GetLayoutQuery.cs
deleted file mode 100644
index cbc8ae41a..000000000
--- a/src/API/Grand.Api/Queries/Models/Common/GetLayoutQuery.cs
+++ /dev/null
@@ -1,10 +0,0 @@
-using Grand.Api.DTOs.Common;
-using MediatR;
-
-namespace Grand.Api.Queries.Models.Common;
-
-public class GetLayoutQuery : IRequest<IQueryable<LayoutDto>>
-{
- public string Id { get; set; }
- public string LayoutName { get; set; }
-}
\ No newline at end of file