123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- 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<ActionResult<IEnumerable<ServiceQueue>>> GetQueue()
- {
- return await _context.ServiceQueue.ToListAsync();
- }
-
- // GET: api/Pozo/5
- [HttpGet("{id}")]
- public async Task<ActionResult<ServiceQueue>> GetQueueItem(uint id)
- {
- var queueItem = await _context.ServiceQueue.FindAsync(id);
-
- if (queueItem == null)
- {
- return NotFound();
- }
-
- return queueItem;
- }
-
- [HttpGet("Running")]
- public async Task<bool> ServiceRunning()
- {
- var queueItem = await _context.ServiceQueue.AnyAsync(x => x.Corriendo != 0);
-
- return queueItem;
- }
-
- [HttpGet("Inqueue")]
- public async Task<bool> 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<int> 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<ActionResult<ServiceQueue>> 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<ActionResult<ServiceQueue>> 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<ActionResult<ServiceQueue>> 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<IActionResult> 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);
- }
-
- }
- }
|