Compare commits

...

2 Commits

Author SHA1 Message Date
hz
86fa27c78d Adding coordinate to Location model
Some checks failed
Deploy Promiscuity Auth API / deploy (push) Successful in 44s
Deploy Promiscuity Character API / deploy (push) Successful in 42s
Deploy Promiscuity Locations API / deploy (push) Failing after 12s
k8s smoke test / test (push) Successful in 5s
2026-01-20 17:25:41 -06:00
hz
268eca39c0 Adding coordinate to Location model 2026-01-20 17:25:35 -06:00
8 changed files with 107 additions and 4 deletions

View File

@ -2,6 +2,7 @@ using LocationsApi.Models;
using LocationsApi.Services; using LocationsApi.Services;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using MongoDB.Driver;
namespace LocationsApi.Controllers; namespace LocationsApi.Controllers;
@ -23,13 +24,25 @@ public class LocationsController : ControllerBase
if (string.IsNullOrWhiteSpace(req.Name)) if (string.IsNullOrWhiteSpace(req.Name))
return BadRequest("Name required"); return BadRequest("Name required");
if (req.Coord is null)
return BadRequest("Coord required");
var location = new Location var location = new Location
{ {
Name = req.Name.Trim(), Name = req.Name.Trim(),
Coord = req.Coord,
CreatedUtc = DateTime.UtcNow 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); return Ok(location);
} }
@ -59,6 +72,9 @@ public class LocationsController : ControllerBase
if (string.IsNullOrWhiteSpace(req.Name)) if (string.IsNullOrWhiteSpace(req.Name))
return BadRequest("Name required"); 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()); var updated = await _locations.UpdateNameAsync(id, req.Name.Trim());
if (!updated) if (!updated)
return NotFound(); return NotFound();

View File

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

View File

@ -0,0 +1,8 @@
namespace LocationsApi.Models;
public class Coord
{
public int X { get; set; }
public int Y { get; set; }
}

View File

@ -3,4 +3,6 @@ namespace LocationsApi.Models;
public class CreateLocationRequest public class CreateLocationRequest
{ {
public string Name { get; set; } = string.Empty; 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 string Name { get; set; } = string.Empty;
public required Coord Coord { get; set; }
public DateTime CreatedUtc { get; set; } = DateTime.UtcNow; public DateTime CreatedUtc { get; set; } = DateTime.UtcNow;
} }

View File

@ -3,4 +3,6 @@ namespace LocationsApi.Models;
public class UpdateLocationRequest public class UpdateLocationRequest
{ {
public string Name { get; set; } = string.Empty; 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. See `DOCUMENTS.md` for request payloads and stored document shapes.
## Endpoints ## 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). - `GET /api/locations` List all locations (SUPER only).
- `DELETE /api/locations/{id}` Delete a location (SUPER only). - `DELETE /api/locations/{id}` Delete a location (SUPER only).
- `PUT /api/locations/{id}` Update a location name (SUPER only). - `PUT /api/locations/{id}` Update a location name (SUPER only).

View File

@ -1,4 +1,5 @@
using LocationsApi.Models; using LocationsApi.Models;
using MongoDB.Bson;
using MongoDB.Driver; using MongoDB.Driver;
namespace LocationsApi.Services; namespace LocationsApi.Services;
@ -13,8 +14,71 @@ public class LocationStore
var dbName = cfg["MongoDB:DatabaseName"] ?? "GameDb"; var dbName = cfg["MongoDB:DatabaseName"] ?? "GameDb";
var client = new MongoClient(cs); var client = new MongoClient(cs);
var db = client.GetDatabase(dbName); 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); public Task CreateAsync(Location location) => _col.InsertOneAsync(location);