Sin descripción

ProduitController.cs 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. 
  2. using Backend.Domain;
  3. using Backend.Models;
  4. using Mediator;
  5. using MediatR;
  6. using Microsoft.AspNetCore.Http;
  7. using Microsoft.AspNetCore.Mvc;
  8. using Microsoft.Extensions.Logging;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Reflection;
  13. using System.Threading.Tasks;
  14. namespace Backend.API.Controllers
  15. {
  16. [ApiController]
  17. [Route("api/[controller]")]
  18. public class ProduitController : ControllerBase
  19. {
  20. private const string c_getAllProduit = "getAll";
  21. private const string c_postCreateOrUpdate = "createOrUpdate";
  22. private const string c_deleteById = "deleteById/{id}";
  23. private readonly ILogger<ProduitController> _log;
  24. private readonly IMediator _mediator;
  25. public ProduitController(ILogger<ProduitController> log, IMediator mediator)
  26. {
  27. _log = log;
  28. _mediator = mediator;
  29. }
  30. /// <summary>
  31. /// get all type propriete
  32. /// </summary>
  33. /// <returns></returns>
  34. [HttpGet(c_getAllProduit)]
  35. [ProducesResponseType(StatusCodes.Status200OK)]
  36. [ProducesResponseType(StatusCodes.Status500InternalServerError)]
  37. public async Task<IActionResult> GetAllAsync()
  38. {
  39. _log.LogDebug($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - Request to get all produit");
  40. if (_mediator == null)
  41. {
  42. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_HTTP_ERROR_5XX}");
  43. return StatusCode(StatusCodes.Status500InternalServerError, ErrorsConstants.S_HTTP_ERROR_5XX);
  44. }
  45. try
  46. {
  47. var query = new GetProduitListQuery();
  48. using Task<IEnumerable<ProduitResponse>> typesTask = _mediator.Send(query);
  49. await typesTask;
  50. if (typesTask.IsCompleted && typesTask.IsCompletedSuccessfully)
  51. {
  52. return Ok(typesTask.Result);
  53. }
  54. else
  55. {
  56. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_HTTP_ERROR_5XX}");
  57. return StatusCode(StatusCodes.Status500InternalServerError, ErrorsConstants.S_HTTP_ERROR_5XX);
  58. }
  59. }
  60. catch (Exception ex)
  61. {
  62. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - Exception:{ex.Message}");
  63. return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
  64. }
  65. }
  66. /// <summary>
  67. /// create or update type
  68. /// </summary>
  69. /// <param name="cmd"></param>
  70. /// <returns></returns>
  71. [HttpPost(c_postCreateOrUpdate)]
  72. [ProducesResponseType(StatusCodes.Status200OK)]
  73. [ProducesResponseType(StatusCodes.Status400BadRequest)]
  74. [ProducesResponseType(StatusCodes.Status500InternalServerError)]
  75. public async Task<IActionResult> CreateOrUpdateAsync([FromBody] AddProduitCommand cmd)
  76. {
  77. _log.LogDebug($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - Request to create or update Produit");
  78. if (_mediator == null)
  79. {
  80. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_HTTP_ERROR_5XX}");
  81. return StatusCode(StatusCodes.Status500InternalServerError, ErrorsConstants.S_HTTP_ERROR_5XX);
  82. }
  83. if(cmd != null)
  84. {
  85. try
  86. {
  87. using Task<int> typesTask = _mediator.Send(cmd);
  88. await typesTask;
  89. if (typesTask.IsCompleted && typesTask.IsCompletedSuccessfully)
  90. {
  91. return Ok(typesTask.Result);
  92. }
  93. else
  94. {
  95. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_HTTP_ERROR_5XX}");
  96. return StatusCode(StatusCodes.Status500InternalServerError, ErrorsConstants.S_HTTP_ERROR_5XX);
  97. }
  98. }
  99. catch (Exception ex)
  100. {
  101. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - Exception:{ex.Message}");
  102. return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
  103. }
  104. }
  105. else
  106. {
  107. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_HTTP_ERROR_4XX}");
  108. return StatusCode(StatusCodes.Status400BadRequest, ErrorsConstants.S_HTTP_ERROR_4XX);
  109. }
  110. }
  111. /// <summary>
  112. /// delete type by id
  113. /// </summary>
  114. /// <param name="id"></param>
  115. /// <returns></returns>
  116. [HttpDelete(c_deleteById)]
  117. [ProducesResponseType(StatusCodes.Status200OK)]
  118. [ProducesResponseType(StatusCodes.Status400BadRequest)]
  119. [ProducesResponseType(StatusCodes.Status500InternalServerError)]
  120. public async Task<IActionResult> DeleteByIdAsync([FromRoute] Guid id)
  121. {
  122. _log.LogDebug($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - Request to delete type by: {id}");
  123. if (_mediator == null)
  124. {
  125. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_HTTP_ERROR_5XX}");
  126. return StatusCode(StatusCodes.Status500InternalServerError, ErrorsConstants.S_HTTP_ERROR_5XX);
  127. }
  128. if (id != null)
  129. {
  130. try
  131. {
  132. RemoveProduitCommand removeProduitCommand = new RemoveProduitCommand { Id = id };
  133. using Task<int> typesTask = _mediator.Send(removeProduitCommand);
  134. await typesTask;
  135. if (typesTask.IsCompleted && typesTask.IsCompletedSuccessfully)
  136. {
  137. return Ok(typesTask.Result);
  138. }
  139. else
  140. {
  141. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_HTTP_ERROR_5XX}");
  142. return StatusCode(StatusCodes.Status500InternalServerError, ErrorsConstants.S_HTTP_ERROR_5XX);
  143. }
  144. }
  145. catch (Exception ex)
  146. {
  147. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - Exception:{ex.Message}");
  148. return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
  149. }
  150. }
  151. else
  152. {
  153. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_HTTP_ERROR_4XX}");
  154. return StatusCode(StatusCodes.Status400BadRequest, ErrorsConstants.S_HTTP_ERROR_4XX);
  155. }
  156. }
  157. }
  158. }

Powered by TurnKey Linux.