| | | 1 | | using ClutterStock.Contracts.Locations; |
| | | 2 | | using ClutterStock.Domain.Abstractions; |
| | | 3 | | using ClutterStock.Domain.Extensions; |
| | | 4 | | using Microsoft.AspNetCore.Http; |
| | | 5 | | using Microsoft.AspNetCore.Http.HttpResults; |
| | | 6 | | using Microsoft.EntityFrameworkCore; |
| | | 7 | | |
| | | 8 | | namespace ClutterStock.Domain.Features.Locations.UpdateLocation; |
| | | 9 | | |
| | | 10 | | public interface IUpdateLocationCommandHandler : ICommandHandler |
| | | 11 | | { |
| | | 12 | | Task<Results<Ok<LocationResponse>, NotFound>> HandleAsync(Command command, CancellationToken cancellationToken = def |
| | | 13 | | |
| | | 14 | | record Command(int Id, string Name, string? Description); |
| | | 15 | | } |
| | | 16 | | |
| | 2 | 17 | | public class UpdateLocationCommandHandler(IAppDbContext context) : IUpdateLocationCommandHandler |
| | | 18 | | { |
| | | 19 | | public async Task<Results<Ok<LocationResponse>, NotFound>> HandleAsync(IUpdateLocationCommandHandler.Command command |
| | | 20 | | { |
| | 2 | 21 | | var location = await context.Locations.FirstOrDefaultAsync(l => l.Id == command.Id, cancellationToken); |
| | 2 | 22 | | if (location is null) |
| | 1 | 23 | | return TypedResults.NotFound(); |
| | | 24 | | |
| | 1 | 25 | | location.Name = command.Name; |
| | 1 | 26 | | location.Description = command.Description; |
| | 1 | 27 | | location.UpdatedAtUtc = DateTimeOffset.UtcNow; |
| | | 28 | | |
| | 1 | 29 | | await context.SaveChangesAsync(cancellationToken); |
| | 1 | 30 | | return TypedResults.Ok(location.ToResponse()); |
| | 2 | 31 | | } |
| | | 32 | | } |