Skip to content

Commit

Permalink
Return HTTP Conflict when a route for this user already exists which …
Browse files Browse the repository at this point in the history
…is exactly the same
  • Loading branch information
sandermvanvliet committed Dec 29, 2023
1 parent d99f4a0 commit a56c168
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/RoadCaptain.App.Web/Adapters/SqliteRouteStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ public Models.RouteModel Store(CreateRouteModel createModel, User user)
{
var route = RouteStorageModelFrom(createModel, user);

if (_roadCaptainDataContext.Routes.Any(r => r.Hash == route.Hash && r.UserId == route.UserId))
{
throw new DuplicateRouteException();
}

_roadCaptainDataContext.Routes.Add(route);
_roadCaptainDataContext.SaveChanges();

Expand Down
14 changes: 13 additions & 1 deletion src/RoadCaptain.App.Web/Controllers/RoutesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,19 @@ public IActionResult CreateRoute([FromBody] CreateRouteModel createRoute)
(int)HttpStatusCode.BadRequest, "Invalid user"));
}

return Ok(_routeStore.Store(createRoute, user));
try
{
return Ok(_routeStore.Store(createRoute, user));
}
catch (DuplicateRouteException e)
{
_monitoringEvents.Error(e, "Duplicate route exists");

return Conflict(ProblemDetailsFactory.CreateProblemDetails(
HttpContext,
(int)HttpStatusCode.Conflict,
"Duplicate route exists"));
}
}

[HttpPut("{id:long}", Name = "UpdateRouteById")]
Expand Down
10 changes: 10 additions & 0 deletions src/RoadCaptain.App.Web/DuplicateRouteException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace RoadCaptain.App.Web
{
public class DuplicateRouteException : Exception
{
public DuplicateRouteException()
: base("Another route exists that is exactly the same")
{
}
}
}

0 comments on commit a56c168

Please sign in to comment.