Adding coordinate to Location model

This commit is contained in:
2026-01-20 17:25:35 -06:00
parent 88e3767971
commit 268eca39c0
7 changed files with 99 additions and 4 deletions
@@ -2,6 +2,7 @@ using LocationsApi.Models;
using LocationsApi.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MongoDB.Driver;
namespace LocationsApi.Controllers;
@@ -23,13 +24,25 @@ public class LocationsController : ControllerBase
if (string.IsNullOrWhiteSpace(req.Name))
return BadRequest("Name required");
if (req.Coord is null)
return BadRequest("Coord required");
var location = new Location
{
Name = req.Name.Trim(),
Coord = req.Coord,
CreatedUtc = DateTime.UtcNow
};
await _locations.CreateAsync(location);
try
{
await _locations.CreateAsync(location);
}
catch (MongoWriteException ex) when (ex.WriteError.Category == ServerErrorCategory.DuplicateKey)
{
return Conflict("Coord must be unique");
}
return Ok(location);
}
@@ -59,6 +72,9 @@ public class LocationsController : ControllerBase
if (string.IsNullOrWhiteSpace(req.Name))
return BadRequest("Name required");
if (req.Coord is not null)
return BadRequest("Coord cannot be updated");
var updated = await _locations.UpdateNameAsync(id, req.Name.Trim());
if (!updated)
return NotFound();