Compare commits
2 Commits
88e3767971
...
86fa27c78d
| Author | SHA1 | Date | |
|---|---|---|---|
| 86fa27c78d | |||
| 268eca39c0 |
@ -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
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -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();
|
||||||
|
|||||||
@ -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)"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
8
microservices/LocationsApi/Models/Coord.cs
Normal file
8
microservices/LocationsApi/Models/Coord.cs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
namespace LocationsApi.Models;
|
||||||
|
|
||||||
|
public class Coord
|
||||||
|
{
|
||||||
|
public int X { get; set; }
|
||||||
|
|
||||||
|
public int Y { get; set; }
|
||||||
|
}
|
||||||
@ -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; }
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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; }
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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).
|
||||||
|
|||||||
@ -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);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user