-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added product getbyid function (#933)
* added product getbyid * renamed exception class * removed handler for query * Revert "removed handler for query" This reverts commit 0fa4df0. * removed product read handler * reverting proj and settings file * Added product get list method * added update endpoint for product * fixes to product update * fix update mechanism * Added delete endpoint for product * changed response of delete to 204 and made endpoints async * updated proj file * Update launchSettings.json * Update Server.csproj --------- Co-authored-by: Vipul Malhotra <vipulmalhotra@192.168.1.3> Co-authored-by: Mukesh Murugan <31455818+iammukeshm@users.noreply.github.com> Co-authored-by: Mukesh Murugan <iammukeshm@gmail.com>
- Loading branch information
1 parent
93e3787
commit 9051e95
Showing
23 changed files
with
305 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
api/modules/Catalog/Catalog.Application/Products/Delete/v1/DeleteProductCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
using MediatR; | ||
|
||
namespace FSH.WebApi.Catalog.Application.Products.Delete.v1; | ||
public sealed record DeleteProductCommand( | ||
Guid Id) : IRequest; |
22 changes: 22 additions & 0 deletions
22
api/modules/Catalog/Catalog.Application/Products/Delete/v1/DeleteProductHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using FSH.Framework.Core.Persistence; | ||
using FSH.WebApi.Catalog.Domain; | ||
using FSH.WebApi.Catalog.Domain.Exceptions; | ||
using MediatR; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace FSH.WebApi.Catalog.Application.Products.Delete.v1; | ||
public sealed class DeleteProductHandler( | ||
ILogger<DeleteProductHandler> logger, | ||
[FromKeyedServices("catalog:products")] IRepository<Product> repository) | ||
: IRequestHandler<DeleteProductCommand> | ||
{ | ||
public async Task Handle(DeleteProductCommand request, CancellationToken cancellationToken) | ||
{ | ||
ArgumentNullException.ThrowIfNull(request); | ||
var product = await repository.GetByIdAsync(request.Id, cancellationToken); | ||
_ = product ?? throw new ProductNotFoundException(request.Id); | ||
await repository.DeleteAsync(product, cancellationToken); | ||
logger.LogInformation("product with id : {ProductId} deleted", product.Id); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
api/modules/Catalog/Catalog.Application/Products/Get/v1/GetProductHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using FSH.WebApi.Catalog.Domain.Exceptions; | ||
using FSH.Framework.Core.Persistence; | ||
using FSH.Framework.Core.Caching; | ||
using FSH.WebApi.Catalog.Domain; | ||
using MediatR; | ||
|
||
namespace FSH.WebApi.Catalog.Application.Products.Get.v1; | ||
public sealed class GetProductHandler( | ||
[FromKeyedServices("catalog:products")] IReadRepository<Product> repository, | ||
ICacheService cache) | ||
: IRequestHandler<GetProductRequest, GetProductResponse> | ||
{ | ||
public async Task<GetProductResponse> Handle(GetProductRequest request, CancellationToken cancellationToken) | ||
{ | ||
ArgumentNullException.ThrowIfNull(request); | ||
var item = await cache.GetOrSetAsync( | ||
$"product:{request.Id}", | ||
async () => | ||
{ | ||
var productItem = await repository.GetByIdAsync(request.Id, cancellationToken); | ||
if (productItem == null) throw new ProductNotFoundException(request.Id); | ||
return new GetProductResponse(productItem.Id, productItem.Name, productItem.Description, productItem.Price); | ||
}, | ||
cancellationToken: cancellationToken); | ||
return item!; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
api/modules/Catalog/Catalog.Application/Products/Get/v1/GetProductRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using MediatR; | ||
|
||
namespace FSH.WebApi.Catalog.Application.Products.Get.v1; | ||
public class GetProductRequest : IRequest<GetProductResponse> | ||
{ | ||
public Guid Id { get; set; } | ||
public GetProductRequest(Guid id) => Id = id; | ||
} |
2 changes: 2 additions & 0 deletions
2
api/modules/Catalog/Catalog.Application/Products/Get/v1/GetProductResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
namespace FSH.WebApi.Catalog.Application.Products.Get.v1; | ||
public sealed record GetProductResponse(Guid? Id, string Name, string? Description, decimal Price); |
22 changes: 22 additions & 0 deletions
22
api/modules/Catalog/Catalog.Application/Products/GetList/v1/GetProductListHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using FSH.WebApi.Catalog.Application.Products.Get.v1; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using FSH.Framework.Core.Specifications; | ||
using FSH.Framework.Core.Persistence; | ||
using FSH.WebApi.Catalog.Domain; | ||
using FSH.Framework.Core.Paging; | ||
using MediatR; | ||
|
||
|
||
namespace FSH.WebApi.Catalog.Application.Products.GetList.v1; | ||
public sealed class GetProductListHandler( | ||
[FromKeyedServices("catalog:products")] IReadRepository<Product> repository) | ||
: IRequestHandler<GetProductListRequest, PagedList<GetProductResponse>> | ||
{ | ||
public async Task<PagedList<GetProductResponse>> Handle(GetProductListRequest request, CancellationToken cancellationToken) | ||
{ | ||
ArgumentNullException.ThrowIfNull(request); | ||
var spec = new ListSpecification<Product, GetProductResponse>(request.PageNumber, request.PageSize); | ||
var items = await repository.PaginatedListAsync(spec, request.PageNumber, request.PageSize, cancellationToken).ConfigureAwait(false); | ||
return items; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
api/modules/Catalog/Catalog.Application/Products/GetList/v1/GetProductListRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
using FSH.WebApi.Catalog.Application.Products.Get.v1; | ||
using FSH.Framework.Core.Paging; | ||
using MediatR; | ||
|
||
namespace FSH.WebApi.Catalog.Application.Products.GetList.v1; | ||
|
||
public record GetProductListRequest(int PageNumber, int PageSize) : IRequest<PagedList<GetProductResponse>>; |
8 changes: 8 additions & 0 deletions
8
api/modules/Catalog/Catalog.Application/Products/Update/v1/UpdateProductCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using MediatR; | ||
|
||
namespace FSH.WebApi.Catalog.Application.Products.Update.v1; | ||
public sealed record UpdateProductCommand( | ||
Guid Id, | ||
string? Name, | ||
decimal Price, | ||
string? Description = null) : IRequest<UpdateProductResponse>; |
11 changes: 11 additions & 0 deletions
11
api/modules/Catalog/Catalog.Application/Products/Update/v1/UpdateProductCommandValidator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using FluentValidation; | ||
|
||
namespace FSH.WebApi.Catalog.Application.Products.Update.v1; | ||
public class UpdateProductCommandValidator : AbstractValidator<UpdateProductCommand> | ||
{ | ||
public UpdateProductCommandValidator() | ||
{ | ||
RuleFor(p => p.Name).NotEmpty().MinimumLength(2).MaximumLength(75); | ||
RuleFor(p => p.Price).GreaterThan(0); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
api/modules/Catalog/Catalog.Application/Products/Update/v1/UpdateProductHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using FSH.Framework.Core.Persistence; | ||
using FSH.WebApi.Catalog.Domain; | ||
using FSH.WebApi.Catalog.Domain.Exceptions; | ||
using MediatR; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace FSH.WebApi.Catalog.Application.Products.Update.v1; | ||
public sealed class UpdateProductHandler( | ||
ILogger<UpdateProductHandler> logger, | ||
[FromKeyedServices("catalog:products")] IRepository<Product> repository) | ||
: IRequestHandler<UpdateProductCommand, UpdateProductResponse> | ||
{ | ||
public async Task<UpdateProductResponse> Handle(UpdateProductCommand request, CancellationToken cancellationToken) | ||
{ | ||
ArgumentNullException.ThrowIfNull(request); | ||
var product = await repository.GetByIdAsync(request.Id, cancellationToken); | ||
_ = product ?? throw new ProductNotFoundException(request.Id); | ||
var updatedProduct = product.Update(request.Name, request.Description, request.Price); | ||
await repository.UpdateAsync(updatedProduct, cancellationToken); | ||
logger.LogInformation("product with id : {ProductId} updated.", product.Id); | ||
return new UpdateProductResponse(product.Id); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
api/modules/Catalog/Catalog.Application/Products/Update/v1/UpdateProductResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
namespace FSH.WebApi.Catalog.Application.Products.Update.v1; | ||
public sealed record UpdateProductResponse(Guid? Id); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
using FSH.Framework.Core.Domain.Events; | ||
|
||
namespace FSH.WebApi.Catalog.Domain.Events; | ||
public sealed record ProductUpdated : DomainEvent | ||
{ | ||
public Product? Product { get; set; } | ||
} |
10 changes: 10 additions & 0 deletions
10
api/modules/Catalog/Catalog.Domain/Exceptions/ProductNotFoundException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using FSH.Framework.Core.Exceptions; | ||
|
||
namespace FSH.WebApi.Catalog.Domain.Exceptions; | ||
public sealed class ProductNotFoundException : NotFoundException | ||
{ | ||
public ProductNotFoundException(Guid id) | ||
: base($"product with id {id} not found") | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
api/modules/Catalog/Catalog.Infrastructure/Endpoints/v1/DeleteProductEndpoint.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using FSH.Framework.Infrastructure.Auth.Policy; | ||
using FSH.WebApi.Catalog.Application.Products.Delete.v1; | ||
using MediatR; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Routing; | ||
|
||
namespace FSH.WebApi.Catalog.Infrastructure.Endpoints.v1; | ||
public static class DeleteProductEndpoint | ||
{ | ||
internal static RouteHandlerBuilder MapProductDeleteEndpoint(this IEndpointRouteBuilder endpoints) | ||
{ | ||
return endpoints | ||
.MapDelete("/{id:guid}", async (Guid id, ISender mediator) => | ||
{ | ||
await mediator.Send(new DeleteProductCommand(id)); | ||
return Results.NoContent(); | ||
}) | ||
.WithName(nameof(DeleteProductEndpoint)) | ||
.WithSummary("deletes product by id") | ||
.WithDescription("deletes product by id") | ||
.Produces(StatusCodes.Status204NoContent) | ||
.RequirePermission("Permissions.Products.Delete") | ||
.MapToApiVersion(1); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
api/modules/Catalog/Catalog.Infrastructure/Endpoints/v1/GetProductEndpoint.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using FSH.Framework.Infrastructure.Auth.Policy; | ||
using FSH.WebApi.Catalog.Application.Products.Get.v1; | ||
using MediatR; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Routing; | ||
|
||
namespace FSH.WebApi.Catalog.Infrastructure.Endpoints.v1; | ||
public static class GetProductEndpoint | ||
{ | ||
internal static RouteHandlerBuilder MapGetProductEndpoint(this IEndpointRouteBuilder endpoints) | ||
{ | ||
return endpoints | ||
.MapGet("/{id:guid}", async (Guid id, ISender mediator) => | ||
{ | ||
var response = await mediator.Send(new GetProductRequest(id)); | ||
return Results.Ok(response); | ||
}) | ||
.WithName(nameof(GetProductEndpoint)) | ||
.WithSummary("gets product by id") | ||
.WithDescription("gets prodct by id") | ||
.Produces<GetProductResponse>() | ||
.RequirePermission("Permissions.Products.View") | ||
.MapToApiVersion(1); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
api/modules/Catalog/Catalog.Infrastructure/Endpoints/v1/GetProductListEndpoint.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using FSH.Framework.Core.Paging; | ||
using FSH.Framework.Infrastructure.Auth.Policy; | ||
using FSH.WebApi.Catalog.Application.Products.Get.v1; | ||
using FSH.WebApi.Catalog.Application.Products.GetList.v1; | ||
using MediatR; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Routing; | ||
|
||
namespace FSH.WebApi.Catalog.Infrastructure.Endpoints.v1; | ||
public static class GetProductListEndpoint | ||
{ | ||
internal static RouteHandlerBuilder MapGetProductListEndpoint(this IEndpointRouteBuilder endpoints) | ||
{ | ||
return endpoints | ||
.MapGet("/", async (ISender mediator, int pageNumber = 1, int pageSize = 10) => | ||
{ | ||
var response = await mediator.Send(new GetProductListRequest(pageNumber, pageSize)); | ||
return Results.Ok(response); | ||
}) | ||
.WithName(nameof(GetProductListEndpoint)) | ||
.WithSummary("gets a list of products") | ||
.WithDescription("gets a list of products") | ||
.Produces<PagedList<GetProductResponse>>() | ||
.RequirePermission("Permissions.Products.View") | ||
.MapToApiVersion(1); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
api/modules/Catalog/Catalog.Infrastructure/Endpoints/v1/UpdateProductEndpoint.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using FSH.Framework.Infrastructure.Auth.Policy; | ||
using FSH.WebApi.Catalog.Application.Products.Update.v1; | ||
using MediatR; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Routing; | ||
|
||
namespace FSH.WebApi.Catalog.Infrastructure.Endpoints.v1; | ||
public static class UpdateProductEndpoint | ||
{ | ||
internal static RouteHandlerBuilder MapProductUpdateEndpoint(this IEndpointRouteBuilder endpoints) | ||
{ | ||
return endpoints | ||
.MapPut("/{id:guid}", async (Guid id, UpdateProductCommand request, ISender mediator) => | ||
{ | ||
if (id != request.Id) return Results.BadRequest(); | ||
var response = await mediator.Send(request); | ||
return Results.Ok(response); | ||
}) | ||
.WithName(nameof(UpdateProductEndpoint)) | ||
.WithSummary("update a product") | ||
.WithDescription("update a product") | ||
.Produces<UpdateProductResponse>() | ||
.RequirePermission("Permissions.Products.Update") | ||
.MapToApiVersion(1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,4 +12,4 @@ | |
} | ||
}, | ||
"$schema": "http://json.schemastore.org/launchsettings.json" | ||
} | ||
} |