using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using WebApplication3.Models; using WebApplication3.Clases; namespace WebApplication3.Controllers { //[Route("api/queue")] //[ApiController] [ApiExplorerSettings(IgnoreApi = true)] public class ServiceQueueController : ControllerBase { private readonly AquiferContext _context; //private TsPozoController tsPozoController; public ServiceQueueController(AquiferContext context) { _context = context; //tsPozoController = new TsPozoController(context); } // GET: api/Pozo [HttpGet] public async Task>> GetQueue() { return await _context.ServiceQueue.ToListAsync(); } // GET: api/Pozo/5 [HttpGet("{id}")] public async Task> GetQueueItem(uint id) { var queueItem = await _context.ServiceQueue.FindAsync(id); if (queueItem == null) { return NotFound(); } return queueItem; } [HttpGet("Running")] public async Task ServiceRunning() { var queueItem = await _context.ServiceQueue.AnyAsync(x => x.Corriendo != 0); return queueItem; } [HttpGet("Inqueue")] public async Task AnyInQueue() { return await _context.ServiceQueue.AnyAsync(x => x.Terminado == 0 && x.Corriendo == 0 && x.Error == 0); } [HttpGet("Next")] public ServiceQueue GetNextInQueue() { return _context.ServiceQueue.Where(x => x.Terminado == 0 && x.Corriendo == 0 && x.Error == 0).OrderBy(x=> x.Id).FirstOrDefault(); } [HttpPost("Corriendo/{id}")] public async Task SetCorriendo(uint id) { var serviceQueueItem = await _context.ServiceQueue.FindAsync(id); serviceQueueItem.Corriendo = 1; serviceQueueItem.Inicio = DateTime.Now.ToString(); _context.Entry(serviceQueueItem).State = EntityState.Modified; int result = 0; try { result = await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { } return result; } [NonAction] public async Task> SetTerminado(uint id) { var serviceQueueItem = await _context.ServiceQueue.FindAsync(id); serviceQueueItem.Corriendo = 0; serviceQueueItem.Terminado = 1; serviceQueueItem.Fin = DateTime.Now.ToString(); _context.Entry(serviceQueueItem).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ServiceQueueExists(id)) { return NotFound(); } else { throw; } } return CreatedAtAction("GetQueueItem", new { id = serviceQueueItem.Id }, serviceQueueItem); } [NonAction] public async Task> SetError(uint id) { var serviceQueueItem = await _context.ServiceQueue.FindAsync(id); serviceQueueItem.Error = 1; serviceQueueItem.Corriendo = 0; serviceQueueItem.Terminado = 1; serviceQueueItem.Fin = DateTime.Now.ToString(); _context.Entry(serviceQueueItem).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ServiceQueueExists(id)) { return NotFound(); } else { throw; } } return CreatedAtAction("GetQueueItem", new { id = serviceQueueItem.Id }, serviceQueueItem); } // POST: api/ServiceQueue // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 [HttpPost] public async Task> PostServiceQueue(ServiceQueue serviceQueue) { _context.ServiceQueue.Add(serviceQueue); try { await _context.SaveChangesAsync(); } catch (DbUpdateException) { if (ServiceQueueExists(serviceQueue.Id)) { return Conflict(); } else { throw; } } return CreatedAtAction("GetQueueItem", new { id = serviceQueue.Id }, serviceQueue); } // DELETE: api/Pozo/5 [HttpDelete("{id}")] public async Task DeleteServiceQueue(uint id) { var serviceQueueItem = await _context.ServiceQueue.FindAsync(id); if (serviceQueueItem == null) { return NotFound(); } _context.ServiceQueue.Remove(serviceQueueItem); await _context.SaveChangesAsync(); return NoContent(); } private bool ServiceQueueExists(uint id) { return _context.ServiceQueue.Any(e => e.Id == id); } } }