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/[controller]")] //[ApiController] [ApiExplorerSettings(IgnoreApi = true)] public class PiezometriaController : ControllerBase { private readonly AquiferContext _context; private TsPozoController _tsPozoController; public PiezometriaController(AquiferContext context) { _context = context; _tsPozoController = new TsPozoController(context); } // GET: api/Pozo [HttpGet] public async Task>> GetPozos() { return await _context.Piezometria.ToListAsync(); } // GET: api/Pozo/5 [HttpGet("{id}")] [ProducesResponseType(200)] [ProducesResponseType(404)] public async Task> GetPozo(uint id) { var piezometria = await _context.Piezometria.FindAsync(id); if (piezometria == null) { return NotFound(); } return piezometria; } // PUT: api/Pozo/5 // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 //[HttpPut("{id}")] //public async Task PutPozo(uint id, [FromBody] DatosPozoRecarga datos) //{ // Pozo pozo = _context.Pozos.Find(id); // if (id != pozo.Id) // { // return BadRequest(); // } // pozo.IdAquifero = (datos.Acuifero != null && pozo.IdAquifero != datos.Acuifero) ? datos.Acuifero : pozo.IdAquifero; // pozo.IdSimulacion = (datos.Simulacion != null && pozo.IdSimulacion != datos.Simulacion) ? datos.Simulacion : pozo.IdSimulacion; // pozo.Latitud = (datos.Latitud != null && pozo.Latitud != datos.Latitud) ? datos.Latitud : pozo.Latitud; // pozo.Longitud = (datos.Longitud != null && pozo.Longitud != datos.Longitud) ? datos.Longitud : pozo.Longitud; // pozo.Maximo = (datos.Maximo != null && pozo.Maximo != datos.Maximo) ? datos.Maximo : pozo.Maximo; // pozo.Nombre = (datos.Nombre != null && pozo.Nombre != datos.Nombre) ? datos.Nombre : pozo.Nombre; // _context.Entry(pozo).State = EntityState.Modified; // try // { // await _context.SaveChangesAsync(); // } // catch (DbUpdateConcurrencyException) // { // if (!PiezometriaExists(id)) // { // return NotFound(); // } // else // { // throw; // } // } // return CreatedAtAction("GetPozo", new { id = pozo.Id }, pozo); //} // POST: api/Pozo // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 [HttpPost] [ProducesResponseType(200)] [ProducesResponseType(409)] public async Task> PostPiezometria(Piezometria piezometria) { Logger.WriteLog("Se procede a guardar piezometria"); _context.Piezometria.Add(piezometria); try { Logger.WriteLog("Guardar cambios async"); await _context.SaveChangesAsync(); } catch (DbUpdateException ex) { if (PiezometriaExists(piezometria.Id)) { return Conflict(); } else { Logger.WriteLog("FALLO AL GUARDAR PIEZOMETRIA"); Logger.WriteLog(ex.Message); Logger.WriteLog(ex.InnerException.Message); throw; } } return Ok(piezometria); //return CreatedAtAction("GetPiezometria", new { id = piezometria.Id }, piezometria); } // DELETE: api/Pozo/5 [HttpDelete("{id}")] [ProducesResponseType(404)] [ProducesResponseType(204)] public async Task DeletePiezometria(uint id) { var piezometria = await _context.Piezometria.FindAsync(id); if (piezometria == null) { return NotFound(); } _context.Piezometria.Remove(piezometria); await _context.SaveChangesAsync(); return NoContent(); } private bool PiezometriaExists(uint id) { return _context.Piezometria.Any(e => e.Id == id); } [NonAction] public void GetDimensions(uint idSimulacion, out int rows, out int cols) { Piezometria piezometria = _context.Piezometria.Where(x => x.IdSimulacion == idSimulacion).FirstOrDefault(); rows = piezometria.Rows; cols = piezometria.Columns; } } }