Updating character location based on occupied tile
This commit is contained in:
@@ -104,6 +104,31 @@ public class CharactersController : ControllerBase
|
||||
return Ok(locations);
|
||||
}
|
||||
|
||||
[HttpPut("{id}/coord")]
|
||||
[Authorize(Roles = "USER,SUPER")]
|
||||
public async Task<IActionResult> UpdateCoord(string id, [FromBody] UpdateCharacterCoordRequest req)
|
||||
{
|
||||
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
if (string.IsNullOrWhiteSpace(userId))
|
||||
return Unauthorized();
|
||||
if (req.Coord is null)
|
||||
return BadRequest("Coord required");
|
||||
|
||||
var allowAnyOwner = User.IsInRole("SUPER");
|
||||
var character = await _characters.GetByIdAsync(id);
|
||||
if (character is null)
|
||||
return NotFound();
|
||||
if (!allowAnyOwner && character.OwnerUserId != userId)
|
||||
return Forbid();
|
||||
|
||||
character.Coord = req.Coord;
|
||||
var updated = await _characters.UpdateCoordAsync(id, req.Coord);
|
||||
if (!updated)
|
||||
return NotFound();
|
||||
|
||||
return Ok(character);
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
[Authorize(Roles = "USER,SUPER")]
|
||||
public async Task<IActionResult> Delete(string id)
|
||||
|
||||
@@ -4,12 +4,21 @@ This service expects JSON request bodies for character creation and stores
|
||||
character documents in MongoDB.
|
||||
|
||||
Inbound JSON documents
|
||||
- CreateCharacterRequest (`POST /api/characters`)
|
||||
```json
|
||||
{
|
||||
"name": "string"
|
||||
}
|
||||
```
|
||||
- CreateCharacterRequest (`POST /api/characters`)
|
||||
```json
|
||||
{
|
||||
"name": "string"
|
||||
}
|
||||
```
|
||||
- UpdateCharacterCoordRequest (`PUT /api/characters/{id}/coord`)
|
||||
```json
|
||||
{
|
||||
"coord": {
|
||||
"x": "number",
|
||||
"y": "number"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Stored documents (MongoDB)
|
||||
- Character
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace CharacterApi.Models;
|
||||
|
||||
public class UpdateCharacterCoordRequest
|
||||
{
|
||||
public required Coord Coord { get; set; }
|
||||
}
|
||||
@@ -6,5 +6,6 @@ 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.
|
||||
- `PUT /api/characters/{id}/coord` Update the current location coord for an owned character.
|
||||
- `GET /api/characters/{id}/visible-locations` Ensure and list locations visible to that owned character.
|
||||
- `DELETE /api/characters/{id}` Delete a character owned by the current user.
|
||||
|
||||
@@ -34,6 +34,14 @@ public class CharacterStore
|
||||
public async Task<Character?> GetByIdAsync(string id) =>
|
||||
await _col.Find(c => c.Id == id).FirstOrDefaultAsync();
|
||||
|
||||
public async Task<bool> UpdateCoordAsync(string id, Coord coord)
|
||||
{
|
||||
var filter = Builders<Character>.Filter.Eq(c => c.Id, id);
|
||||
var update = Builders<Character>.Update.Set(c => c.Coord, coord);
|
||||
var result = await _col.UpdateOneAsync(filter, update);
|
||||
return result.ModifiedCount > 0 || result.MatchedCount > 0;
|
||||
}
|
||||
|
||||
public Task<List<VisibleLocation>> GetVisibleLocationsAsync(Character character)
|
||||
{
|
||||
return GetVisibleLocationsInternalAsync(character, ensureGenerated: false);
|
||||
|
||||
Reference in New Issue
Block a user