Compare commits

..

No commits in common. "86fa27c78d333e5a9a40428113648cc2937e4691" and "88e376797146a1ceef7431c28cf4e8f4ac55b987" have entirely different histories.

8 changed files with 4 additions and 107 deletions

View File

@ -2,7 +2,6 @@ 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;
@ -24,25 +23,13 @@ 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
}; };
try
{
await _locations.CreateAsync(location); 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);
} }
@ -72,9 +59,6 @@ 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,11 +7,7 @@ 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}`)
@ -20,7 +16,6 @@ Inbound JSON documents
"name": "string" "name": "string"
} }
``` ```
`coord` cannot be updated.
Stored documents (MongoDB) Stored documents (MongoDB)
- Location - Location
@ -28,10 +23,6 @@ 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

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

View File

@ -3,6 +3,4 @@ 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,7 +11,5 @@ 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,6 +3,4 @@ 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 with a unique coord pair (SUPER only). - `POST /api/locations` Create a location (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,5 +1,4 @@
using LocationsApi.Models; using LocationsApi.Models;
using MongoDB.Bson;
using MongoDB.Driver; using MongoDB.Driver;
namespace LocationsApi.Services; namespace LocationsApi.Services;
@ -14,71 +13,8 @@ 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);
var collectionName = "Locations"; _col = db.GetCollection<Location>("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);