No Description

LigneCommandeController.cs 7.0KB

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

Powered by TurnKey Linux.