using Distribution.Infra.UoW; namespace Distribution.Features.Route; public class RouteService(IRouteRepository repo, IUnitOfWork uow) { public async Task> GetAllAsync() => (await repo.GetAllAsync()) .Where(r => r.IsActive) .Select(r => new RouteResponse(r.Id, r.RouteName, r.StartPoint, r.EndPoint, r.Length, r.IsActive, r.CreatedAt)) .ToList(); public async Task GetByIdAsync(Guid id) { var route = await repo.GetByIdAsync(id); if (route is null || !route.IsActive) return null; return new RouteResponse(route.Id, route.RouteName, route.StartPoint, route.EndPoint, route.Length, route.IsActive, route.CreatedAt); } public async Task CreateAsync(CreateRouteRequest request, CancellationToken ct = default) { var route = new Models.Route { RouteName = request.RouteName, StartPoint = request.StartPoint, EndPoint = request.EndPoint, Length = request.Length }; await uow.BeginAsync(ct); try { await repo.AddAsync(route); await uow.CommitAsync(ct); } catch { await uow.RollbackAsync(ct); throw; } return new RouteResponse(route.Id, route.RouteName, route.StartPoint, route.EndPoint, route.Length, route.IsActive, route.CreatedAt); } public async Task UpdateAsync(Guid id, UpdateRouteRequest request, CancellationToken ct = default) { var route = await repo.GetByIdAsync(id); if (route is null || !route.IsActive) return null; route.RouteName = request.RouteName; route.StartPoint = request.StartPoint; route.EndPoint = request.EndPoint; route.Length = request.Length; await uow.BeginAsync(ct); try { repo.Update(route); await uow.CommitAsync(ct); } catch { await uow.RollbackAsync(ct); throw; } return new RouteResponse(route.Id, route.RouteName, route.StartPoint, route.EndPoint, route.Length, route.IsActive, route.CreatedAt); } public async Task DeleteAsync(Guid id, CancellationToken ct = default) { var route = await repo.GetByIdAsync(id); if (route is null || !route.IsActive) return false; await uow.BeginAsync(ct); try { repo.Remove(route); await uow.CommitAsync(ct); } catch { await uow.RollbackAsync(ct); throw; } return true; } }