| | | 1 | | using ClutterStock.Contracts.Items; |
| | | 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.Items.UpdateItem; |
| | | 9 | | |
| | | 10 | | public interface IUpdateItemCommandHandler : ICommandHandler |
| | | 11 | | { |
| | | 12 | | Task<Results<Ok<ItemResponse>, NotFound>> HandleAsync(Command command, CancellationToken cancellationToken = default |
| | | 13 | | |
| | | 14 | | record Command(int Id, int RoomId, string Name, string? Description, string? Category, string? Notes); |
| | | 15 | | } |
| | | 16 | | |
| | 2 | 17 | | public class UpdateItemCommandHandler(IAppDbContext context) : IUpdateItemCommandHandler |
| | | 18 | | { |
| | | 19 | | public async Task<Results<Ok<ItemResponse>, NotFound>> HandleAsync(IUpdateItemCommandHandler.Command command, Cancel |
| | | 20 | | { |
| | 2 | 21 | | var item = await context.Items.FirstOrDefaultAsync(i => i.Id == command.Id, cancellationToken); |
| | 2 | 22 | | if (item is null) |
| | 1 | 23 | | return TypedResults.NotFound(); |
| | | 24 | | |
| | 1 | 25 | | item.RoomId = command.RoomId; |
| | 1 | 26 | | item.Name = command.Name; |
| | 1 | 27 | | item.Description = command.Description; |
| | 1 | 28 | | item.Category = command.Category; |
| | 1 | 29 | | item.Notes = command.Notes; |
| | 1 | 30 | | item.UpdatedAtUtc = DateTimeOffset.UtcNow; |
| | | 31 | | |
| | 1 | 32 | | await context.SaveChangesAsync(cancellationToken); |
| | 1 | 33 | | return TypedResults.Ok(item.ToResponse()); |
| | 2 | 34 | | } |
| | | 35 | | } |