using System; using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Serilog; using LCU.Api.Internal.MaidBotApi.Exceptions; using LCU.Api.Internal.MaidBotApi.Services; using LCU.Api.Internal.MaidBotLib.Models; using LCU.Library.Internal.Exceptions; namespace LCU.API.Internal.MaidBotApi.Controllers; [ApiController] [Route("api/[controller]")] public class LoggingMatrixController : ControllerBase { private readonly ILoggingMatrixService _loggingMatrixService; public LoggingMatrixController(ILoggingMatrixService loggingMatrixService) { _loggingMatrixService = loggingMatrixService; } [HttpGet(Name = "GetAllLoggingMatrixes")] [ProducesResponseType(typeof(IEnumerable), 200)] [ProducesResponseType(500)] public async Task Get() { try { var result = await _loggingMatrixService.GetAsync(); return Ok(result); } catch (ServiceException exception) { return StatusCode(StatusCodes.Status500InternalServerError, exception.Message); } catch (Exception exception) { Log.Error(exception, "Unexpected Exception"); return StatusCode(StatusCodes.Status500InternalServerError); } } [HttpPost] [ProducesResponseType(typeof(LoggingMatrix), 201)] [ProducesResponseType(typeof(List), 400)] [ProducesResponseType(500)] public async Task Post([FromBody] LoggingMatrix loggingMatrix) { try { var result = await _loggingMatrixService.SaveAsync(loggingMatrix); if (result == null) { Log.Error($"Result is null could not create Logging Matrix"); return StatusCode((StatusCodes.Status400BadRequest), "Could not create Logging Matrix."); } return StatusCode(StatusCodes.Status201Created, result); } catch (ServiceException exception) { return StatusCode(StatusCodes.Status500InternalServerError, exception.Message); } catch (Exception exception) { Log.Error(exception, "Unexpected Exception"); return StatusCode(StatusCodes.Status500InternalServerError); } } [HttpPut] [ProducesResponseType(typeof(LoggingMatrix), 200)] [ProducesResponseType(400)] [ProducesResponseType(404)] [ProducesResponseType(500)] public async Task Put([FromBody] LoggingMatrix loggingMatrix) { try { if (loggingMatrix.LoggingMatrixId <= 0) { return BadRequest("Invalid ID"); } var result = await _loggingMatrixService.UpdateAsync(loggingMatrix); if(result == null) { return NotFound($"The LoggingMatrix with LoggingMatrixId: {loggingMatrix.LoggingMatrixId} " + "could not be found"); } return Ok(result); } catch (ServiceException exception) { return StatusCode(StatusCodes.Status500InternalServerError, exception.Message); } catch (Exception exception) { Log.Error(exception, "Unexpected Exception"); return StatusCode(StatusCodes.Status500InternalServerError); } } }