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 Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MongoDB.Driver;
namespace LocationsApi.Controllers;
@ -24,25 +23,13 @@ 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");
}
await _locations.CreateAsync(location);
return Ok(location);
}
@ -72,9 +59,6 @@ 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,11 +7,7 @@ Inbound JSON documents
- CreateLocationRequest (`POST /api/locations`)
```json
{
"name": "string",
"coord": {
"x": 0,
"y": 0
}
"name": "string"
}
```
- UpdateLocationRequest (`PUT /api/locations/{id}`)
@ -20,7 +16,6 @@ Inbound JSON documents
"name": "string"
}
```
`coord` cannot be updated.
Stored documents (MongoDB)
- Location
@ -28,10 +23,6 @@ Stored documents (MongoDB)
{
"id": "string (ObjectId)",
"name": "string",
"coord": {
"x": 0,
"y": 0
},
"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 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 required Coord Coord { get; set; }
public DateTime CreatedUtc { get; set; } = DateTime.UtcNow;
}

View File

@ -3,6 +3,4 @@ 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 with a unique coord pair (SUPER only).
- `POST /api/locations` Create a location (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,5 +1,4 @@
using LocationsApi.Models;
using MongoDB.Bson;
using MongoDB.Driver;
namespace LocationsApi.Services;
@ -14,71 +13,8 @@ public class LocationStore
var dbName = cfg["MongoDB:DatabaseName"] ?? "GameDb";
var client = new MongoClient(cs);
var db = client.GetDatabase(dbName);
var collectionName = "Locations";
EnsureLocationSchema(db, collectionName);
_col = db.GetCollection<Location>(collectionName);
_col = db.GetCollection<Location>("Locations");
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);