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
65b61dc
commit 64b0eb7
Showing
16 changed files
with
169 additions
and
8 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
src/RookieShop.ApiService/Endpoints/Baskets/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,5 @@ | ||
using RookieShop.Domain.Entities.ProductAggregator.Primitives; | ||
|
||
namespace RookieShop.ApiService.Endpoints.Baskets; | ||
|
||
public sealed record UpdateBasketRequest(Guid AccountId, ProductId ProductId, int Quantity); |
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,38 @@ | ||
using Ardalis.Result; | ||
using MediatR; | ||
using Microsoft.AspNetCore.Http.HttpResults; | ||
using Microsoft.AspNetCore.Mvc; | ||
using RookieShop.ApiService.Filters; | ||
using RookieShop.ApiService.ViewModels.Baskets; | ||
using RookieShop.Application.Baskets.Commands.Update; | ||
using RookieShop.Domain.Constants; | ||
using RookieShop.Infrastructure.Endpoints.Abstractions; | ||
using RookieShop.Infrastructure.RateLimiter; | ||
|
||
namespace RookieShop.ApiService.Endpoints.Baskets; | ||
|
||
public sealed class Update(ISender sender) : IEndpoint<Ok<BasketVm>, UpdateBasketRequest> | ||
{ | ||
public void MapEndpoint(IEndpointRouteBuilder app) => | ||
app.MapPatch("/baskets", | ||
async ([FromHeader(Name = HeaderName.IdempotencyKey)] string key, UpdateBasketRequest basket) => | ||
await HandleAsync(basket)) | ||
.AddEndpointFilter<IdempotencyFilter>() | ||
.Produces<Ok<BasketVm>>() | ||
.Produces<NotFound<string>>(StatusCodes.Status404NotFound) | ||
.Produces<BadRequest<IEnumerable<ValidationError>>>(StatusCodes.Status400BadRequest) | ||
.WithTags(nameof(Baskets)) | ||
.WithName("Update Basket Quantity") | ||
.MapToApiVersion(new(1, 0)) | ||
.RequirePerUserRateLimit(); | ||
|
||
public async Task<Ok<BasketVm>> HandleAsync(UpdateBasketRequest request, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
UpdateBasketCommand command = new(request.AccountId, request.ProductId, request.Quantity); | ||
|
||
var result = await sender.Send(command, cancellationToken); | ||
|
||
return TypedResults.Ok(result.Value.ToBasketVm()); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/RookieShop.Application/Baskets/Commands/Update/UpdateBasketCommand.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,9 @@ | ||
using Ardalis.Result; | ||
using RookieShop.Application.Baskets.DTOs; | ||
using RookieShop.Domain.Entities.ProductAggregator.Primitives; | ||
using RookieShop.Domain.SharedKernel; | ||
|
||
namespace RookieShop.Application.Baskets.Commands.Update; | ||
|
||
public sealed record UpdateBasketCommand(Guid AccountId, ProductId ProductId, int Quantity) | ||
: ICommand<Result<BasketDto>>; |
32 changes: 32 additions & 0 deletions
32
src/RookieShop.Application/Baskets/Commands/Update/UpdateBasketHandler.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,32 @@ | ||
using System.Text.Json; | ||
using Ardalis.GuardClauses; | ||
using Ardalis.Result; | ||
using Microsoft.Extensions.Logging; | ||
using RookieShop.Application.Baskets.DTOs; | ||
using RookieShop.Domain.Entities.BasketAggregator; | ||
using RookieShop.Domain.SharedKernel; | ||
using RookieShop.Infrastructure.Cache.Redis; | ||
|
||
namespace RookieShop.Application.Baskets.Commands.Update; | ||
|
||
public sealed class UpdateBasketHandler(IRedisService redisService, ILogger<UpdateBasketHandler> logger) | ||
: ICommandHandler<UpdateBasketCommand, Result<BasketDto>> | ||
{ | ||
public async Task<Result<BasketDto>> Handle(UpdateBasketCommand request, CancellationToken cancellationToken) | ||
{ | ||
var basket = await redisService.HashGetAsync<Basket>(nameof(Basket), request.AccountId.ToString()); | ||
|
||
Guard.Against.NotFound(request.AccountId, basket); | ||
|
||
var basketDetail = basket.BasketDetails.First(x => x.Id == request.ProductId); | ||
|
||
basketDetail.Quantity = request.Quantity; | ||
|
||
logger.LogInformation("[{Command}] - Updating basket for account {AccountId} with {@Basket}", | ||
nameof(UpdateBasketCommand), request.AccountId, JsonSerializer.Serialize(basket)); | ||
|
||
var result = await redisService.HashSetAsync(nameof(Basket), request.AccountId.ToString(), basket); | ||
|
||
return result.ToBasketDto(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/RookieShop.Application/Baskets/Commands/Update/UpdateBasketValidator.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,18 @@ | ||
using FluentValidation; | ||
|
||
namespace RookieShop.Application.Baskets.Commands.Update; | ||
|
||
public sealed class UpdateBasketValidator : AbstractValidator<UpdateBasketCommand> | ||
{ | ||
public UpdateBasketValidator() | ||
{ | ||
RuleFor(x => x.AccountId) | ||
.NotEmpty(); | ||
|
||
RuleFor(x => x.ProductId) | ||
.NotEmpty(); | ||
|
||
RuleFor(x => x.Quantity) | ||
.GreaterThan(0); | ||
} | ||
} |
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
21 changes: 21 additions & 0 deletions
21
ui/storefront/Areas/Basket/Models/UpdateBasketQuantityRequest.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,21 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Text.Json.Serialization; | ||
using Refit; | ||
|
||
namespace RookieShop.Storefront.Areas.Basket.Models; | ||
|
||
public sealed class UpdateBasketQuantityRequest | ||
{ | ||
[AliasAs("accountId")] | ||
public Guid? AccountId { get; set; } | ||
|
||
[AliasAs("productId")] | ||
[Required(ErrorMessage = "Product Id is required")] | ||
[JsonRequired] | ||
public Guid ProductId { get; set; } | ||
|
||
[AliasAs("quantity")] | ||
[Required(ErrorMessage = "Quantity is required")] | ||
[Range(1, int.MaxValue, ErrorMessage = "Quantity must be greater than 0")] | ||
public int Quantity { get; set; } | ||
} |
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
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
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