Adding endpoint to only get locations visible to character
This commit is contained in:
@@ -19,10 +19,10 @@ public class CharactersController : ControllerBase
|
||||
|
||||
[HttpPost]
|
||||
[Authorize(Roles = "USER,SUPER")]
|
||||
public async Task<IActionResult> Create([FromBody] CreateCharacterRequest req)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(req.Name))
|
||||
return BadRequest("Name required");
|
||||
public async Task<IActionResult> Create([FromBody] CreateCharacterRequest req)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(req.Name))
|
||||
return BadRequest("Name required");
|
||||
|
||||
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
if (string.IsNullOrWhiteSpace(userId))
|
||||
@@ -33,6 +33,7 @@ public class CharactersController : ControllerBase
|
||||
OwnerUserId = userId,
|
||||
Name = req.Name.Trim(),
|
||||
Coord = new Coord { X = 0, Y = 0 },
|
||||
VisionRadius = 3,
|
||||
CreatedUtc = DateTime.UtcNow
|
||||
};
|
||||
|
||||
@@ -42,19 +43,36 @@ public class CharactersController : ControllerBase
|
||||
|
||||
[HttpGet]
|
||||
[Authorize(Roles = "USER,SUPER")]
|
||||
public async Task<IActionResult> ListMine()
|
||||
{
|
||||
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
if (string.IsNullOrWhiteSpace(userId))
|
||||
return Unauthorized();
|
||||
public async Task<IActionResult> ListMine()
|
||||
{
|
||||
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
if (string.IsNullOrWhiteSpace(userId))
|
||||
return Unauthorized();
|
||||
|
||||
var characters = await _characters.GetForOwnerAsync(userId);
|
||||
return Ok(characters);
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
[Authorize(Roles = "USER,SUPER")]
|
||||
public async Task<IActionResult> Delete(string id)
|
||||
var characters = await _characters.GetForOwnerAsync(userId);
|
||||
return Ok(characters);
|
||||
}
|
||||
|
||||
[HttpGet("{id}/visible-locations")]
|
||||
[Authorize(Roles = "USER,SUPER")]
|
||||
public async Task<IActionResult> VisibleLocations(string id)
|
||||
{
|
||||
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
if (string.IsNullOrWhiteSpace(userId))
|
||||
return Unauthorized();
|
||||
|
||||
var allowAnyOwner = User.IsInRole("SUPER");
|
||||
var character = await _characters.GetForOwnerByIdAsync(id, userId, allowAnyOwner);
|
||||
if (character is null)
|
||||
return NotFound();
|
||||
|
||||
var locations = await _characters.GetVisibleLocationsAsync(character);
|
||||
return Ok(locations);
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
[Authorize(Roles = "USER,SUPER")]
|
||||
public async Task<IActionResult> Delete(string id)
|
||||
{
|
||||
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
if (string.IsNullOrWhiteSpace(userId))
|
||||
|
||||
@@ -12,16 +12,32 @@ Inbound JSON documents
|
||||
```
|
||||
|
||||
Stored documents (MongoDB)
|
||||
- Character
|
||||
```json
|
||||
{
|
||||
"id": "string (ObjectId)",
|
||||
"ownerUserId": "string",
|
||||
"name": "string",
|
||||
"coord": {
|
||||
"x": "number",
|
||||
"y": "number"
|
||||
},
|
||||
"createdUtc": "string (ISO-8601 datetime)"
|
||||
}
|
||||
```
|
||||
- Character
|
||||
```json
|
||||
{
|
||||
"id": "string (ObjectId)",
|
||||
"ownerUserId": "string",
|
||||
"name": "string",
|
||||
"coord": {
|
||||
"x": "number",
|
||||
"y": "number"
|
||||
},
|
||||
"visionRadius": "number",
|
||||
"createdUtc": "string (ISO-8601 datetime)"
|
||||
}
|
||||
```
|
||||
|
||||
Outbound JSON documents
|
||||
- VisibleLocation (`GET /api/characters/{id}/visible-locations`)
|
||||
```json
|
||||
[
|
||||
{
|
||||
"id": "string (ObjectId)",
|
||||
"name": "string",
|
||||
"coord": {
|
||||
"x": "number",
|
||||
"y": "number"
|
||||
}
|
||||
}
|
||||
]
|
||||
```
|
||||
|
||||
@@ -15,5 +15,7 @@ public class Character
|
||||
|
||||
public Coord Coord { get; set; } = new();
|
||||
|
||||
public int VisionRadius { get; set; } = 3;
|
||||
|
||||
public DateTime CreatedUtc { get; set; } = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
|
||||
namespace CharacterApi.Models;
|
||||
|
||||
public class VisibleLocation
|
||||
{
|
||||
[BsonId]
|
||||
[BsonRepresentation(BsonType.ObjectId)]
|
||||
public string? Id { get; set; }
|
||||
|
||||
[BsonElement("name")]
|
||||
public string Name { get; set; } = string.Empty;
|
||||
|
||||
[BsonElement("coord")]
|
||||
public Coord Coord { get; set; } = new();
|
||||
}
|
||||
@@ -6,4 +6,5 @@ See `DOCUMENTS.md` for request payloads and stored document shapes.
|
||||
## Endpoints
|
||||
- `POST /api/characters` Create a character.
|
||||
- `GET /api/characters` List characters for the current user.
|
||||
- `GET /api/characters/{id}/visible-locations` List locations visible to that owned character.
|
||||
- `DELETE /api/characters/{id}` Delete a character owned by the current user.
|
||||
|
||||
@@ -3,31 +3,65 @@ using MongoDB.Driver;
|
||||
|
||||
namespace CharacterApi.Services;
|
||||
|
||||
public class CharacterStore
|
||||
{
|
||||
private readonly IMongoCollection<Character> _col;
|
||||
|
||||
public CharacterStore(IConfiguration cfg)
|
||||
{
|
||||
var cs = cfg["MongoDB:ConnectionString"] ?? "mongodb://127.0.0.1:27017";
|
||||
var dbName = cfg["MongoDB:DatabaseName"] ?? "GameDb";
|
||||
var client = new MongoClient(cs);
|
||||
var db = client.GetDatabase(dbName);
|
||||
_col = db.GetCollection<Character>("Characters");
|
||||
|
||||
var ownerIndex = Builders<Character>.IndexKeys.Ascending(c => c.OwnerUserId);
|
||||
_col.Indexes.CreateOne(new CreateIndexModel<Character>(ownerIndex));
|
||||
}
|
||||
public class CharacterStore
|
||||
{
|
||||
private readonly IMongoCollection<Character> _col;
|
||||
private readonly IMongoCollection<VisibleLocation> _locations;
|
||||
|
||||
public CharacterStore(IConfiguration cfg)
|
||||
{
|
||||
var cs = cfg["MongoDB:ConnectionString"] ?? "mongodb://127.0.0.1:27017";
|
||||
var dbName = cfg["MongoDB:DatabaseName"] ?? "GameDb";
|
||||
var client = new MongoClient(cs);
|
||||
var db = client.GetDatabase(dbName);
|
||||
_col = db.GetCollection<Character>("Characters");
|
||||
_locations = db.GetCollection<VisibleLocation>("Locations");
|
||||
|
||||
var ownerIndex = Builders<Character>.IndexKeys.Ascending(c => c.OwnerUserId);
|
||||
_col.Indexes.CreateOne(new CreateIndexModel<Character>(ownerIndex));
|
||||
}
|
||||
|
||||
public Task CreateAsync(Character character) => _col.InsertOneAsync(character);
|
||||
|
||||
public Task<List<Character>> GetForOwnerAsync(string ownerUserId) =>
|
||||
_col.Find(c => c.OwnerUserId == ownerUserId).ToListAsync();
|
||||
|
||||
public async Task<bool> DeleteForOwnerAsync(string id, string ownerUserId, bool allowAnyOwner)
|
||||
{
|
||||
var filter = Builders<Character>.Filter.Eq(c => c.Id, id);
|
||||
if (!allowAnyOwner)
|
||||
public Task<List<Character>> GetForOwnerAsync(string ownerUserId) =>
|
||||
_col.Find(c => c.OwnerUserId == ownerUserId).ToListAsync();
|
||||
|
||||
public async Task<Character?> GetForOwnerByIdAsync(string id, string ownerUserId, bool allowAnyOwner)
|
||||
{
|
||||
var filter = Builders<Character>.Filter.Eq(c => c.Id, id);
|
||||
if (!allowAnyOwner)
|
||||
{
|
||||
filter = Builders<Character>.Filter.And(
|
||||
filter,
|
||||
Builders<Character>.Filter.Eq(c => c.OwnerUserId, ownerUserId)
|
||||
);
|
||||
}
|
||||
|
||||
return await _col.Find(filter).FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public Task<List<VisibleLocation>> GetVisibleLocationsAsync(Character character)
|
||||
{
|
||||
var radius = character.VisionRadius > 0 ? character.VisionRadius : 3;
|
||||
var minX = character.Coord.X - radius;
|
||||
var maxX = character.Coord.X + radius;
|
||||
var minY = character.Coord.Y - radius;
|
||||
var maxY = character.Coord.Y + radius;
|
||||
|
||||
var filter = Builders<VisibleLocation>.Filter.And(
|
||||
Builders<VisibleLocation>.Filter.Gte(l => l.Coord.X, minX),
|
||||
Builders<VisibleLocation>.Filter.Lte(l => l.Coord.X, maxX),
|
||||
Builders<VisibleLocation>.Filter.Gte(l => l.Coord.Y, minY),
|
||||
Builders<VisibleLocation>.Filter.Lte(l => l.Coord.Y, maxY)
|
||||
);
|
||||
|
||||
return _locations.Find(filter).ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteForOwnerAsync(string id, string ownerUserId, bool allowAnyOwner)
|
||||
{
|
||||
var filter = Builders<Character>.Filter.Eq(c => c.Id, id);
|
||||
if (!allowAnyOwner)
|
||||
{
|
||||
filter = Builders<Character>.Filter.And(
|
||||
filter,
|
||||
|
||||
Reference in New Issue
Block a user