using Application.DataTransferObjects.ZombieSiege; using Application.Errors; using Application.Interfaces; using Asp.Versioning; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace Api.Controllers.v1 { [Route("api/v{version:apiVersion}/[controller]")] [ApiController] [ApiVersion("1.0")] [Authorize] public class ZombieSiegesController(IZombieSiegeRepository zombieSiegeRepository, ILogger logger, IClaimTypeService claimTypeService) : ControllerBase { [HttpGet("{zombieSiegeId:guid}")] public async Task> GetZombieSiege(Guid zombieSiegeId, CancellationToken cancellationToken) { try { var zombieSiegeResult = await zombieSiegeRepository.GetZombieSiegeAsync(zombieSiegeId, cancellationToken); return zombieSiegeResult.IsFailure ? BadRequest(zombieSiegeResult.Error) : Ok(zombieSiegeResult.Value); } catch (Exception e) { logger.LogError(e, e.Message); return StatusCode(StatusCodes.Status500InternalServerError); } } [HttpGet("Alliance/{allianceId:guid}")] public async Task>> GetAllianceZombieSieges(Guid allianceId, [FromQuery] int take, CancellationToken cancellationToken) { try { var allianceZombieSiegesResult = await zombieSiegeRepository.GetAllianceZombieSiegesAsync(allianceId, take, cancellationToken); if (allianceZombieSiegesResult.IsFailure) return BadRequest(allianceZombieSiegesResult.Error); return allianceZombieSiegesResult.Value.Count > 0 ? Ok(allianceZombieSiegesResult.Value) : NoContent(); } catch (Exception e) { logger.LogError(e, e.Message); return StatusCode(StatusCodes.Status500InternalServerError); } } [HttpGet("[action]/{zombieSiegeId:guid}")] public async Task> GetZombieSiegeDetail(Guid zombieSiegeId, CancellationToken cancellationToken) { try { var zombieSiegeDetailResult = await zombieSiegeRepository.GetZombieSiegeDetailAsync(zombieSiegeId, cancellationToken); return zombieSiegeDetailResult.IsFailure ? BadRequest(zombieSiegeDetailResult.Error) : Ok(zombieSiegeDetailResult.Value); } catch (Exception e) { logger.LogError(e, e.Message); return StatusCode(StatusCodes.Status500InternalServerError); } } [HttpPost] public async Task> CreateZombieSiege(CreateZombieSiegeDto createZombieSiegeDto, CancellationToken cancellationToken) { try { if (!ModelState.IsValid) return UnprocessableEntity(ModelState); var createResult = await zombieSiegeRepository.CreateZombieSiegeAsync(createZombieSiegeDto, claimTypeService.GetFullName(User), cancellationToken); return createResult.IsFailure ? BadRequest(createResult.Error) : CreatedAtAction(nameof(GetZombieSiege), new { zombieSiegeId = createResult.Value.Id }, createResult.Value); } catch (Exception e) { logger.LogError(e, e.Message); return StatusCode(StatusCodes.Status500InternalServerError); } } [HttpPut("{zombieSiegeId:guid}")] public async Task> UpdateZombieSiege(Guid zombieSiegeId, UpdateZombieSiegeDto updateZombieSiegeDto, CancellationToken cancellationToken) { try { if (!ModelState.IsValid) return UnprocessableEntity(ModelState); if (zombieSiegeId != updateZombieSiegeDto.Id) return Conflict(ZombieSiegeErrors.IdConflict); var updateResult = await zombieSiegeRepository.UpdateZombieSiegeAsync(updateZombieSiegeDto, claimTypeService.GetFullName(User), cancellationToken); return updateResult.IsFailure ? BadRequest(updateResult.Error) : Ok(updateResult.Value); } catch (Exception e) { logger.LogError(e, e.Message); return StatusCode(StatusCodes.Status500InternalServerError); } } [HttpDelete("{zombieSiegeId:guid}")] public async Task> DeleteZombieSiege(Guid zombieSiegeId, CancellationToken cancellationToken) { try { var deleteResult = await zombieSiegeRepository.DeleteZombieSiegeAsync(zombieSiegeId, cancellationToken); return deleteResult.IsFailure ? BadRequest(deleteResult.Error) : Ok(deleteResult.Value); } catch (Exception e) { logger.LogError(e, e.Message); return StatusCode(StatusCodes.Status500InternalServerError); } } } }