using Distribution.Infra.UoW; namespace Distribution.Features.Vehicle; public class VehicleService(IVehicleRepository repo, IUnitOfWork uow) { public async Task> GetAllAsync() => (await repo.GetAllAsync()) .Where(v => v.IsActive) .Select(v => new VehicleResponse(v.Id, v.RegNumber, v.NumberPlate, v.Type, v.IsActive, v.CreatedAt)) .ToList(); public async Task GetByIdAsync(Guid id) { var vehicle = await repo.GetByIdAsync(id); if (vehicle is null || !vehicle.IsActive) return null; return new VehicleResponse(vehicle.Id, vehicle.RegNumber, vehicle.NumberPlate, vehicle.Type, vehicle.IsActive, vehicle.CreatedAt); } public async Task CreateAsync(CreateVehicleRequest request, CancellationToken ct = default) { var vehicle = new Models.Vehicle { RegNumber = request.RegNumber, NumberPlate = request.NumberPlate, Type = request.Type }; await uow.BeginAsync(ct); try { await repo.AddAsync(vehicle); await uow.CommitAsync(ct); } catch { await uow.RollbackAsync(ct); throw; } return new VehicleResponse(vehicle.Id, vehicle.RegNumber, vehicle.NumberPlate, vehicle.Type, vehicle.IsActive, vehicle.CreatedAt); } public async Task UpdateAsync(Guid id, UpdateVehicleRequest request, CancellationToken ct = default) { var vehicle = await repo.GetByIdAsync(id); if (vehicle is null || !vehicle.IsActive) return null; vehicle.RegNumber = request.RegNumber; vehicle.NumberPlate = request.NumberPlate; vehicle.Type = request.Type; await uow.BeginAsync(ct); try { repo.Update(vehicle); await uow.CommitAsync(ct); } catch { await uow.RollbackAsync(ct); throw; } return new VehicleResponse(vehicle.Id, vehicle.RegNumber, vehicle.NumberPlate, vehicle.Type, vehicle.IsActive, vehicle.CreatedAt); } public async Task DeleteAsync(Guid id, CancellationToken ct = default) { var vehicle = await repo.GetByIdAsync(id); if (vehicle is null || !vehicle.IsActive) return false; await uow.BeginAsync(ct); try { repo.Remove(vehicle); await uow.CommitAsync(ct); } catch { await uow.RollbackAsync(ct); throw; } return true; } }