PlayerManagement/Api/Controllers/v1/CustomEventParticipantsController.cs
Tomasi - Developing c48d8d8211 v.0.8.3
2025-04-10 15:41:00 +02:00

125 lines
5.1 KiB
C#

using Application.DataTransferObjects.CustomEventParticipant;
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 CustomEventParticipantsController(ICustomEventParticipantRepository customEventParticipantRepository, ILogger<CustomEventParticipantsController> logger) : ControllerBase
{
[HttpGet("{customEventParticipantId:guid}")]
public async Task<ActionResult<CustomEventParticipantDto>> GetCustomEventParticipant(
Guid customEventParticipantId, CancellationToken cancellationToken)
{
try
{
var customEventParticipantResult =
await customEventParticipantRepository.GetCustomEventParticipantAsync(customEventParticipantId,
cancellationToken);
return customEventParticipantResult.IsFailure
? BadRequest(customEventParticipantResult.Error)
: Ok(customEventParticipantResult.Value);
}
catch (Exception e)
{
logger.LogError(e, "{ErrorMessage}", e.Message);
return Problem(
detail: $"Failed to process {nameof(GetCustomEventParticipant)}",
statusCode: StatusCodes.Status500InternalServerError,
title: "Internal server error");
}
}
[HttpGet("Player/{playerId:guid}")]
public async Task<ActionResult<List<CreateCustomEventParticipantDto>>> GetPlayerCustomEventParticipants(
Guid playerId, [FromQuery] int last,
CancellationToken cancellationToken)
{
try
{
var customEventPlayerParticipatedResult =
await customEventParticipantRepository.GetPlayerCustomEventParticipantsAsync(playerId, last,
cancellationToken);
if (customEventPlayerParticipatedResult.IsFailure)
return BadRequest(customEventPlayerParticipatedResult.Error);
return customEventPlayerParticipatedResult.Value.Count > 0
? Ok(customEventPlayerParticipatedResult.Value)
: NoContent();
}
catch (Exception e)
{
logger.LogError(e, "{ErrorMessage}", e.Message);
return Problem(
detail: $"Failed to process {nameof(GetPlayerCustomEventParticipants)}",
statusCode: StatusCodes.Status500InternalServerError,
title: "Internal server error");
}
}
[HttpPost]
public async Task<IActionResult> InsertCustomEventParticipant(
List<CreateCustomEventParticipantDto> createCustomEventParticipants, CancellationToken cancellationToken)
{
try
{
if (!ModelState.IsValid) return UnprocessableEntity(ModelState);
var createResult =
await customEventParticipantRepository.InsertCustomEventParticipantAsync(
createCustomEventParticipants, cancellationToken);
return createResult.IsFailure
? BadRequest(createResult.Error)
: StatusCode(StatusCodes.Status201Created);
}
catch (Exception e)
{
logger.LogError(e, "{ErrorMessage}", e.Message);
return Problem(
detail: $"Failed to process {nameof(InsertCustomEventParticipant)}",
statusCode: StatusCodes.Status500InternalServerError,
title: "Internal server error");
}
}
[HttpPut("{customEventParticipantId:guid}")]
public async Task<ActionResult<CustomEventParticipantDto>> UpdateCustomEventParticipant(
Guid customEventParticipantId,
UpdateCustomEventParticipantDto updateEventParticipantDto, CancellationToken cancellationToken)
{
try
{
if (!ModelState.IsValid) return UnprocessableEntity(ModelState);
if (customEventParticipantId != updateEventParticipantDto.Id)
return Conflict(new Error("", ""));
var updateResult =
await customEventParticipantRepository.UpdateCustomEventParticipantAsync(
updateEventParticipantDto, 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(UpdateCustomEventParticipant)}",
statusCode: StatusCodes.Status500InternalServerError,
title: "Internal server error");
}
}
}
}