Compare commits
No commits in common. "db632e2522f2c36f1cd1fa792a9b93cec61e072b" and "52e1dfe99f0f9b88b0e44d44c1b3f9ec3bfa2c1f" have entirely different histories.
db632e2522
...
52e1dfe99f
@ -28,13 +28,12 @@ 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(),
|
||||||
Coord = new Coord { X = 0, Y = 0 },
|
CreatedUtc = DateTime.UtcNow
|
||||||
CreatedUtc = DateTime.UtcNow
|
};
|
||||||
};
|
|
||||||
|
|
||||||
await _characters.CreateAsync(character);
|
await _characters.CreateAsync(character);
|
||||||
return Ok(character);
|
return Ok(character);
|
||||||
|
|||||||
@ -18,10 +18,6 @@ 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)"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@ -11,9 +11,7 @@ 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 Coord Coord { get; set; } = new();
|
public DateTime CreatedUtc { get; set; } = DateTime.UtcNow;
|
||||||
|
}
|
||||||
public DateTime CreatedUtc { get; set; } = DateTime.UtcNow;
|
|
||||||
}
|
|
||||||
|
|||||||
@ -1,8 +0,0 @@
|
|||||||
namespace CharacterApi.Models;
|
|
||||||
|
|
||||||
public class Coord
|
|
||||||
{
|
|
||||||
public int X { get; set; }
|
|
||||||
|
|
||||||
public int Y { get; set; }
|
|
||||||
}
|
|
||||||
@ -23,8 +23,6 @@ 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)
|
||||||
@ -102,31 +100,4 @@ 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.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user