using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using GameAuthApi.Models; using GameAuthApi.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace GameAuthApi.Controllers; [ApiController] [Route("api/[controller]")] public class LocationController : ControllerBase { private readonly LocationService _locationService; private readonly PlayerService _playerService; public LocationController(LocationService locationService, PlayerService playerService) { _locationService = locationService; _playerService = playerService; } [HttpPost] [Authorize(Roles = "SUPER")] public async Task Create([FromBody] CreateLocationRequest req) { var newLoc = new Location { X = req.X, Y = req.Y, Z = req.Z, N = req.N, NE = req.NE, E = req.E, SE = req.SE, S = req.S, SW = req.SW, W = req.W, NW = req.NW }; await _locationService.CreateAsync(newLoc); return Ok(newLoc); } [HttpDelete("{id}")] [Authorize(Roles = "SUPER")] public async Task Delete(string id) { var location = await _locationService.GetByIdAsync(id); if (location == null) return NotFound("Location not found."); // Null out all players at this location var affectedPlayers = await _playerService.GetByLocationIdAsync(id); foreach (var player in affectedPlayers) { player.LocationId = null; await _playerService.UpdateAsync(player); } await _locationService.DeleteAsync(id); return Ok($"Location deleted and {affectedPlayers.Count} player(s) unlinked."); } [HttpGet("{id}")] [Authorize(Roles = "USER")] public async Task GetById(string id) { var location = await _locationService.GetByIdAsync(id); if (location == null) return NotFound("Location not found"); return Ok(location); } }