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