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.
C# aspnet-corecsharpdockerdotnetecommerceecommerce-platformmongodbmulti-storemulti-vendornosqlonline-shopshoppingcart
Grand.API - minor refactoring - use GenericQueryHandler(Customer group, Language, Picture, Store, Vendor)
support25a06562b91a
diff --git a/src/API/Grand.Api/Controllers/OData/CustomerGroupController.cs b/src/API/Grand.Api/Controllers/OData/CustomerGroupController.csindex 8c90dc217..ae45e8f83 100644--- a/src/API/Grand.Api/Controllers/OData/CustomerGroupController.cs+++ b/src/API/Grand.Api/Controllers/OData/CustomerGroupController.cs@@ -34,7 +34,7 @@ namespace Grand.Api.Controllers.ODataif (!await _permissionService.Authorize(PermissionSystemName.Customers))return Forbid();- var customerGroup = await _mediator.Send(new GetQuery<CustomerGroupDto>() { Id = key });+ var customerGroup = await _mediator.Send(new GetGenericQuery<CustomerGroupDto, Domain.Customers.CustomerGroup>(key));if (!customerGroup.Any())return NotFound();@@ -51,7 +51,7 @@ namespace Grand.Api.Controllers.ODataif (!await _permissionService.Authorize(PermissionSystemName.Customers))return Forbid();- return Ok(await _mediator.Send(new GetQuery<CustomerGroupDto>()));+ return Ok(await _mediator.Send(new GetGenericQuery<CustomerGroupDto, Domain.Customers.CustomerGroup>()));}[SwaggerOperation(summary: "Add new entity to CustomerGroup", OperationId = "InsertCustomerGroup")]@@ -83,7 +83,7 @@ namespace Grand.Api.Controllers.ODataif (!await _permissionService.Authorize(PermissionSystemName.Customers))return Forbid();- var customerGroup = await _mediator.Send(new GetQuery<CustomerGroupDto>() { Id = model.Id });+ var customerGroup = await _mediator.Send(new GetGenericQuery<CustomerGroupDto, Domain.Customers.CustomerGroup>(model.Id));if (!customerGroup.Any()){return NotFound();@@ -108,7 +108,7 @@ namespace Grand.Api.Controllers.ODataif (!await _permissionService.Authorize(PermissionSystemName.Customers))return Forbid();- var customerGroup = await _mediator.Send(new GetQuery<CustomerGroupDto>() { Id = key });+ var customerGroup = await _mediator.Send(new GetGenericQuery<CustomerGroupDto, Domain.Customers.CustomerGroup>(key));if (!customerGroup.Any()){return NotFound();@@ -135,7 +135,7 @@ namespace Grand.Api.Controllers.ODataif (!await _permissionService.Authorize(PermissionSystemName.Customers))return Forbid();- var customerGroup = await _mediator.Send(new GetQuery<CustomerGroupDto>() { Id = key });+ var customerGroup = await _mediator.Send(new GetGenericQuery<CustomerGroupDto, Domain.Customers.CustomerGroup>(key));if (!customerGroup.Any()){return NotFound();diff --git a/src/API/Grand.Api/Controllers/OData/LanguageController.cs b/src/API/Grand.Api/Controllers/OData/LanguageController.csindex 17c188066..26c6629b7 100644--- a/src/API/Grand.Api/Controllers/OData/LanguageController.cs+++ b/src/API/Grand.Api/Controllers/OData/LanguageController.cs@@ -31,7 +31,7 @@ namespace Grand.Api.Controllers.ODataif (!await _permissionService.Authorize(PermissionSystemName.Languages))return Forbid();- var language = await _mediator.Send(new GetQuery<LanguageDto>() { Id = key });+ var language = await _mediator.Send(new GetGenericQuery<LanguageDto, Domain.Localization.Language>(key));if (!language.Any())return NotFound();@@ -48,7 +48,7 @@ namespace Grand.Api.Controllers.ODataif (!await _permissionService.Authorize(PermissionSystemName.Languages))return Forbid();- return Ok(await _mediator.Send(new GetQuery<LanguageDto>()));+ return Ok(await _mediator.Send(new GetGenericQuery<LanguageDto, Domain.Localization.Language>()));}}}diff --git a/src/API/Grand.Api/Controllers/OData/PictureController.cs b/src/API/Grand.Api/Controllers/OData/PictureController.csindex acc8a1d9b..a228c85f7 100644--- a/src/API/Grand.Api/Controllers/OData/PictureController.cs+++ b/src/API/Grand.Api/Controllers/OData/PictureController.cs@@ -32,11 +32,11 @@ namespace Grand.Api.Controllers.ODataif (!await _permissionService.Authorize(PermissionSystemName.Pictures))return Forbid();- var picture = await _mediator.Send(new GetPictureByIdQuery() { Id = key });- if (picture == null)+ var picture = await _mediator.Send(new GetGenericQuery<PictureDto, Domain.Media.Picture>(key));+ if (picture == null || !picture.Any())return NotFound();- return Ok(picture);+ return Ok(picture.FirstOrDefault());}[SwaggerOperation(summary: "Add new entity in Picture", OperationId = "InsertPicture")]@@ -70,8 +70,8 @@ namespace Grand.Api.Controllers.ODataif (ModelState.IsValid){- var picture = await _mediator.Send(new GetPictureByIdQuery() { Id = model.Id });- if (picture == null)+ var picture = await _mediator.Send(new GetGenericQuery<PictureDto, Domain.Media.Picture>(model.Id));+ if (picture == null || !picture.Any()){return NotFound();}@@ -92,12 +92,12 @@ namespace Grand.Api.Controllers.ODataif (!await _permissionService.Authorize(PermissionSystemName.Pictures))return Forbid();- var picture = await _mediator.Send(new GetPictureByIdQuery() { Id = key });- if (picture == null)+ var picture = await _mediator.Send(new GetGenericQuery<PictureDto, Domain.Media.Picture>(key));+ if (picture == null || !picture.Any()){return NotFound();}- await _mediator.Send(new DeletePictureCommand() { PictureDto = picture });+ await _mediator.Send(new DeletePictureCommand() { PictureDto = picture.FirstOrDefault() });return Ok();}}diff --git a/src/API/Grand.Api/Controllers/OData/StateProvinceController.cs b/src/API/Grand.Api/Controllers/OData/StateProvinceController.csdeleted file mode 100644index 327fa973d..000000000--- a/src/API/Grand.Api/Controllers/OData/StateProvinceController.cs+++ /dev/null@@ -1,54 +0,0 @@-using Grand.Api.DTOs.Common;-using Grand.Api.Queries.Models.Common;-using Grand.Business.Common.Interfaces.Security;-using Grand.Business.Common.Services.Security;-using MediatR;-using Microsoft.AspNetCore.Mvc;-using Microsoft.AspNetCore.OData.Query;-using Swashbuckle.AspNetCore.Annotations;-using System.Net;--namespace Grand.Api.Controllers.OData-{- public partial class StateProvinceController : BaseODataController- {- private readonly IMediator _mediator;- private readonly IPermissionService _permissionService;-- public StateProvinceController(IMediator mediator, IPermissionService permissionService)- {- _mediator = mediator;- _permissionService = permissionService;- }-- [SwaggerOperation(summary: "Get entity from StateProvince by key", OperationId = "GetStateProvinceById")]- [HttpGet("{key}")]- [ProducesResponseType((int)HttpStatusCode.Forbidden)]- [ProducesResponseType((int)HttpStatusCode.OK)]- [ProducesResponseType((int)HttpStatusCode.NotFound)]- public async Task<IActionResult> Get(string key)- {- if (!await _permissionService.Authorize(PermissionSystemName.Countries))- return Forbid();-- var states = await _mediator.Send(new GetQuery<StateProvinceDto>() { Id = key });- if (!states.Any())- return NotFound();-- return Ok(states);- }-- [SwaggerOperation(summary: "Get entities from StateProvince", OperationId = "GetStateProvinces")]- [HttpGet]- [EnableQuery(HandleNullPropagation = HandleNullPropagationOption.False)]- [ProducesResponseType((int)HttpStatusCode.Forbidden)]- [ProducesResponseType((int)HttpStatusCode.OK)]- public async Task<IActionResult> Get()- {- if (!await _permissionService.Authorize(PermissionSystemName.Countries))- return Forbid();-- return Ok(await _mediator.Send(new GetQuery<StateProvinceDto>()));- }- }-}diff --git a/src/API/Grand.Api/Controllers/OData/StoreController.cs b/src/API/Grand.Api/Controllers/OData/StoreController.csindex e6d203587..b869c6ebb 100644--- a/src/API/Grand.Api/Controllers/OData/StoreController.cs+++ b/src/API/Grand.Api/Controllers/OData/StoreController.cs@@ -31,7 +31,7 @@ namespace Grand.Api.Controllers.ODataif (!await _permissionService.Authorize(PermissionSystemName.Stores))return Forbid();- var store = await _mediator.Send(new GetQuery<StoreDto>() { Id = key });+ var store = await _mediator.Send(new GetGenericQuery<StoreDto, Domain.Stores.Store>(key));if (!store.Any())return NotFound();@@ -48,7 +48,7 @@ namespace Grand.Api.Controllers.ODataif (!await _permissionService.Authorize(PermissionSystemName.Stores))return Forbid();- return Ok(await _mediator.Send(new GetQuery<StoreDto>()));+ return Ok(await _mediator.Send(new GetGenericQuery<StoreDto, Domain.Stores.Store>()));}}}diff --git a/src/API/Grand.Api/Controllers/OData/VendorController.cs b/src/API/Grand.Api/Controllers/OData/VendorController.csindex 4e8ac8325..efdbb96be 100644--- a/src/API/Grand.Api/Controllers/OData/VendorController.cs+++ b/src/API/Grand.Api/Controllers/OData/VendorController.cs@@ -31,7 +31,7 @@ namespace Grand.Api.Controllers.ODataif (!await _permissionService.Authorize(PermissionSystemName.Vendors))return Forbid();- var vendor = await _mediator.Send(new GetQuery<VendorDto>() { Id = key });+ var vendor = await _mediator.Send(new GetGenericQuery<VendorDto, Domain.Vendors.Vendor>(key));if (!vendor.Any())return NotFound();@@ -49,7 +49,7 @@ namespace Grand.Api.Controllers.ODataif (!await _permissionService.Authorize(PermissionSystemName.Vendors))return Forbid();- return Ok(await _mediator.Send(new GetQuery<VendorDto>()));+ return Ok(await _mediator.Send(new GetGenericQuery<VendorDto, Domain.Vendors.Vendor>()));}}}diff --git a/src/API/Grand.Api/DTOs/Common/CountryDto.cs b/src/API/Grand.Api/DTOs/Common/CountryDto.csindex 6e20d66ba..c8b512efd 100644--- a/src/API/Grand.Api/DTOs/Common/CountryDto.cs+++ b/src/API/Grand.Api/DTOs/Common/CountryDto.cs@@ -4,6 +4,8 @@ namespace Grand.Api.DTOs.Common{public partial class CountryDto : BaseApiEntityModel{+ private ICollection<StateProvinceDto> _stateProvinces;+public string Name { get; set; }public bool AllowsBilling { get; set; }public bool AllowsShipping { get; set; }@@ -13,5 +15,18 @@ namespace Grand.Api.DTOs.Commonpublic bool SubjectToVat { get; set; }public bool Published { get; set; }public int DisplayOrder { get; set; }++ public virtual ICollection<StateProvinceDto> StateProvinces {+ get { return _stateProvinces ??= new List<StateProvinceDto>(); }+ protected set { _stateProvinces = value; }+ }++ public class StateProvinceDto+ {+ public string Name { get; set; }+ public string Abbreviation { get; set; }+ public bool Published { get; set; }+ public int DisplayOrder { get; set; }+ }}}diff --git a/src/API/Grand.Api/DTOs/Common/StateProvinceDto.cs b/src/API/Grand.Api/DTOs/Common/StateProvinceDto.csdeleted file mode 100644index c2bd57172..000000000--- a/src/API/Grand.Api/DTOs/Common/StateProvinceDto.cs+++ /dev/null@@ -1,13 +0,0 @@-using Grand.Api.Models;--namespace Grand.Api.DTOs.Common-{- public partial class StateProvinceDto : BaseApiEntityModel- {- public string CountryId { get; set; }- public string Name { get; set; }- public string Abbreviation { get; set; }- public bool Published { get; set; }- public int DisplayOrder { get; set; }- }-}diff --git a/src/API/Grand.Api/DTOs/Customers/CustomerDto.cs b/src/API/Grand.Api/DTOs/Customers/CustomerDto.csindex 490b3be52..500bf0c34 100644--- a/src/API/Grand.Api/DTOs/Customers/CustomerDto.cs+++ b/src/API/Grand.Api/DTOs/Customers/CustomerDto.cs@@ -1,16 +1,16 @@-using System.ComponentModel.DataAnnotations;+using Grand.Api.Models;+using System.ComponentModel.DataAnnotations;namespace Grand.Api.DTOs.Customers{- public partial class CustomerDto+ public partial class CustomerDto : BaseApiEntityModel{public CustomerDto(){- this.CustomerGuid = Guid.NewGuid();- this.Groups = new List<string>();- this.Addresses = new List<AddressDto>();+ CustomerGuid = Guid.NewGuid();+ Groups = new List<string>();+ Addresses = new List<AddressDto>();}- public string Id { get; set; }public Guid CustomerGuid { get; set; }public string Username { get; set; }[Key]diff --git a/src/API/Grand.Api/Infrastructure/DependencyEdmModel.cs b/src/API/Grand.Api/Infrastructure/DependencyEdmModel.csindex 3fc65b6d6..fe72abffa 100644--- a/src/API/Grand.Api/Infrastructure/DependencyEdmModel.cs+++ b/src/API/Grand.Api/Infrastructure/DependencyEdmModel.cs@@ -37,12 +37,6 @@ namespace Grand.Api.Infrastructure#endregion- #region State province model-- builder.EntitySet<StateProvinceDto>("StateProvince");-- #endregion-#region layout modelbuilder.EntitySet<LayoutDto>("CategoryLayout");diff --git a/src/API/Grand.Api/Infrastructure/ODataStartup.cs b/src/API/Grand.Api/Infrastructure/ODataStartup.csindex 0007478d6..ca01cef2a 100644--- a/src/API/Grand.Api/Infrastructure/ODataStartup.cs+++ b/src/API/Grand.Api/Infrastructure/ODataStartup.cs@@ -1,6 +1,7 @@using Grand.Api.Constants;using Grand.Api.DTOs.Catalog;using Grand.Api.DTOs.Common;+using Grand.Api.DTOs.Customers;using Grand.Api.DTOs.Shipping;using Grand.Api.Infrastructure.DependencyManagement;using Grand.Api.Queries.Handlers.Common;@@ -128,6 +129,21 @@ namespace Grand.Api.Infrastructureservices.AddScoped(typeof(IRequestHandler<GetGenericQuery<DeliveryDateDto, Domain.Shipping.DeliveryDate>,IQueryable<DeliveryDateDto>>), typeof(GetGenericQueryHandler<DeliveryDateDto, Domain.Shipping.DeliveryDate>));+ services.AddScoped(typeof(IRequestHandler<GetGenericQuery<VendorDto, Domain.Vendors.Vendor>,+ IQueryable<VendorDto>>), typeof(GetGenericQueryHandler<VendorDto, Domain.Vendors.Vendor>));++ services.AddScoped(typeof(IRequestHandler<GetGenericQuery<CustomerGroupDto, Domain.Customers.CustomerGroup>,+ IQueryable<CustomerGroupDto>>), typeof(GetGenericQueryHandler<CustomerGroupDto, Domain.Customers.CustomerGroup>));++ services.AddScoped(typeof(IRequestHandler<GetGenericQuery<StoreDto, Domain.Stores.Store>,+ IQueryable<StoreDto>>), typeof(GetGenericQueryHandler<StoreDto, Domain.Stores.Store>));++ services.AddScoped(typeof(IRequestHandler<GetGenericQuery<LanguageDto, Domain.Localization.Language>,+ IQueryable<LanguageDto>>), typeof(GetGenericQueryHandler<LanguageDto, Domain.Localization.Language>));++ services.AddScoped(typeof(IRequestHandler<GetGenericQuery<PictureDto, Domain.Media.Picture>,+ IQueryable<PictureDto>>), typeof(GetGenericQueryHandler<PictureDto, Domain.Media.Picture>));+}}}diff --git a/src/API/Grand.Api/Queries/Handlers/Common/GetLanguageQueryHandler.cs b/src/API/Grand.Api/Queries/Handlers/Common/GetLanguageQueryHandler.csdeleted file mode 100644index 110d68fea..000000000--- a/src/API/Grand.Api/Queries/Handlers/Common/GetLanguageQueryHandler.cs+++ /dev/null@@ -1,27 +0,0 @@-using Grand.Api.DTOs.Common;-using Grand.Api.Queries.Models.Common;-using Grand.Domain.Data;-using MediatR;--namespace Grand.Api.Queries.Handlers.Common-{- public class GetLanguageQueryHandler : IRequestHandler<GetQuery<LanguageDto>, IQueryable<LanguageDto>>- {- private readonly IDatabaseContext _dbContext;-- public GetLanguageQueryHandler(IDatabaseContext dbContext)- {- _dbContext = dbContext;- }- public async Task<IQueryable<LanguageDto>> Handle(GetQuery<LanguageDto> request, CancellationToken cancellationToken)- {- var query = _dbContext.Table<LanguageDto>(typeof(Domain.Localization.Language).Name);-- if (string.IsNullOrEmpty(request.Id))- return query;- else- return await Task.FromResult(query.Where(x => x.Id == request.Id));-- }- }-}diff --git a/src/API/Grand.Api/Queries/Handlers/Common/GetPictureByIdQueryHandler.cs b/src/API/Grand.Api/Queries/Handlers/Common/GetPictureByIdQueryHandler.csdeleted file mode 100644index 8765ea708..000000000--- a/src/API/Grand.Api/Queries/Handlers/Common/GetPictureByIdQueryHandler.cs+++ /dev/null@@ -1,23 +0,0 @@-using Grand.Api.DTOs.Common;-using Grand.Api.Queries.Models.Common;-using Grand.Domain.Data;-using MediatR;--namespace Grand.Api.Queries.Handlers.Common-{- public class GetPictureByIdQueryHandler : IRequestHandler<GetPictureByIdQuery, PictureDto>- {- private readonly IDatabaseContext _dbContext;-- public GetPictureByIdQueryHandler(IDatabaseContext dbContext)- {- _dbContext = dbContext;- }-- public async Task<PictureDto> Handle(GetPictureByIdQuery request, CancellationToken cancellationToken)- {- var query = _dbContext.Table<PictureDto>(typeof(Domain.Media.Picture).Name);- return await Task.FromResult(query.Where(x => x.Id == request.Id).FirstOrDefault());- }- }-}diff --git a/src/API/Grand.Api/Queries/Handlers/Common/GetStateProvinceQueryHandler.cs b/src/API/Grand.Api/Queries/Handlers/Common/GetStateProvinceQueryHandler.csdeleted file mode 100644index ed8f22ffa..000000000--- a/src/API/Grand.Api/Queries/Handlers/Common/GetStateProvinceQueryHandler.cs+++ /dev/null@@ -1,27 +0,0 @@-using Grand.Api.DTOs.Common;-using Grand.Api.Queries.Models.Common;-using Grand.Domain.Data;-using MediatR;--namespace Grand.Api.Queries.Handlers.Common-{- public class GetStateProvinceQueryHandler : IRequestHandler<GetQuery<StateProvinceDto>, IQueryable<StateProvinceDto>>- {- private readonly IDatabaseContext _dbContext;-- public GetStateProvinceQueryHandler(IDatabaseContext dbContext)- {- _dbContext = dbContext;- }- public async Task<IQueryable<StateProvinceDto>> Handle(GetQuery<StateProvinceDto> request, CancellationToken cancellationToken)- {- var query = _dbContext.Table<StateProvinceDto>(typeof(Domain.Directory.StateProvince).Name);-- if (string.IsNullOrEmpty(request.Id))- return query;- else- return await Task.FromResult(query.Where(x => x.Id == request.Id));-- }- }-}diff --git a/src/API/Grand.Api/Queries/Handlers/Common/GetStoreQueryHandler.cs b/src/API/Grand.Api/Queries/Handlers/Common/GetStoreQueryHandler.csdeleted file mode 100644index becb2e8eb..000000000--- a/src/API/Grand.Api/Queries/Handlers/Common/GetStoreQueryHandler.cs+++ /dev/null@@ -1,27 +0,0 @@-using Grand.Api.DTOs.Common;-using Grand.Api.Queries.Models.Common;-using Grand.Domain.Data;-using MediatR;--namespace Grand.Api.Queries.Handlers.Common-{- public class GetStoreQueryHandler : IRequestHandler<GetQuery<StoreDto>, IQueryable<StoreDto>>- {- private readonly IDatabaseContext _dbContext;-- public GetStoreQueryHandler(IDatabaseContext dbContext)- {- _dbContext = dbContext;- }- public async Task<IQueryable<StoreDto>> Handle(GetQuery<StoreDto> request, CancellationToken cancellationToken)- {- var query = _dbContext.Table<StoreDto>(typeof(Domain.Stores.Store).Name);-- if (string.IsNullOrEmpty(request.Id))- return query;- else- return await Task.FromResult(query.Where(x => x.Id == request.Id));-- }- }-}diff --git a/src/API/Grand.Api/Queries/Handlers/Customers/GetCustomerGroupQueryHandler.cs b/src/API/Grand.Api/Queries/Handlers/Customers/GetCustomerGroupQueryHandler.csdeleted file mode 100644index 03b677b30..000000000--- a/src/API/Grand.Api/Queries/Handlers/Customers/GetCustomerGroupQueryHandler.cs+++ /dev/null@@ -1,27 +0,0 @@-using Grand.Api.DTOs.Customers;-using Grand.Api.Queries.Models.Common;-using Grand.Domain.Data;-using MediatR;--namespace Grand.Api.Queries.Handlers.Customers-{- public class GetCustomerGroupQueryHandler : IRequestHandler<GetQuery<CustomerGroupDto>, IQueryable<CustomerGroupDto>>- {- private readonly IDatabaseContext _dbContext;-- public GetCustomerGroupQueryHandler(IDatabaseContext dbContext)- {- _dbContext = dbContext;- }- public async Task<IQueryable<CustomerGroupDto>> Handle(GetQuery<CustomerGroupDto> request, CancellationToken cancellationToken)- {- var query = _dbContext.Table<CustomerGroupDto>(typeof(Domain.Customers.CustomerGroup).Name);-- if (string.IsNullOrEmpty(request.Id))- return query;- else- return await Task.FromResult(query.Where(x => x.Id == request.Id));-- }- }-}diff --git a/src/API/Grand.Api/Queries/Handlers/Customers/GetVendorQueryHandler.cs b/src/API/Grand.Api/Queries/Handlers/Customers/GetVendorQueryHandler.csdeleted file mode 100644index 9bf4ef488..000000000--- a/src/API/Grand.Api/Queries/Handlers/Customers/GetVendorQueryHandler.cs+++ /dev/null@@ -1,27 +0,0 @@-using Grand.Api.DTOs.Customers;-using Grand.Api.Queries.Models.Common;-using Grand.Domain.Data;-using MediatR;--namespace Grand.Api.Queries.Handlers.Customers-{- public class GetVendorQueryHandler : IRequestHandler<GetQuery<VendorDto>, IQueryable<VendorDto>>- {- private readonly IDatabaseContext _dbContext;-- public GetVendorQueryHandler(IDatabaseContext dbContext)- {- _dbContext = dbContext;- }- public async Task<IQueryable<VendorDto>> Handle(GetQuery<VendorDto> request, CancellationToken cancellationToken)- {- var query = _dbContext.Table<VendorDto>(typeof(Domain.Vendors.Vendor).Name);-- if (string.IsNullOrEmpty(request.Id))- return query;- else- return await Task.FromResult(query.Where(x => x.Id == request.Id));-- }- }-}diff --git a/src/API/Grand.Api/Queries/Models/Common/GetPictureByIdQuery.cs b/src/API/Grand.Api/Queries/Models/Common/GetPictureByIdQuery.csdeleted file mode 100644index d98aefb25..000000000--- a/src/API/Grand.Api/Queries/Models/Common/GetPictureByIdQuery.cs+++ /dev/null@@ -1,10 +0,0 @@-using Grand.Api.DTOs.Common;-using MediatR;--namespace Grand.Api.Queries.Models.Common-{- public class GetPictureByIdQuery : IRequest<PictureDto>- {- public string Id { get; set; }- }-}