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] public class PozoController : ControllerBase { private readonly AquiferContext _context; private TsPozoController _tsPozoController; public PozoController(AquiferContext context) { _context = context; _tsPozoController = new TsPozoController(context); } // GET: api/Pozo [HttpGet] public async Task>> GetPozos() { var pozos = await _context.Pozos.ToListAsync(); List result = new(); foreach(Pozo p in pozos) { DatosPozoRecarga datosPozo = new DatosPozoRecarga(); datosPozo.Id = p.Id; datosPozo.Nombre = p.Nombre; datosPozo.Latitud = p.Latitud; datosPozo.Longitud = p.Longitud; datosPozo.Maximo = p.Maximo; datosPozo.Minimo = p.Minimo; datosPozo.Simulacion = p.IdSimulacion; datosPozo.Ts = _tsPozoController.GetTsPozo(p.Id); result.Add(datosPozo); } return result; } // GET: api/Pozo/5 [HttpGet("{id}")] [ProducesResponseType(200)] [ProducesResponseType(404)] public async Task> GetPozo(uint id) { var pozo = await _context.Pozos.FindAsync(id); if (pozo == null) { return NotFound(); } DatosPozoRecarga datosPozo = new DatosPozoRecarga(); datosPozo.Id = pozo.Id; datosPozo.Nombre = pozo.Nombre; datosPozo.Latitud = pozo.Latitud; datosPozo.Longitud = pozo.Longitud; datosPozo.Maximo = pozo.Maximo; datosPozo.Minimo = pozo.Minimo; datosPozo.Simulacion = pozo.IdSimulacion; datosPozo.Ts = _tsPozoController.GetTsPozo(pozo.Id); return datosPozo; } [HttpGet("Simulacion/{id}")] public async Task>> GetPozosFromSimulation(uint id) { var pozos = await _context.Pozos.Where(x => x.IdSimulacion == id).ToListAsync(); List result = new(); foreach (Pozo p in pozos) { DatosPozoRecarga datosPozo = new DatosPozoRecarga(); datosPozo.Id = p.Id; datosPozo.Nombre = p.Nombre; datosPozo.Latitud = p.Latitud; datosPozo.Longitud = p.Longitud; datosPozo.Maximo = p.Maximo; datosPozo.Minimo = p.Minimo; datosPozo.Simulacion = p.IdSimulacion; datosPozo.Ts = _tsPozoController.GetTsPozo(p.Id); result.Add(datosPozo); } return result; } // PUT: api/Pozo/5 // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 [HttpPut("{id}")] [ProducesResponseType(201)] [ProducesResponseType(400)] [ProducesResponseType(404)] 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 (!PozoExists(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(201)] [ProducesResponseType(409)] public async Task> PostPozo([FromBody] DatosPozoRecarga datos) { Pozo pozo = new Pozo(); pozo.IdAquifero = datos.Acuifero; pozo.IdSimulacion = datos.Simulacion; pozo.Latitud = datos.Latitud; pozo.Longitud = datos.Longitud; pozo.Maximo = datos.Maximo; pozo.Minimo = datos.Minimo; pozo.Nombre = datos.Nombre; _context.Pozos.Add(pozo); try { await _context.SaveChangesAsync(); foreach (TimeSerie ts in datos.Ts) { TsPozo tsPozo = new TsPozo(); tsPozo.IdPozo = pozo.Id; tsPozo.MarcaTiempo = ts.Hora; tsPozo.Valor = ts.Valor; await _tsPozoController.PostTsPozo(tsPozo); } } catch (DbUpdateException) { if (PozoExists(pozo.Id)) { return Conflict(); } else { throw; } } return CreatedAtAction("GetPozo", new { id = pozo.Id }, pozo); } // DELETE: api/Pozo/5 [HttpDelete("{id}")] [ProducesResponseType(201)] [ProducesResponseType(404)] public async Task DeletePozo(uint id) { var pozo = await _context.Pozos.FindAsync(id); if (pozo == null) { return NotFound(); } await _tsPozoController.DeleteTsPozoFromPozo(id); _context.Pozos.Remove(pozo); await _context.SaveChangesAsync(); return NoContent(); } private bool PozoExists(uint id) { return _context.Pozos.Any(e => e.Id == id); } [NonAction] public async Task>> ParseoFichero(uint acuifero, string file,uint simId) { var s = acuifero; Parser wellParser = new Parser(); //string file = @"C:\Users\Admin\Desktop\ASD\infoPozos.VMW"; WellData[] wells = wellParser.ParseWellData(file); foreach (WellData w in wells) { Pozo pozo = w.ToPozo(acuifero, simId); _context.Pozos.Add(pozo); try { await _context.SaveChangesAsync(); foreach (TimeSerieInterno ts in w.Rates) { TsPozo tsPozo = new TsPozo(); tsPozo.IdPozo = pozo.Id; tsPozo.MarcaTiempo = ts.Hora; tsPozo.MarcaTiempoEnd = ts.HoraEnd; tsPozo.Valor = ts.Valor; tsPozo.InfoComplementaria = ts.Info; await _tsPozoController.PostTsPozo(tsPozo); } } catch (DbUpdateException) { if (PozoExists(pozo.Id)) { return Conflict(); } else { throw; } } } return await _context.Pozos.ToListAsync(); } [NonAction] public async Task>> PostMultipleWells(uint aquiferId,WellData[] wellDatas, uint simulationId) { foreach (WellData w in wellDatas) { Pozo pozo = w.ToPozo(aquiferId, simulationId); _context.Pozos.Add(pozo); try { await _context.SaveChangesAsync(); foreach (TimeSerieInterno ts in w.Rates) { TsPozo tsPozo = new TsPozo(); tsPozo.IdPozo = pozo.Id; tsPozo.MarcaTiempo = ts.Hora; tsPozo.Valor = ts.Valor; tsPozo.InfoComplementaria = ts.Info; await _tsPozoController.PostTsPozo(tsPozo); } } catch (DbUpdateException) { if (PozoExists(pozo.Id)) { return Conflict(); } else { throw; } } } return await _context.Pozos.ToListAsync(); } [NonAction] public Pozo[] GetPozosAcuifero(uint acuiferoId) { return _context.Pozos.Where(x => x.IdAquifero == acuiferoId).ToArray(); } [NonAction] public Pozo[] GetPozosAcuiferoSimulacionInicial(uint acuiferoId) { return _context.Pozos.Where(x => x.IdAquifero == acuiferoId && x.IdSimulacion == null).ToArray(); } [NonAction] public async void SetPozosSimulacion(uint? acuiferoId, uint? simulacionId) { //Obtenemos los pozos del acuifero Pozo[] pozos = _context.Pozos.Where(x => x.IdAquifero == acuiferoId && x.IdSimulacion == null).ToArray(); foreach(Pozo p in pozos) { Pozo pozo = p.Copy(simulacionId); _context.Pozos.Add(pozo); } await _context.SaveChangesAsync(); } [NonAction] public List GetPozosSimulacion(uint? simulacionId) { List pozos = _context.Pozos.Where(x => x.IdSimulacion == simulacionId).ToList(); return pozos; } [NonAction] public List GetTsPozoFromPozo(uint? pozo) { List tsPozos = _context.TsPozos.Where(x => x.IdPozo == pozo).ToList(); return tsPozos; } [NonAction] public async Task DeletePozoFromSim(uint? idSimulacion) { List pozos = _context.Pozos.Where(x => x.IdSimulacion == idSimulacion).ToList(); bool error = false; try { foreach(Pozo p in pozos) { await DeletePozo(p.Id); } } catch(Exception ex) { error = true; Logger.WriteLog("Fail at deleting well from a simulation (simulationID: " + idSimulacion+")"); Logger.WriteLog(ex.Message); Logger.WriteLog(ex.InnerException.Message); } return error; } } }