123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- 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;
- using System.IO.Compression;
- using System.IO;
-
-
- namespace WebApplication3.Controllers
- {
- [Route("api/[controller]")]
- [ApiController]
- public class AcuiferoController : ControllerBase
- {
- private readonly AquiferContext _context;
- private PozoController pozoController;
- private RecargasController recargaController;
-
- public AcuiferoController(AquiferContext context)
- {
- _context = context;
- pozoController = new PozoController(context);
- recargaController = new RecargasController(context);
-
- }
-
- // GET: api/Acuifero
- [HttpGet]
- public async Task<ActionResult<IEnumerable<Acuifero>>> GetAcuiferos()
- {
- return await _context.Acuiferos.ToListAsync();
- }
-
- // GET: api/Acuifero/5
- [HttpGet("{id}")]
- [ProducesResponseType(200)]
- [ProducesResponseType(404)]
- public async Task<ActionResult<Acuifero>> GetAcuifero(uint id)
- {
- var acuifero = await _context.Acuiferos.FindAsync(id);
-
- if (acuifero == null)
- {
- return NotFound();
- }
-
- return acuifero;
- }
-
- [HttpGet("ColorMap/{id}")]
- public async Task<ActionResult<string>> GetRechargeColorMap(uint id)
- {
- Acuifero aquifer = await _context.Acuiferos.FindAsync(id);
-
- return Ok(aquifer.RechargeColorMap);
- }
-
- // PUT: api/Acuifero/5
- // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
- [HttpPut("{id}")]
- [ProducesResponseType(200)]
- [ProducesResponseType(400)]
- [ProducesResponseType(404)]
- public async Task<IActionResult> PutAcuifero(uint id, [FromBody] DatosPostAcuifero datos)
- {
-
- Acuifero acuifero = _context.Acuiferos.Find(id);
- //string pathZip = @"C:\Users\Admin\Desktop\";
- //string pathExtract = @"C:\Users\Admin\Desktop\TestExtract\";
- string pozoFile, recargaFile;
- List<string> files;
-
- if (id != acuifero.Id)
- {
- return BadRequest();
- }
-
- try
- {
- //Se parsea el base 64 que nos llega
- byte[] tempBytes = Convert.FromBase64String(datos.Fichero.Base64);
- //Guardamos en disco el archivo obtenido del base 64
- System.IO.File.WriteAllBytes(@"C:\Users\Admin\Desktop\" + datos.Fichero.Nombre, tempBytes);
- }
- catch (Exception ex)
- {
- return Problem(ex.Message);
- }
-
-
- //Guardamos en bbdd la actualizacion del nombre de fichero
- acuifero.Fichero = datos.Fichero.Nombre;
-
- _context.Entry(acuifero).State = EntityState.Modified;
-
- try
- {
- await _context.SaveChangesAsync();
- Pozo[] pozos = pozoController.GetPozosAcuiferoSimulacionInicial(acuifero.Id);
- Recarga[] recargas = recargaController.GetRecargasAcuiferoSimulacionInicial(acuifero.Id);
- uint simId = GetSimZeroId(acuifero.Id);
-
- foreach (Pozo p in pozos)
- {
- await pozoController.DeletePozo(p.Id);
- }
-
- foreach (Recarga r in recargas)
- {
- await recargaController.DeleteRecarga(r.Id);
- }
-
- //1- Descomprimimos el fichero zip
- ZipFile.ExtractToDirectory(Constants.PathZip + datos.Fichero.Nombre, Constants.PathExtract, true);
- //2- Obtenemos nombre de los ficheros de pozos y recargas
- files = Directory.GetFiles(Constants.PathExtract).ToList<string>();
- pozoFile = files.Find(x => x.Contains(".VMW"));
- recargaFile = files.Find(x => x.Contains(".VMP"));
- //3- Parseamos pozos
- if (System.IO.File.Exists(pozoFile))
- {
- await pozoController.ParseoFichero(acuifero.Id, pozoFile,simId);
- }
- //4- Parseamos recargas
- if (System.IO.File.Exists(recargaFile))
- {
- await recargaController.ParseoFichero(acuifero.Id, recargaFile,simId);
- }
-
- }
- catch (DbUpdateConcurrencyException)
- {
- if (!AcuiferoExists(id))
- {
- return NotFound();
- }
- else
- {
- throw;
- }
- }
-
- return CreatedAtAction("GetAcuifero", new { id = acuifero.Id }, acuifero);
- }
-
- [NonAction]
- public uint GetSimZeroId(uint aquiferId)
- {
- var sim = _context.Simulaciones.Where(x => x.IdAcuifero == aquiferId && x.SimulacionAcuifero == 0).First();
-
- return sim.Id;
- }
-
- [HttpPost("TestBinary")]
- public bool TestBinario([FromBody] DatosPostAcuifero datos)
- {
- //string file = Constants.PathZip + "test.zip";
- //Utilities.BinaryToFile(datos.Fichero.Test, file);
- return true;
- //if (System.IO.File.Exists(file))
- //{
- // return true;
- //}
- //else
- //{
- // return false;
- //}
-
- }
- // POST: api/Acuifero
- // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
- [HttpPost]
- [ProducesResponseType(201)]
- [ProducesResponseType(409)]
- public async Task<ActionResult<Acuifero>> PostAcuifero([FromBody] DatosPostAcuifero datos)
- {
- Acuifero acuifero = new ();
- acuifero.Nombre = datos.Nombre;
- //string pathZip = @"C:\Users\Admin\Desktop\";
- //string pathExtract = @"C:\Users\Admin\Desktop\TestExtract\";
- string pozoFile, recargaFile;
- List<string> files;
- Parser parser = new();
-
- try
- {
- byte[] tempBytes = Convert.FromBase64String(datos.Fichero.Base64);
- System.IO.File.WriteAllBytes(Constants.PathZip + datos.Fichero.Nombre, tempBytes);
-
- }
- catch (Exception ex)
- {
- return Problem(ex.Message);
- }
-
- acuifero.Fichero = datos.Fichero.Nombre;
-
- try
- {
-
- //Como se crea el acuifero, no tiene simulacion 0, por lo que se insertan los datos de los pozos y recargas
- //1- Descomprimimos el fichero zip
- ZipFile.ExtractToDirectory(Constants.PathZip + datos.Fichero.Nombre, Constants.PathExtract, true);
- files = Directory.GetFiles(Constants.PathExtract+datos.Nombre).ToList<string>();
- //1.2 - Obtencion de coordenadas de origen (en el mundo) del modelo
- string geoFile = files.Find(x => x.Contains(".vmf"));
- Logger.WriteLog("geofile " + geoFile);
- string world_origin_x, world_origin_y, angle, length, height;
-
- parser.GetGeolocalizationFromFile(geoFile, out world_origin_x, out world_origin_y, out angle, out length, out height);
-
- acuifero.WorldOriginX = world_origin_x;
- acuifero.WorldOriginY = world_origin_y;
- acuifero.Angle = angle;
- acuifero.Length = length;
- acuifero.Height = height;
-
- _context.Acuiferos.Add(acuifero);
- await _context.SaveChangesAsync();
-
- Simulacion sim = new ();
- sim.IdAcuifero = acuifero.Id;
- sim.Nombre = "Sim_0";
- sim.SimulacionAcuifero = 0;
- await PostSimulationZero(sim);
-
- //2- Obtenemos nombre de los ficheros de pozos y recargas
- pozoFile = files.Find(x => x.Contains(".VMW") || x.Contains(".vmw"));
- recargaFile = files.Find(x => x.Contains(".VMP") || x.Contains(".vmp"));
-
- //3- Parseamos pozos
- if (System.IO.File.Exists(pozoFile))
- {
- await pozoController.ParseoFichero(acuifero.Id, pozoFile,sim.Id);
- }
- //4- Parseamos recargas
- if (System.IO.File.Exists(recargaFile))
- {
- await recargaController.ParseoFichero(acuifero.Id, recargaFile, sim.Id);
- }
- }
- catch (DbUpdateException)
- {
- if (AcuiferoExists(acuifero.Id))
- {
- return Conflict();
- }
- else
- {
- throw;
- }
- }
-
- return CreatedAtAction("GetAcuifero", new { id = acuifero.Id }, acuifero);
- }
-
- [NonAction]
- public async Task<ActionResult<Simulacion>> PostSimulationZero(Simulacion simulacion)
- {
- _context.Simulaciones.Add(simulacion);
- await _context.SaveChangesAsync();
-
- return CreatedAtAction("GetSimulacion", new { id = simulacion.Id }, simulacion);
- }
-
- // DELETE: api/Acuifero/5
- [HttpDelete("{id}")]
- [ProducesResponseType(404)]
- [ProducesResponseType(209)]
- public async Task<IActionResult> DeleteAcuifero(int id)
- {
- var acuifero = await _context.Acuiferos.FindAsync(id);
- if (acuifero == null)
- {
- return NotFound();
- }
-
- _context.Acuiferos.Remove(acuifero);
- await _context.SaveChangesAsync();
-
- return NoContent();
- }
-
- private bool AcuiferoExists(uint id)
- {
- return _context.Acuiferos.Any(e => e.Id == id);
- }
-
- [NonAction]
- public string GetFicheroZip(uint id)
- {
- var acuifero = _context.Acuiferos.Find(id);
-
- if (acuifero != null)
- {
- return acuifero.Fichero;
- }
-
- return string.Empty;
- }
-
- }
- }
|