using Application.DataTransferObjects.ZombieSiegeParticipant; 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 ZombieSiegeParticipantsController(IZombieSiegeParticipantRepository zombieSiegeParticipantRepository, ILogger logger) : ControllerBase { [HttpGet("{zombieSiegeParticipantId:guid}")] public async Task> GetZombieSiegeParticipant( Guid zombieSiegeParticipantId, CancellationToken cancellationToken) { try { var zombieSiegeParticipantResult = await zombieSiegeParticipantRepository.GetZombieSiegeParticipantAsync(zombieSiegeParticipantId, cancellationToken); return zombieSiegeParticipantResult.IsFailure ? BadRequest(zombieSiegeParticipantResult.Error) : Ok(zombieSiegeParticipantResult.Value); } catch (Exception e) { logger.LogError(e, "{ErrorMessage}", e.Message); return Problem( detail: $"Failed to process {nameof(GetZombieSiegeParticipant)}", statusCode: StatusCodes.Status500InternalServerError, title: "Internal server error"); } } [HttpGet("Player/{playerId:guid}")] public async Task>> GetPlayerZombieSiegeParticipants(Guid playerId, [FromQuery] int last, CancellationToken cancellationToken) { try { var playerZombieSiegeParticipantsResult = await zombieSiegeParticipantRepository.GetPlayerZombieSiegeParticipantsAsync(playerId, last, cancellationToken); return playerZombieSiegeParticipantsResult.IsFailure ? BadRequest(playerZombieSiegeParticipantsResult.Error) : Ok(playerZombieSiegeParticipantsResult.Value); } catch (Exception e) { logger.LogError(e, "{ErrorMessage}", e.Message); return Problem( detail: $"Failed to process {nameof(GetPlayerZombieSiegeParticipants)}", statusCode: StatusCodes.Status500InternalServerError, title: "Internal server error"); } } [HttpPost] public async Task> InsertZombieSiegeParticipants( List createZombieSiegeParticipants, CancellationToken cancellationToken) { try { if (!ModelState.IsValid) return UnprocessableEntity(ModelState); var createResult = await zombieSiegeParticipantRepository.InsertZombieSiegeParticipantsAsync( createZombieSiegeParticipants, cancellationToken); return createResult.IsFailure ? BadRequest(createResult.Error) : Ok(createResult.Value); } catch (Exception e) { logger.LogError(e, "{ErrorMessage}", e.Message); return Problem( detail: $"Failed to process {nameof(InsertZombieSiegeParticipants)}", statusCode: StatusCodes.Status500InternalServerError, title: "Internal server error"); } } [HttpPut("{zombieSiegeParticipantId:guid}")] public async Task> UpdateZombieSiegeParticipant( Guid zombieSiegeParticipantId, UpdateZombieSiegeParticipantDto updateZombieSiegeParticipant, CancellationToken cancellationToken) { try { if (!ModelState.IsValid) return UnprocessableEntity(ModelState); if (zombieSiegeParticipantId != updateZombieSiegeParticipant.Id) return Conflict(ZombieSiegeParticipantErrors.IdConflict); var updateResult = await zombieSiegeParticipantRepository.UpdateZombieSiegeParticipantAsync( updateZombieSiegeParticipant, cancellationToken); return updateResult.IsFailure ? BadRequest(updateResult.Error) : Ok(updateResult.Value); } catch (Exception e) { logger.LogError(e, "{ErrorMessage}", e.Message); return Problem( detail: $"Failed to process {nameof(UpdateZombieSiegeParticipant)}", statusCode: StatusCodes.Status500InternalServerError, title: "Internal server error"); } } } }