ServiceQueueController.cs 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Http;
  6. using Microsoft.AspNetCore.Mvc;
  7. using Microsoft.EntityFrameworkCore;
  8. using WebApplication3.Models;
  9. using WebApplication3.Clases;
  10. namespace WebApplication3.Controllers
  11. {
  12. //[Route("api/queue")]
  13. //[ApiController]
  14. [ApiExplorerSettings(IgnoreApi = true)]
  15. public class ServiceQueueController : ControllerBase
  16. {
  17. private readonly AquiferContext _context;
  18. //private TsPozoController tsPozoController;
  19. public ServiceQueueController(AquiferContext context)
  20. {
  21. _context = context;
  22. //tsPozoController = new TsPozoController(context);
  23. }
  24. // GET: api/Pozo
  25. [HttpGet]
  26. public async Task<ActionResult<IEnumerable<ServiceQueue>>> GetQueue()
  27. {
  28. return await _context.ServiceQueue.ToListAsync();
  29. }
  30. // GET: api/Pozo/5
  31. [HttpGet("{id}")]
  32. public async Task<ActionResult<ServiceQueue>> GetQueueItem(uint id)
  33. {
  34. var queueItem = await _context.ServiceQueue.FindAsync(id);
  35. if (queueItem == null)
  36. {
  37. return NotFound();
  38. }
  39. return queueItem;
  40. }
  41. [HttpGet("Running")]
  42. public async Task<bool> ServiceRunning()
  43. {
  44. var queueItem = await _context.ServiceQueue.AnyAsync(x => x.Corriendo != 0);
  45. return queueItem;
  46. }
  47. [HttpGet("Inqueue")]
  48. public async Task<bool> AnyInQueue()
  49. {
  50. return await _context.ServiceQueue.AnyAsync(x => x.Terminado == 0 && x.Corriendo == 0 && x.Error == 0);
  51. }
  52. [HttpGet("Next")]
  53. public ServiceQueue GetNextInQueue()
  54. {
  55. return _context.ServiceQueue.Where(x => x.Terminado == 0 && x.Corriendo == 0 && x.Error == 0).OrderBy(x=> x.Id).FirstOrDefault();
  56. }
  57. [HttpPost("Corriendo/{id}")]
  58. public async Task<int> SetCorriendo(uint id)
  59. {
  60. var serviceQueueItem = await _context.ServiceQueue.FindAsync(id);
  61. serviceQueueItem.Corriendo = 1;
  62. serviceQueueItem.Inicio = DateTime.Now.ToString();
  63. _context.Entry(serviceQueueItem).State = EntityState.Modified;
  64. int result = 0;
  65. try
  66. {
  67. result = await _context.SaveChangesAsync();
  68. }
  69. catch (DbUpdateConcurrencyException)
  70. {
  71. }
  72. return result;
  73. }
  74. [NonAction]
  75. public async Task<ActionResult<ServiceQueue>> SetTerminado(uint id)
  76. {
  77. var serviceQueueItem = await _context.ServiceQueue.FindAsync(id);
  78. serviceQueueItem.Corriendo = 0;
  79. serviceQueueItem.Terminado = 1;
  80. serviceQueueItem.Fin = DateTime.Now.ToString();
  81. _context.Entry(serviceQueueItem).State = EntityState.Modified;
  82. try
  83. {
  84. await _context.SaveChangesAsync();
  85. }
  86. catch (DbUpdateConcurrencyException)
  87. {
  88. if (!ServiceQueueExists(id))
  89. {
  90. return NotFound();
  91. }
  92. else
  93. {
  94. throw;
  95. }
  96. }
  97. return CreatedAtAction("GetQueueItem", new { id = serviceQueueItem.Id }, serviceQueueItem);
  98. }
  99. [NonAction]
  100. public async Task<ActionResult<ServiceQueue>> SetError(uint id)
  101. {
  102. var serviceQueueItem = await _context.ServiceQueue.FindAsync(id);
  103. serviceQueueItem.Error = 1;
  104. serviceQueueItem.Corriendo = 0;
  105. serviceQueueItem.Terminado = 1;
  106. serviceQueueItem.Fin = DateTime.Now.ToString();
  107. _context.Entry(serviceQueueItem).State = EntityState.Modified;
  108. try
  109. {
  110. await _context.SaveChangesAsync();
  111. }
  112. catch (DbUpdateConcurrencyException)
  113. {
  114. if (!ServiceQueueExists(id))
  115. {
  116. return NotFound();
  117. }
  118. else
  119. {
  120. throw;
  121. }
  122. }
  123. return CreatedAtAction("GetQueueItem", new { id = serviceQueueItem.Id }, serviceQueueItem);
  124. }
  125. // POST: api/ServiceQueue
  126. // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
  127. [HttpPost]
  128. public async Task<ActionResult<ServiceQueue>> PostServiceQueue(ServiceQueue serviceQueue)
  129. {
  130. _context.ServiceQueue.Add(serviceQueue);
  131. try
  132. {
  133. await _context.SaveChangesAsync();
  134. }
  135. catch (DbUpdateException)
  136. {
  137. if (ServiceQueueExists(serviceQueue.Id))
  138. {
  139. return Conflict();
  140. }
  141. else
  142. {
  143. throw;
  144. }
  145. }
  146. return CreatedAtAction("GetQueueItem", new { id = serviceQueue.Id }, serviceQueue);
  147. }
  148. // DELETE: api/Pozo/5
  149. [HttpDelete("{id}")]
  150. public async Task<IActionResult> DeleteServiceQueue(uint id)
  151. {
  152. var serviceQueueItem = await _context.ServiceQueue.FindAsync(id);
  153. if (serviceQueueItem == null)
  154. {
  155. return NotFound();
  156. }
  157. _context.ServiceQueue.Remove(serviceQueueItem);
  158. await _context.SaveChangesAsync();
  159. return NoContent();
  160. }
  161. private bool ServiceQueueExists(uint id)
  162. {
  163. return _context.ServiceQueue.Any(e => e.Id == id);
  164. }
  165. }
  166. }