123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- 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<ActionResult<IEnumerable<DatosPozoRecarga>>> GetPozos()
- {
- var pozos = await _context.Pozos.ToListAsync();
- List<DatosPozoRecarga> 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<ActionResult<DatosPozoRecarga>> 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<ActionResult<IEnumerable<DatosPozoRecarga>>> GetPozosFromSimulation(uint id)
- {
- var pozos = await _context.Pozos.Where(x => x.IdSimulacion == id).ToListAsync();
-
- List<DatosPozoRecarga> 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<IActionResult> 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<ActionResult<Pozo>> 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<IActionResult> 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<ActionResult<IEnumerable<Pozo>>> 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<ActionResult<IEnumerable<Pozo>>> 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<Pozo>();
- }
-
- [NonAction]
- public Pozo[] GetPozosAcuiferoSimulacionInicial(uint acuiferoId)
- {
- return _context.Pozos.Where(x => x.IdAquifero == acuiferoId && x.IdSimulacion == null).ToArray<Pozo>();
- }
-
- [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<Pozo>();
-
- foreach(Pozo p in pozos)
- {
- Pozo pozo = p.Copy(simulacionId);
-
- _context.Pozos.Add(pozo);
- }
- await _context.SaveChangesAsync();
-
-
- }
-
- [NonAction]
- public List<Pozo> GetPozosSimulacion(uint? simulacionId)
- {
- List <Pozo> pozos = _context.Pozos.Where(x => x.IdSimulacion == simulacionId).ToList<Pozo>();
-
- return pozos;
- }
-
- [NonAction]
- public List<TsPozo> GetTsPozoFromPozo(uint? pozo)
- {
- List<TsPozo> tsPozos = _context.TsPozos.Where(x => x.IdPozo == pozo).ToList<TsPozo>();
-
- return tsPozos;
- }
-
- [NonAction]
- public async Task<bool> DeletePozoFromSim(uint? idSimulacion)
- {
- List<Pozo> 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;
- }
- }
- }
|