This repository has been archived by the owner on Jul 12, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5fca54a
commit de0f2ac
Showing
3 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
src/RookieShop.ApiService/Endpoints/Products/Update.Request.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,15 @@ | ||
using RookieShop.Domain.Entities.CategoryAggregator.Primitives; | ||
using RookieShop.Domain.Entities.ProductAggregator.Primitives; | ||
|
||
namespace RookieShop.ApiService.Endpoints.Products; | ||
|
||
public sealed record UpdateProductRequest( | ||
ProductId Id, | ||
string Name, | ||
string? Description, | ||
int Quantity, | ||
decimal Price, | ||
decimal PriceSale, | ||
IFormFile? ProductImages, | ||
bool IsDeletedOldImage, | ||
CategoryId? CategoryId = null); |
5 changes: 5 additions & 0 deletions
5
src/RookieShop.ApiService/Endpoints/Products/Update.Response.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 RookieShop.ApiService.ViewModels.Products; | ||
|
||
namespace RookieShop.ApiService.Endpoints.Products; | ||
|
||
public sealed record UpdateProductResponse(ProductVm Product); |
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,55 @@ | ||
using MediatR; | ||
using Microsoft.AspNetCore.Http.HttpResults; | ||
using Microsoft.AspNetCore.Mvc; | ||
using RookieShop.ApiService.ViewModels.Products; | ||
using RookieShop.Application.Products.Commands.Update; | ||
using RookieShop.Domain.Entities.CategoryAggregator.Primitives; | ||
using RookieShop.Domain.Entities.ProductAggregator.Primitives; | ||
using RookieShop.Infrastructure.Endpoints.Abstractions; | ||
using RookieShop.Infrastructure.RateLimiter; | ||
|
||
namespace RookieShop.ApiService.Endpoints.Products; | ||
|
||
public sealed class Update(ISender sender) : IEndpoint<Ok<UpdateProductResponse>, UpdateProductRequest> | ||
{ | ||
public void MapEndpoint(IEndpointRouteBuilder app) => | ||
app.MapPut("/products", | ||
async ( | ||
[FromForm] ProductId id, | ||
[FromForm] string name, | ||
[FromForm] string? description, | ||
[FromForm] int quantity, | ||
[FromForm] decimal price, | ||
[FromForm] decimal priceSale, | ||
[FromForm] IFormFile? productImages, | ||
[FromForm] bool isDeletedOldImage = false, | ||
[FromForm] CategoryId? categoryId = null) | ||
=> await HandleAsync(new(id, name, description, quantity, price, priceSale, productImages, | ||
isDeletedOldImage, categoryId))) | ||
.Produces<UpdateProductResponse>() | ||
.WithTags(nameof(Products)) | ||
.WithName("Update Product") | ||
.MapToApiVersion(new(1, 0)) | ||
.RequirePerUserRateLimit(); | ||
|
||
public async Task<Ok<UpdateProductResponse>> HandleAsync(UpdateProductRequest request, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
UpdateProductCommand command = new( | ||
request.Id, | ||
request.Name, | ||
request.Description, | ||
request.Quantity, | ||
request.Price, | ||
request.PriceSale, | ||
request.ProductImages, | ||
request.IsDeletedOldImage, | ||
request.CategoryId); | ||
|
||
var result = await sender.Send(command, cancellationToken); | ||
|
||
UpdateProductResponse response = new(result.Value.ToProductVm()); | ||
|
||
return TypedResults.Ok(response); | ||
} | ||
} |