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.

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.cs
index 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.OData
if (!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.OData
if (!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.cs
index 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.Infrastructure
var apiConfig = services.BuildServiceProvider().GetService<ApiConfig>();
if (apiConfig.Enabled)
{
+ //register RequestHandler
+ RegisterRequestHandler(services);
+
//cors
services.AddCors(options =>
{
@@ -77,5 +84,15 @@ namespace Grand.Api.Infrastructure
dependencyRegistrar.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.cs
similarity index 50%
rename from src/API/Grand.Api/Queries/Handlers/Common/GetCountryQueryHandler.cs
rename to src/API/Grand.Api/Queries/Handlers/Common/GetGenericQueryHandler.cs
index 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.cs
new file mode 100644
index 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; }
+ }
+}