Compare commits

..

3 Commits

Author SHA1 Message Date
db632e2522 Merge pull request 'character-coords' (#5) from character-coords into main
All checks were successful
Deploy Promiscuity Auth API / deploy (push) Successful in 45s
Deploy Promiscuity Character API / deploy (push) Successful in 1m25s
Deploy Promiscuity Locations API / deploy (push) Successful in 1m0s
k8s smoke test / test (push) Successful in 7s
Reviewed-on: #5
2026-01-27 20:23:25 -06:00
Zeeshaun
3a99c2154c Location Service now ensures that the 0,0 location exists 2026-01-23 14:24:08 -06:00
Zeeshaun
241312b0be Adding character coords 2026-01-23 14:24:08 -06:00
5 changed files with 54 additions and 10 deletions

View File

@ -28,12 +28,13 @@ public class CharactersController : ControllerBase
if (string.IsNullOrWhiteSpace(userId)) if (string.IsNullOrWhiteSpace(userId))
return Unauthorized(); return Unauthorized();
var character = new Character var character = new Character
{ {
OwnerUserId = userId, OwnerUserId = userId,
Name = req.Name.Trim(), Name = req.Name.Trim(),
CreatedUtc = DateTime.UtcNow Coord = new Coord { X = 0, Y = 0 },
}; CreatedUtc = DateTime.UtcNow
};
await _characters.CreateAsync(character); await _characters.CreateAsync(character);
return Ok(character); return Ok(character);

View File

@ -18,6 +18,10 @@ Stored documents (MongoDB)
"id": "string (ObjectId)", "id": "string (ObjectId)",
"ownerUserId": "string", "ownerUserId": "string",
"name": "string", "name": "string",
"coord": {
"x": "number",
"y": "number"
},
"createdUtc": "string (ISO-8601 datetime)" "createdUtc": "string (ISO-8601 datetime)"
} }
``` ```

View File

@ -11,7 +11,9 @@ public class Character
public string OwnerUserId { get; set; } = string.Empty; public string OwnerUserId { get; set; } = string.Empty;
public string Name { get; set; } = string.Empty; public string Name { get; set; } = string.Empty;
public DateTime CreatedUtc { get; set; } = DateTime.UtcNow; public Coord Coord { get; set; } = new();
}
public DateTime CreatedUtc { get; set; } = DateTime.UtcNow;
}

View File

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

View File

@ -23,6 +23,8 @@ public class LocationStore
.Ascending(l => l.Coord.Y); .Ascending(l => l.Coord.Y);
var coordIndexOptions = new CreateIndexOptions { Unique = true }; var coordIndexOptions = new CreateIndexOptions { Unique = true };
_col.Indexes.CreateOne(new CreateIndexModel<Location>(coordIndex, coordIndexOptions)); _col.Indexes.CreateOne(new CreateIndexModel<Location>(coordIndex, coordIndexOptions));
EnsureOriginLocation();
} }
private static void EnsureLocationSchema(IMongoDatabase db, string collectionName) private static void EnsureLocationSchema(IMongoDatabase db, string collectionName)
@ -100,4 +102,31 @@ public class LocationStore
var result = await _col.UpdateOneAsync(filter, update); var result = await _col.UpdateOneAsync(filter, update);
return result.ModifiedCount > 0; return result.ModifiedCount > 0;
} }
private void EnsureOriginLocation()
{
var filter = Builders<Location>.Filter.And(
Builders<Location>.Filter.Eq(l => l.Coord.X, 0),
Builders<Location>.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.
}
}
} }