Tracking character movement
This commit is contained in:
@@ -8,14 +8,16 @@ namespace CharacterApi.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class CharactersController : ControllerBase
|
||||
{
|
||||
private readonly CharacterStore _characters;
|
||||
|
||||
public CharactersController(CharacterStore characters)
|
||||
{
|
||||
_characters = characters;
|
||||
}
|
||||
public class CharactersController : ControllerBase
|
||||
{
|
||||
private readonly CharacterStore _characters;
|
||||
private readonly LocationsClient _locations;
|
||||
|
||||
public CharactersController(CharacterStore characters, LocationsClient locations)
|
||||
{
|
||||
_characters = characters;
|
||||
_locations = locations;
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Authorize(Roles = "USER,SUPER")]
|
||||
@@ -54,17 +56,52 @@ public class CharactersController : ControllerBase
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
[Authorize(Roles = "USER,SUPER")]
|
||||
public async Task<IActionResult> Delete(string id)
|
||||
{
|
||||
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
if (string.IsNullOrWhiteSpace(userId))
|
||||
return Unauthorized();
|
||||
public async Task<IActionResult> Delete(string id)
|
||||
{
|
||||
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
if (string.IsNullOrWhiteSpace(userId))
|
||||
return Unauthorized();
|
||||
|
||||
var allowAnyOwner = User.IsInRole("SUPER");
|
||||
var deleted = await _characters.DeleteForOwnerAsync(id, userId, allowAnyOwner);
|
||||
if (!deleted)
|
||||
return NotFound();
|
||||
|
||||
return Ok("Deleted");
|
||||
}
|
||||
}
|
||||
|
||||
return Ok("Deleted");
|
||||
}
|
||||
|
||||
[HttpPut("{id}/move")]
|
||||
[Authorize(Roles = "USER,SUPER")]
|
||||
public async Task<IActionResult> Move(string id, [FromBody] MoveCharacterRequest req, CancellationToken ct)
|
||||
{
|
||||
if (req?.Coord is null)
|
||||
return BadRequest("Coord required");
|
||||
|
||||
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
if (string.IsNullOrWhiteSpace(userId))
|
||||
return Unauthorized();
|
||||
|
||||
var allowAnyOwner = User.IsInRole("SUPER");
|
||||
var existing = await _characters.GetForOwnerByIdAsync(id, userId, allowAnyOwner);
|
||||
if (existing is null)
|
||||
return NotFound();
|
||||
|
||||
var presence = await _locations.UpdatePresenceAsync(id, req.Coord, ct);
|
||||
if (!presence.Ok)
|
||||
{
|
||||
var message = string.IsNullOrWhiteSpace(presence.Body)
|
||||
? "Location presence update failed"
|
||||
: presence.Body;
|
||||
return StatusCode((int)presence.Status, message);
|
||||
}
|
||||
|
||||
var updated = await _characters.UpdateCoordAsync(id, userId, allowAnyOwner, req.Coord);
|
||||
if (!updated)
|
||||
{
|
||||
await _locations.UpdatePresenceAsync(id, existing.Coord, ct);
|
||||
return StatusCode(500, "Failed to update character coord");
|
||||
}
|
||||
|
||||
return Ok("Moved");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user