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
Implement GenericQueryHandler - use with Country
support3a0207fdd932
diff --git a/src/API/Grand.Api/Controllers/OData/CountryController.cs b/src/API/Grand.Api/Controllers/OData/CountryController.csindex 3689b0660..3f9be1e52 100644--- a/src/API/Grand.Api/Controllers/OData/CountryController.cs+++ b/src/API/Grand.Api/Controllers/OData/CountryController.cs@@ -31,7 +31,8 @@ namespace Grand.Api.Controllers.ODataif (!await _permissionService.Authorize(PermissionSystemName.Countries))return Forbid();- var country = await _mediator.Send(new GetQuery<CountryDto>() { Id = key });+ //Domain.Directory.Country+ var country = await _mediator.Send(new GetGenericQuery<CountryDto, Domain.Directory.Country>() { Id = key });if (!country.Any())return NotFound();@@ -48,7 +49,7 @@ namespace Grand.Api.Controllers.ODataif (!await _permissionService.Authorize(PermissionSystemName.Countries))return Forbid();- return Ok(await _mediator.Send(new GetQuery<CountryDto>()));+ return Ok(await _mediator.Send(new GetGenericQuery<CountryDto, Domain.Directory.Country>()));}}}diff --git a/src/API/Grand.Api/Infrastructure/ODataStartup.cs b/src/API/Grand.Api/Infrastructure/ODataStartup.csindex 6fe4d53cd..9b864535b 100644--- a/src/API/Grand.Api/Infrastructure/ODataStartup.cs+++ b/src/API/Grand.Api/Infrastructure/ODataStartup.cs@@ -1,8 +1,12 @@using Grand.Api.Constants;+using Grand.Api.DTOs.Common;using Grand.Api.Infrastructure.DependencyManagement;+using Grand.Api.Queries.Handlers.Common;+using Grand.Api.Queries.Models.Common;using Grand.Infrastructure;using Grand.Infrastructure.Configuration;using Grand.Infrastructure.TypeSearchers;+using MediatR;using Microsoft.AspNetCore.Builder;using Microsoft.AspNetCore.Hosting;using Microsoft.AspNetCore.OData;@@ -30,6 +34,9 @@ namespace Grand.Api.Infrastructurevar apiConfig = services.BuildServiceProvider().GetService<ApiConfig>();if (apiConfig.Enabled){+ //register RequestHandler+ RegisterRequestHandler(services);+//corsservices.AddCors(options =>{@@ -77,5 +84,15 @@ namespace Grand.Api.InfrastructuredependencyRegistrar.Register(builder, apiConfig);}++ private void RegisterRequestHandler(IServiceCollection services)+ {++ //Workaround - there is a problem with register generic type with IRequestHandler+ services.AddScoped(typeof(IRequestHandler<GetGenericQuery<CountryDto, Domain.Directory.Country>,+ IQueryable<CountryDto>>), typeof(GetGenericQueryHandler<CountryDto, Domain.Directory.Country>));+++ }}}diff --git a/src/API/Grand.Api/Queries/Handlers/Common/GetCountryQueryHandler.cs b/src/API/Grand.Api/Queries/Handlers/Common/GetGenericQueryHandler.cssimilarity index 50%rename from src/API/Grand.Api/Queries/Handlers/Common/GetCountryQueryHandler.csrename to src/API/Grand.Api/Queries/Handlers/Common/GetGenericQueryHandler.csindex c375aa4b5..9533ba484 100644--- a/src/API/Grand.Api/Queries/Handlers/Common/GetCountryQueryHandler.cs+++ b/src/API/Grand.Api/Queries/Handlers/Common/GetGenericQueryHandler.cs@@ -1,21 +1,24 @@-using Grand.Api.DTOs.Common;+using Grand.Api.Models;using Grand.Api.Queries.Models.Common;+using Grand.Domain;using Grand.Domain.Data;using MediatR;namespace Grand.Api.Queries.Handlers.Common{- public class GetCountryQueryHandler : IRequestHandler<GetQuery<CountryDto>, IQueryable<CountryDto>>+ public class GetGenericQueryHandler<T,C> : IRequestHandler<GetGenericQuery<T,C>, IQueryable<T>>+ where T : BaseApiEntityModel+ where C : BaseEntity{private readonly IDatabaseContext _dbContext;- public GetCountryQueryHandler(IDatabaseContext dbContext)+ public GetGenericQueryHandler(IDatabaseContext dbContext){_dbContext = dbContext;}- public async Task<IQueryable<CountryDto>> Handle(GetQuery<CountryDto> request, CancellationToken cancellationToken)+ public async Task<IQueryable<T>> Handle(GetGenericQuery<T,C> request, CancellationToken cancellationToken){- var query = _dbContext.Table<CountryDto>(typeof(Domain.Directory.Country).Name);+ var query = _dbContext.Table<T>(typeof(C).Name);if (string.IsNullOrEmpty(request.Id))return query;diff --git a/src/API/Grand.Api/Queries/Models/Common/GetGenericQuery.cs b/src/API/Grand.Api/Queries/Models/Common/GetGenericQuery.csnew file mode 100644index 000000000..e440c4772--- /dev/null+++ b/src/API/Grand.Api/Queries/Models/Common/GetGenericQuery.cs@@ -0,0 +1,13 @@+using Grand.Api.Models;+using Grand.Domain;+using MediatR;++namespace Grand.Api.Queries.Models.Common+{+ public class GetGenericQuery<T,C> : IRequest<IQueryable<T>>+ where T : BaseApiEntityModel+ where C : BaseEntity+ {+ public string Id { get; set; }+ }+}