diff --git a/microservices/CharacterApi/Controllers/CharactersController.cs b/microservices/CharacterApi/Controllers/CharactersController.cs index 77f0069..060edc4 100644 --- a/microservices/CharacterApi/Controllers/CharactersController.cs +++ b/microservices/CharacterApi/Controllers/CharactersController.cs @@ -28,12 +28,13 @@ public class CharactersController : ControllerBase if (string.IsNullOrWhiteSpace(userId)) return Unauthorized(); - var character = new Character - { - OwnerUserId = userId, - Name = req.Name.Trim(), - CreatedUtc = DateTime.UtcNow - }; + var character = new Character + { + OwnerUserId = userId, + Name = req.Name.Trim(), + Coord = new Coord { X = 0, Y = 0 }, + CreatedUtc = DateTime.UtcNow + }; await _characters.CreateAsync(character); return Ok(character); diff --git a/microservices/CharacterApi/DOCUMENTS.md b/microservices/CharacterApi/DOCUMENTS.md index c75b3da..5e531e7 100644 --- a/microservices/CharacterApi/DOCUMENTS.md +++ b/microservices/CharacterApi/DOCUMENTS.md @@ -18,6 +18,10 @@ Stored documents (MongoDB) "id": "string (ObjectId)", "ownerUserId": "string", "name": "string", + "coord": { + "x": "number", + "y": "number" + }, "createdUtc": "string (ISO-8601 datetime)" } ``` diff --git a/microservices/CharacterApi/Models/Character.cs b/microservices/CharacterApi/Models/Character.cs index 7d26b1f..d5ade03 100644 --- a/microservices/CharacterApi/Models/Character.cs +++ b/microservices/CharacterApi/Models/Character.cs @@ -11,7 +11,9 @@ public class Character public string OwnerUserId { get; set; } = string.Empty; - public string Name { get; set; } = string.Empty; - - public DateTime CreatedUtc { get; set; } = DateTime.UtcNow; -} + public string Name { get; set; } = string.Empty; + + public Coord Coord { get; set; } = new(); + + public DateTime CreatedUtc { get; set; } = DateTime.UtcNow; +} diff --git a/microservices/CharacterApi/Models/Coord.cs b/microservices/CharacterApi/Models/Coord.cs new file mode 100644 index 0000000..d9d7544 --- /dev/null +++ b/microservices/CharacterApi/Models/Coord.cs @@ -0,0 +1,8 @@ +namespace CharacterApi.Models; + +public class Coord +{ + public int X { get; set; } + + public int Y { get; set; } +} diff --git a/microservices/LocationsApi/Services/LocationStore.cs b/microservices/LocationsApi/Services/LocationStore.cs index 6689401..9ae3956 100644 --- a/microservices/LocationsApi/Services/LocationStore.cs +++ b/microservices/LocationsApi/Services/LocationStore.cs @@ -23,6 +23,8 @@ public class LocationStore .Ascending(l => l.Coord.Y); var coordIndexOptions = new CreateIndexOptions { Unique = true }; _col.Indexes.CreateOne(new CreateIndexModel(coordIndex, coordIndexOptions)); + + EnsureOriginLocation(); } private static void EnsureLocationSchema(IMongoDatabase db, string collectionName) @@ -100,4 +102,31 @@ public class LocationStore var result = await _col.UpdateOneAsync(filter, update); return result.ModifiedCount > 0; } + + private void EnsureOriginLocation() + { + var filter = Builders.Filter.And( + Builders.Filter.Eq(l => l.Coord.X, 0), + Builders.Filter.Eq(l => l.Coord.Y, 0) + ); + var existing = _col.Find(filter).FirstOrDefault(); + if (existing is not null) + return; + + var origin = new Location + { + Name = "Origin", + Coord = new Coord { X = 0, Y = 0 }, + CreatedUtc = DateTime.UtcNow + }; + + try + { + _col.InsertOne(origin); + } + catch (MongoWriteException ex) when (ex.WriteError.Category == ServerErrorCategory.DuplicateKey) + { + // Another instance seeded it first. + } + } }