Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Commit

Permalink
feat(product): add update endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
foxminchan committed May 15, 2024
1 parent 5fca54a commit de0f2ac
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/RookieShop.ApiService/Endpoints/Products/Update.Request.cs
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);
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);
55 changes: 55 additions & 0 deletions src/RookieShop.ApiService/Endpoints/Products/Update.cs
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);
}
}

0 comments on commit de0f2ac

Please sign in to comment.