Adding coordinate to Location model

This commit is contained in:
hz 2026-01-20 17:25:35 -06:00
parent 88e3767971
commit 268eca39c0
7 changed files with 99 additions and 4 deletions

View File

@ -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
};
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();

View File

@ -7,7 +7,11 @@ Inbound JSON documents
- CreateLocationRequest (`POST /api/locations`)
```json
{
"name": "string"
"name": "string",
"coord": {
"x": 0,
"y": 0
}
}
```
- UpdateLocationRequest (`PUT /api/locations/{id}`)
@ -16,6 +20,7 @@ Inbound JSON documents
"name": "string"
}
```
`coord` cannot be updated.
Stored documents (MongoDB)
- Location
@ -23,6 +28,10 @@ Stored documents (MongoDB)
{
"id": "string (ObjectId)",
"name": "string",
"coord": {
"x": 0,
"y": 0
},
"createdUtc": "string (ISO-8601 datetime)"
}
```

View File

@ -3,4 +3,6 @@ namespace LocationsApi.Models;
public class CreateLocationRequest
{
public string Name { get; set; } = string.Empty;
public required Coord Coord { get; set; }
}

View File

@ -11,5 +11,7 @@ public class Location
public string Name { get; set; } = string.Empty;
public required Coord Coord { get; set; }
public DateTime CreatedUtc { get; set; } = DateTime.UtcNow;
}

View File

@ -3,4 +3,6 @@ namespace LocationsApi.Models;
public class UpdateLocationRequest
{
public string Name { get; set; } = string.Empty;
public Coord? Coord { get; set; }
}

View File

@ -4,7 +4,7 @@
See `DOCUMENTS.md` for request payloads and stored document shapes.
## Endpoints
- `POST /api/locations` Create a location (SUPER only).
- `POST /api/locations` Create a location with a unique coord pair (SUPER only).
- `GET /api/locations` List all locations (SUPER only).
- `DELETE /api/locations/{id}` Delete a location (SUPER only).
- `PUT /api/locations/{id}` Update a location name (SUPER only).

View File

@ -1,4 +1,5 @@
using LocationsApi.Models;
using MongoDB.Bson;
using MongoDB.Driver;
namespace LocationsApi.Services;
@ -13,8 +14,71 @@ public class LocationStore
var dbName = cfg["MongoDB:DatabaseName"] ?? "GameDb";
var client = new MongoClient(cs);
var db = client.GetDatabase(dbName);
_col = db.GetCollection<Location>("Locations");
var collectionName = "Locations";
EnsureLocationSchema(db, collectionName);
_col = db.GetCollection<Location>(collectionName);
var coordIndex = Builders<Location>.IndexKeys
.Ascending(l => l.Coord.X)
.Ascending(l => l.Coord.Y);
var coordIndexOptions = new CreateIndexOptions { Unique = true };
_col.Indexes.CreateOne(new CreateIndexModel<Location>(coordIndex, coordIndexOptions));
}
private static void EnsureLocationSchema(IMongoDatabase db, string collectionName)
{
var validator = new BsonDocument
{
{
"$jsonSchema", new BsonDocument
{
{ "bsonType", "object" },
{ "required", new BsonArray { "name", "coord", "createdUtc" } },
{
"properties", new BsonDocument
{
{ "name", new BsonDocument { { "bsonType", "string" } } },
{
"coord", new BsonDocument
{
{ "bsonType", "object" },
{ "required", new BsonArray { "x", "y" } },
{
"properties", new BsonDocument
{
{ "x", new BsonDocument { { "bsonType", "int" } } },
{ "y", new BsonDocument { { "bsonType", "int" } } }
}
}
}
},
{ "createdUtc", new BsonDocument { { "bsonType", "date" } } }
}
}
}
}
};
var options = new CreateCollectionOptions
{
Validator = new BsonDocumentFilterDefinition<BsonDocument>(validator),
ValidationAction = DocumentValidationAction.Error
};
var collections = db.ListCollectionNames().ToList();
if (!collections.Contains(collectionName))
{
db.CreateCollection(collectionName, options);
return;
}
var command = new BsonDocument
{
{ "collMod", collectionName },
{ "validator", validator },
{ "validationAction", "error" }
};
db.RunCommand<BsonDocument>(command);
}
public Task CreateAsync(Location location) => _col.InsertOneAsync(location);