| | | 1 | | using MechanicsSoftware.Application.Abstractions; |
| | | 2 | | using MechanicsSoftware.Application.Exceptions; |
| | | 3 | | using MechanicsSoftware.Application.UseCases.Vehicles.Commands; |
| | | 4 | | using MechanicsSoftware.Domain.Entities; |
| | | 5 | | using Microsoft.EntityFrameworkCore; |
| | | 6 | | |
| | | 7 | | namespace MechanicsSoftware.Application.UseCases.Vehicles.Handlers; |
| | | 8 | | |
| | 4 | 9 | | public sealed class UpdateVehicleHandler(IAppDbContext db) |
| | | 10 | | { |
| | | 11 | | public async Task<VehicleResponse> ExecuteAsync( |
| | | 12 | | UpdateVehicleCommand command, |
| | | 13 | | CancellationToken cancellationToken = default) |
| | 4 | 14 | | { |
| | 4 | 15 | | var vehicle = await db.Vehicles |
| | 4 | 16 | | .FirstOrDefaultAsync(v => v.Id == command.Id, cancellationToken) |
| | 4 | 17 | | ?? throw new NotFoundException(nameof(Vehicle), command.Id); |
| | | 18 | | |
| | 2 | 19 | | vehicle.Update(command.Make, command.Model, command.Year); |
| | 2 | 20 | | await db.SaveChangesAsync(cancellationToken); |
| | | 21 | | |
| | 2 | 22 | | return VehicleResponse.From(vehicle); |
| | 2 | 23 | | } |
| | | 24 | | } |