From 3a99c2154c2ec63be554b57f41e9fefef7b0e656 Mon Sep 17 00:00:00 2001 From: Zeeshaun Date: Fri, 23 Jan 2026 14:24:08 -0600 Subject: [PATCH] Location Service now ensures that the 0,0 location exists --- .../LocationsApi/Services/LocationStore.cs | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) 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. + } + } }