ServiceQueueController.cs 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. public class ServiceQueueController : ControllerBase
  15. {
  16. private readonly AquiferContext _context;
  17. //private TsPozoController tsPozoController;
  18. public ServiceQueueController(AquiferContext context)
  19. {
  20. _context = context;
  21. //tsPozoController = new TsPozoController(context);
  22. }
  23. // GET: api/Pozo
  24. [HttpGet]
  25. public async Task<ActionResult<IEnumerable<ServiceQueue>>> GetQueue()
  26. {
  27. return await _context.ServiceQueue.ToListAsync();
  28. }
  29. // GET: api/Pozo/5
  30. [HttpGet("{id}")]
  31. public async Task<ActionResult<ServiceQueue>> GetQueueItem(uint id)
  32. {
  33. var queueItem = await _context.ServiceQueue.FindAsync(id);
  34. if (queueItem == null)
  35. {
  36. return NotFound();
  37. }
  38. return queueItem;
  39. }
  40. [HttpGet("Running")]
  41. public async Task<bool> ServiceRunning()
  42. {
  43. var queueItem = await _context.ServiceQueue.AnyAsync(x => x.Corriendo != 0);
  44. return queueItem;
  45. }
  46. [HttpGet("Inqueue")]
  47. public async Task<bool> AnyInQueue()
  48. {
  49. return await _context.ServiceQueue.AnyAsync(x => x.Terminado == 0 && x.Corriendo == 0 && x.Error == 0);
  50. }
  51. [HttpGet("Next")]
  52. public ServiceQueue GetNextInQueue()
  53. {
  54. return _context.ServiceQueue.Where(x => x.Terminado == 0 && x.Corriendo == 0 && x.Error == 0).OrderBy(x=> x.Id).FirstOrDefault();
  55. }
  56. [HttpPost("Corriendo/{id}")]
  57. public async Task<int> SetCorriendo(uint id)
  58. {
  59. var serviceQueueItem = await _context.ServiceQueue.FindAsync(id);
  60. serviceQueueItem.Corriendo = 1;
  61. serviceQueueItem.Inicio = DateTime.Now.ToString();
  62. _context.Entry(serviceQueueItem).State = EntityState.Modified;
  63. int result = 0;
  64. try
  65. {
  66. result = await _context.SaveChangesAsync();
  67. }
  68. catch (DbUpdateConcurrencyException)
  69. {
  70. }
  71. return result;
  72. }
  73. [NonAction]
  74. public async Task<ActionResult<ServiceQueue>> SetTerminado(uint id)
  75. {
  76. var serviceQueueItem = await _context.ServiceQueue.FindAsync(id);
  77. serviceQueueItem.Corriendo = 0;
  78. serviceQueueItem.Terminado = 1;
  79. serviceQueueItem.Fin = DateTime.Now.ToString();
  80. _context.Entry(serviceQueueItem).State = EntityState.Modified;
  81. try
  82. {
  83. await _context.SaveChangesAsync();
  84. }
  85. catch (DbUpdateConcurrencyException)
  86. {
  87. if (!ServiceQueueExists(id))
  88. {
  89. return NotFound();
  90. }
  91. else
  92. {
  93. throw;
  94. }
  95. }
  96. return CreatedAtAction("GetQueueItem", new { id = serviceQueueItem.Id }, serviceQueueItem);
  97. }
  98. // POST: api/ServiceQueue
  99. // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
  100. [HttpPost]
  101. public async Task<ActionResult<ServiceQueue>> PostServiceQueue(ServiceQueue serviceQueue)
  102. {
  103. _context.ServiceQueue.Add(serviceQueue);
  104. try
  105. {
  106. await _context.SaveChangesAsync();
  107. }
  108. catch (DbUpdateException)
  109. {
  110. if (ServiceQueueExists(serviceQueue.Id))
  111. {
  112. return Conflict();
  113. }
  114. else
  115. {
  116. throw;
  117. }
  118. }
  119. return CreatedAtAction("GetQueueItem", new { id = serviceQueue.Id }, serviceQueue);
  120. }
  121. // DELETE: api/Pozo/5
  122. [HttpDelete("{id}")]
  123. public async Task<IActionResult> DeleteServiceQueue(uint id)
  124. {
  125. var serviceQueueItem = await _context.ServiceQueue.FindAsync(id);
  126. if (serviceQueueItem == null)
  127. {
  128. return NotFound();
  129. }
  130. _context.ServiceQueue.Remove(serviceQueueItem);
  131. await _context.SaveChangesAsync();
  132. return NoContent();
  133. }
  134. private bool ServiceQueueExists(uint id)
  135. {
  136. return _context.ServiceQueue.Any(e => e.Id == id);
  137. }
  138. }
  139. }