using System; using System.Threading.Tasks; using System.Collections.Generic; using Serilog; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Http; using LCU.Api.Internal.ArchiverApi.Services; using LCU.Api.Internal.ArchiverApi.Extensions; using LCU.Library.Internal.ArchiverApiModel.Entities; using LCU.Library.Internal.Exceptions; namespace LCU.Api.Internal.ArchiverApi.Controllers; [ApiController] [Route("[controller]")] public class ApplicationMappingController : ControllerBase { private readonly IApplicationMappingService _applicationMappingService; public ApplicationMappingController(IApplicationMappingService applicationMappingService) { _applicationMappingService = applicationMappingService; } [HttpGet(Name = "GetAllJobs")] [ProducesResponseType(typeof(IEnumerable), 200)] [ProducesResponseType(500)] public async Task Get() { try { var result = await _applicationMappingService.GetAsync(); return Ok(result); } catch (ServiceException) { return StatusCode(StatusCodes.Status500InternalServerError); } catch (Exception exception) { Log.Error(exception, "Unexpected Exception"); return StatusCode(StatusCodes.Status500InternalServerError); } } [HttpGet("{id:int}", Name = "GetById")] [ProducesResponseType(201)] [ProducesResponseType(typeof(ApplicationMapping), 400)] [ProducesResponseType(500)] public async Task GetById(int id) { try { var result = await _applicationMappingService.GetByIdAsync(id); return Ok(result); } catch (ServiceException) { return StatusCode(StatusCodes.Status500InternalServerError); } catch (Exception exception) { Log.Error(exception, "Unexpected Exception"); return StatusCode(StatusCodes.Status500InternalServerError); } } [HttpPost] [ProducesResponseType(201)] [ProducesResponseType(typeof(ApplicationMapping), 400)] [ProducesResponseType(500)] public async Task Post([FromBody] ApplicationMapping applicationMapping) { try { if (!ModelState.IsValid) { return BadRequest(ModelState.GetErrorMessages()); } var result = await _applicationMappingService.SaveAsync(applicationMapping); return Ok(result); } catch (ServiceException) { return StatusCode(StatusCodes.Status500InternalServerError); } catch (Exception exception) { Log.Error(exception, "Unexpected Exception"); return StatusCode(StatusCodes.Status500InternalServerError); } } [HttpPut("Update", Name = "Update")] [ProducesResponseType(typeof(ApplicationMapping), 200)] [ProducesResponseType(500)] public IActionResult Update([FromBody] ApplicationMapping applicationMapping) { try { var result = _applicationMappingService.Update(applicationMapping); return Ok(result); } catch (ServiceException) { return StatusCode(StatusCodes.Status500InternalServerError); } catch (Exception exception) { Log.Error(exception, "Unexpected Exception"); return StatusCode(StatusCodes.Status500InternalServerError); } } [HttpDelete()] [ProducesResponseType(typeof(ApplicationMapping), 200)] [ProducesResponseType(500)] public IActionResult Delete(int jobId) { try { if (jobId <= 0) { return BadRequest("Invalid Id"); } _applicationMappingService.DeleteAsync(jobId); return Ok(); } catch (ServiceException) { return StatusCode(StatusCodes.Status500InternalServerError); } catch (Exception exception) { Log.Error(exception, "Unexpected Exception"); return StatusCode(StatusCodes.Status500InternalServerError); } } }