| | 1 | | using Bookings.Domain; |
| | 2 | | using Bookings.Infrastructure.RequestHandling; |
| | 3 | |
|
| | 4 | | namespace Bookings.Api.Bookings |
| | 5 | | { |
| | 6 | | public static class BookingHandlers |
| | 7 | | { |
| | 8 | | public static Task<IResult> Create(BookingRequest request, IBookingsRepository repository) => |
| 18 | 9 | | RequestHandler.Execute(() => |
| 18 | 10 | | { |
| 18 | 11 | | var booking = repository.Create(new Booking( |
| 18 | 12 | | request.Description, |
| 18 | 13 | | request.Type, |
| 18 | 14 | | new DateRange(request.Start, request.End), |
| 18 | 15 | | request.Email)); |
| 18 | 16 | |
|
| 18 | 17 | | if (booking.Value is null) |
| 0 | 18 | | return null!; |
| 18 | 19 | |
|
| 18 | 20 | | return Results.Created($"/bookings/", new BookingResult( |
| 18 | 21 | | booking.Key, booking.Value.Description, booking.Value.Type, booking.Value.Period, booking.Value.Email)); |
| 24 | 22 | | }, "Create", request.ToString()); |
| 24 | 23 | |
|
| | 24 | | public static Task<IResult> Update(Guid id, BookingRequest request, IBookingsRepository repository) => |
| 2 | 25 | | RequestHandler.Execute(() => |
| 6 | 26 | | { |
| 6 | 27 | | if (id == Guid.Empty) |
| 4 | 28 | | throw new ArgumentException("Invalid id"); |
| 2 | 29 | |
|
| 6 | 30 | | var updated = repository.Update(id, new Booking( |
| 6 | 31 | | request.Description, |
| 6 | 32 | | request.Type, |
| 6 | 33 | | new DateRange(request.Start, request.End), |
| 6 | 34 | | request.Email)); |
| 6 | 35 | |
|
| 6 | 36 | | if (updated.Value is null) |
| 4 | 37 | | return Results.NoContent(); |
| 2 | 38 | |
|
| 6 | 39 | | return Results.Ok(new BookingResult( |
| 6 | 40 | | updated.Key, updated.Value.Description, updated.Value.Type, updated.Value.Period, updated.Value.Emai |
| 8 | 41 | | }, "Update", id.ToString()); |
| 4 | 42 | |
|
| 8 | 43 | | public static Task<IResult> Delete(Guid id, IBookingsRepository repository) => |
| 2 | 44 | | RequestHandler.Execute(() => |
| 2 | 45 | | { |
| 6 | 46 | | if (id == Guid.Empty) |
| 4 | 47 | | throw new ArgumentException($"Invalid id {id}"); |
| 6 | 48 | |
|
| 2 | 49 | | var deleted = repository.Delete(id); |
| 6 | 50 | |
|
| 6 | 51 | | return deleted.Value is null ? Results.NoContent() : Results.Ok(new BookingResult( |
| 6 | 52 | | deleted.Key, deleted.Value.Description, deleted.Value.Type, deleted.Value.Period, deleted.Value.Emai |
| 8 | 53 | | }, "Delete", id.ToString()); |
| 4 | 54 | |
|
| 8 | 55 | | public static Task<IResult> Get(IBookingsRepository repository) => |
| 0 | 56 | | RequestHandler.Execute(() => |
| 0 | 57 | | { |
| 0 | 58 | | return Results.Ok(repository.GetAll()); |
| 0 | 59 | | }, "Get", "All"); |
| 0 | 60 | | } |
| 0 | 61 | | } |