From bfbfe352ee6720e2820e7a3ba217ceca3dc61688 Mon Sep 17 00:00:00 2001 From: DeepnaPooja Date: Mon, 31 Aug 2026 11:11:15 +0530 Subject: [PATCH] Add dedicated endpoint to assign a route to a region PUT /api/routes/{id}/region assigns/reassigns an existing route to a region directly, validating the target region exists and is active. Stands in for the blocked assignAreaToRegion until an Area entity exists between Region and Route. Co-Authored-By: Claude Sonnet 5 --- .../Features/Route/RouteController.cs | 7 ++++++ Distribution/Features/Route/RouteDTOs.cs | 2 ++ Distribution/Features/Route/RouteService.cs | 25 ++++++++++++++++++- 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/Distribution/Features/Route/RouteController.cs b/Distribution/Features/Route/RouteController.cs index 2813e29..f4ed704 100644 --- a/Distribution/Features/Route/RouteController.cs +++ b/Distribution/Features/Route/RouteController.cs @@ -31,6 +31,13 @@ public class RouteController(RouteService service) : ControllerBase return result is null ? NotFound() : Ok(result); } + [HttpPut("{id:guid}/region")] + public async Task AssignToRegion(Guid id, AssignRouteToRegionRequest request, CancellationToken ct) + { + var result = await service.AssignToRegionAsync(id, request, ct); + return result is null ? NotFound() : Ok(result); + } + [HttpDelete("{id:guid}")] public async Task Delete(Guid id, CancellationToken ct) { diff --git a/Distribution/Features/Route/RouteDTOs.cs b/Distribution/Features/Route/RouteDTOs.cs index 9ec9018..fb918d1 100644 --- a/Distribution/Features/Route/RouteDTOs.cs +++ b/Distribution/Features/Route/RouteDTOs.cs @@ -5,3 +5,5 @@ public record CreateRouteRequest(Guid RegionId, string RouteName, string StartPo public record UpdateRouteRequest(Guid RegionId, string RouteName, string StartPoint, string EndPoint, decimal Length); public record RouteResponse(Guid Id, Guid RegionId, string RouteName, string StartPoint, string EndPoint, decimal Length, bool IsActive, DateTime CreatedAt); + +public record AssignRouteToRegionRequest(Guid RegionId); diff --git a/Distribution/Features/Route/RouteService.cs b/Distribution/Features/Route/RouteService.cs index 0d41774..8fce029 100644 --- a/Distribution/Features/Route/RouteService.cs +++ b/Distribution/Features/Route/RouteService.cs @@ -2,7 +2,7 @@ using Distribution.Infra.UoW; namespace Distribution.Features.Route; -public class RouteService(IRouteRepository repo, IUnitOfWork uow) +public class RouteService(IRouteRepository repo, Region.IRegionRepository regionRepo, IUnitOfWork uow) { public async Task> GetAllAsync() => (await repo.GetAllAsync()) @@ -64,6 +64,29 @@ public class RouteService(IRouteRepository repo, IUnitOfWork uow) return new RouteResponse(route.Id, route.FK_RegionId, route.RouteName, route.StartPoint, route.EndPoint, route.Length, route.IsActive, route.CreatedAt); } + public async Task AssignToRegionAsync(Guid id, AssignRouteToRegionRequest request, CancellationToken ct = default) + { + var route = await repo.GetByIdAsync(id); + if (route is null || !route.IsActive) return null; + + var region = await regionRepo.GetByIdAsync(request.RegionId); + if (region is null || !region.IsActive) return null; + + route.FK_RegionId = request.RegionId; + await uow.BeginAsync(ct); + try + { + repo.Update(route); + await uow.CommitAsync(ct); + } + catch + { + await uow.RollbackAsync(ct); + throw; + } + return new RouteResponse(route.Id, route.FK_RegionId, 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);