| | | 1 | | using MechanicsSoftware.Application.Abstractions; |
| | | 2 | | using MechanicsSoftware.Application.Exceptions; |
| | | 3 | | using MechanicsSoftware.Application.UseCases.ServiceOrders.Commands; |
| | | 4 | | using MechanicsSoftware.Domain.Entities; |
| | | 5 | | |
| | | 6 | | namespace MechanicsSoftware.Application.UseCases.ServiceOrders.Handlers; |
| | | 7 | | |
| | 6 | 8 | | public sealed class CreateServiceOrderHandler(IAppDbContext db) |
| | | 9 | | { |
| | | 10 | | public async Task<ServiceOrderResponse> ExecuteAsync( |
| | | 11 | | CreateServiceOrderCommand command, CancellationToken cancellationToken = default) |
| | 6 | 12 | | { |
| | 6 | 13 | | _ = await db.Customers.FindAsync([command.CustomerId], cancellationToken) |
| | 6 | 14 | | ?? throw new NotFoundException(nameof(Customer), command.CustomerId); |
| | | 15 | | |
| | 4 | 16 | | _ = await db.Vehicles.FindAsync([command.VehicleId], cancellationToken) |
| | 4 | 17 | | ?? throw new NotFoundException(nameof(Vehicle), command.VehicleId); |
| | | 18 | | |
| | 2 | 19 | | var order = ServiceOrder.Create(Guid.NewGuid(), command.CustomerId, command.VehicleId); |
| | | 20 | | |
| | 2 | 21 | | db.ServiceOrders.Add(order); |
| | 2 | 22 | | await db.SaveChangesAsync(cancellationToken); |
| | | 23 | | |
| | 2 | 24 | | return ServiceOrderResponse.From(order); |
| | 2 | 25 | | } |
| | | 26 | | } |