PozoController.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Http;
  6. using Microsoft.AspNetCore.Mvc;
  7. using Microsoft.EntityFrameworkCore;
  8. using WebApplication3.Models;
  9. using WebApplication3.Clases;
  10. namespace WebApplication3.Controllers
  11. {
  12. [Route("api/[controller]")]
  13. [ApiController]
  14. public class PozoController : ControllerBase
  15. {
  16. private readonly AquiferContext _context;
  17. private TsPozoController _tsPozoController;
  18. public PozoController(AquiferContext context)
  19. {
  20. _context = context;
  21. _tsPozoController = new TsPozoController(context);
  22. }
  23. // GET: api/Pozo
  24. [HttpGet]
  25. public async Task<ActionResult<IEnumerable<Pozo>>> GetPozos()
  26. {
  27. return await _context.Pozos.ToListAsync();
  28. }
  29. // GET: api/Pozo/5
  30. [HttpGet("{id}")]
  31. public async Task<ActionResult<DatosPozoRecarga>> GetPozo(uint id)
  32. {
  33. var pozo = await _context.Pozos.FindAsync(id);
  34. if (pozo == null)
  35. {
  36. return NotFound();
  37. }
  38. DatosPozoRecarga datosPozo = new DatosPozoRecarga();
  39. datosPozo.Id = pozo.Id;
  40. datosPozo.Nombre = pozo.Nombre;
  41. datosPozo.Latitud = pozo.Latitud;
  42. datosPozo.Longitud = pozo.Longitud;
  43. datosPozo.Maximo = pozo.Maximo;
  44. datosPozo.Minimo = pozo.Minimo;
  45. datosPozo.Simulacion = pozo.IdSimulacion;
  46. datosPozo.Ts = _tsPozoController.GetTsPozo(pozo.Id);
  47. return datosPozo;
  48. }
  49. // PUT: api/Pozo/5
  50. // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
  51. [HttpPut("{id}")]
  52. public async Task<IActionResult> PutPozo(uint id, [FromBody] DatosPozoRecarga datos)
  53. {
  54. Pozo pozo = _context.Pozos.Find(id);
  55. if (id != pozo.Id)
  56. {
  57. return BadRequest();
  58. }
  59. pozo.IdAquifero = (datos.Acuifero != null && pozo.IdAquifero != datos.Acuifero) ? datos.Acuifero : pozo.IdAquifero;
  60. pozo.IdSimulacion = (datos.Simulacion != null && pozo.IdSimulacion != datos.Simulacion) ? datos.Simulacion : pozo.IdSimulacion;
  61. pozo.Latitud = (datos.Latitud != null && pozo.Latitud != datos.Latitud) ? datos.Latitud : pozo.Latitud;
  62. pozo.Longitud = (datos.Longitud != null && pozo.Longitud != datos.Longitud) ? datos.Longitud : pozo.Longitud;
  63. pozo.Maximo = (datos.Maximo != null && pozo.Maximo != datos.Maximo) ? datos.Maximo : pozo.Maximo;
  64. pozo.Nombre = (datos.Nombre != null && pozo.Nombre != datos.Nombre) ? datos.Nombre : pozo.Nombre;
  65. _context.Entry(pozo).State = EntityState.Modified;
  66. try
  67. {
  68. await _context.SaveChangesAsync();
  69. }
  70. catch (DbUpdateConcurrencyException)
  71. {
  72. if (!PozoExists(id))
  73. {
  74. return NotFound();
  75. }
  76. else
  77. {
  78. throw;
  79. }
  80. }
  81. return CreatedAtAction("GetPozo", new { id = pozo.Id }, pozo);
  82. }
  83. // POST: api/Pozo
  84. // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
  85. [HttpPost]
  86. public async Task<ActionResult<Pozo>> PostPozo([FromBody] DatosPozoRecarga datos)
  87. {
  88. Pozo pozo = new Pozo();
  89. pozo.IdAquifero = datos.Acuifero;
  90. pozo.IdSimulacion = datos.Simulacion;
  91. pozo.Latitud = datos.Latitud;
  92. pozo.Longitud = datos.Longitud;
  93. pozo.Maximo = datos.Maximo;
  94. pozo.Minimo = datos.Minimo;
  95. pozo.Nombre = datos.Nombre;
  96. _context.Pozos.Add(pozo);
  97. try
  98. {
  99. await _context.SaveChangesAsync();
  100. foreach (TimeSerie ts in datos.Ts)
  101. {
  102. TsPozo tsPozo = new TsPozo();
  103. tsPozo.IdPozo = pozo.Id;
  104. tsPozo.MarcaTiempo = ts.Hora;
  105. tsPozo.Valor = ts.Valor;
  106. await _tsPozoController.PostTsPozo(tsPozo);
  107. }
  108. }
  109. catch (DbUpdateException)
  110. {
  111. if (PozoExists(pozo.Id))
  112. {
  113. return Conflict();
  114. }
  115. else
  116. {
  117. throw;
  118. }
  119. }
  120. return CreatedAtAction("GetPozo", new { id = pozo.Id }, pozo);
  121. }
  122. // DELETE: api/Pozo/5
  123. [HttpDelete("{id}")]
  124. public async Task<IActionResult> DeletePozo(uint id)
  125. {
  126. var pozo = await _context.Pozos.FindAsync(id);
  127. if (pozo == null)
  128. {
  129. return NotFound();
  130. }
  131. await _tsPozoController.DeleteTsPozoFromPozo(id);
  132. _context.Pozos.Remove(pozo);
  133. await _context.SaveChangesAsync();
  134. return NoContent();
  135. }
  136. private bool PozoExists(uint id)
  137. {
  138. return _context.Pozos.Any(e => e.Id == id);
  139. }
  140. //api/Pozo/parsea/1
  141. [HttpPost("Parsea/{acuifero}")]
  142. public async Task<ActionResult<IEnumerable<Pozo>>> ParseoFicheroPostman(uint acuifero)
  143. {
  144. Parser wellParser = new Parser();
  145. string file = @"C:\Users\Admin\Desktop\ASD\infoPozos.VMW";
  146. WellData[] wells = wellParser.ParseWellData(file);
  147. foreach(WellData w in wells)
  148. {
  149. Pozo pozo = w.ToPozo(acuifero);
  150. _context.Pozos.Add(pozo);
  151. try
  152. {
  153. await _context.SaveChangesAsync();
  154. foreach (TimeSerieInterno ts in w.Rates)
  155. {
  156. TsPozo tsPozo = new TsPozo();
  157. tsPozo.IdPozo = pozo.Id;
  158. tsPozo.MarcaTiempo = ts.Hora;
  159. tsPozo.Valor = ts.Valor;
  160. await _tsPozoController.PostTsPozo(tsPozo);
  161. }
  162. }
  163. catch (DbUpdateException)
  164. {
  165. if (PozoExists(pozo.Id))
  166. {
  167. return Conflict();
  168. }
  169. else
  170. {
  171. throw;
  172. }
  173. }
  174. }
  175. return await _context.Pozos.ToListAsync();
  176. }
  177. [NonAction]
  178. public async Task<ActionResult<IEnumerable<Pozo>>> ParseoFichero(uint acuifero, string file)
  179. {
  180. var s = acuifero;
  181. Parser wellParser = new Parser();
  182. //string file = @"C:\Users\Admin\Desktop\ASD\infoPozos.VMW";
  183. WellData[] wells = wellParser.ParseWellData(file);
  184. foreach (WellData w in wells)
  185. {
  186. Pozo pozo = w.ToPozo(acuifero);
  187. _context.Pozos.Add(pozo);
  188. try
  189. {
  190. await _context.SaveChangesAsync();
  191. foreach (TimeSerieInterno ts in w.Rates)
  192. {
  193. TsPozo tsPozo = new TsPozo();
  194. tsPozo.IdPozo = pozo.Id;
  195. tsPozo.MarcaTiempo = ts.Hora;
  196. tsPozo.Valor = ts.Valor;
  197. tsPozo.InfoComplementaria = ts.Info;
  198. await _tsPozoController.PostTsPozo(tsPozo);
  199. }
  200. }
  201. catch (DbUpdateException)
  202. {
  203. if (PozoExists(pozo.Id))
  204. {
  205. return Conflict();
  206. }
  207. else
  208. {
  209. throw;
  210. }
  211. }
  212. }
  213. return await _context.Pozos.ToListAsync();
  214. }
  215. [NonAction]
  216. public async Task<ActionResult<IEnumerable<Pozo>>> PostMultipleWells(uint aquiferId,WellData[] wellDatas)
  217. {
  218. foreach (WellData w in wellDatas)
  219. {
  220. Pozo pozo = w.ToPozo(aquiferId);
  221. _context.Pozos.Add(pozo);
  222. try
  223. {
  224. await _context.SaveChangesAsync();
  225. foreach (TimeSerieInterno ts in w.Rates)
  226. {
  227. TsPozo tsPozo = new TsPozo();
  228. tsPozo.IdPozo = pozo.Id;
  229. tsPozo.MarcaTiempo = ts.Hora;
  230. tsPozo.Valor = ts.Valor;
  231. tsPozo.InfoComplementaria = ts.Info;
  232. await _tsPozoController.PostTsPozo(tsPozo);
  233. }
  234. }
  235. catch (DbUpdateException)
  236. {
  237. if (PozoExists(pozo.Id))
  238. {
  239. return Conflict();
  240. }
  241. else
  242. {
  243. throw;
  244. }
  245. }
  246. }
  247. return await _context.Pozos.ToListAsync();
  248. }
  249. [NonAction]
  250. public Pozo[] GetPozosAcuifero(uint acuiferoId)
  251. {
  252. return _context.Pozos.Where(x => x.IdAquifero == acuiferoId).ToArray<Pozo>();
  253. }
  254. [NonAction]
  255. public Pozo[] GetPozosAcuiferoSimulacionInicial(uint acuiferoId)
  256. {
  257. return _context.Pozos.Where(x => x.IdAquifero == acuiferoId && x.IdSimulacion == null).ToArray<Pozo>();
  258. }
  259. [NonAction]
  260. public async void SetPozosSimulacion(uint? acuiferoId, uint? simulacionId)
  261. {
  262. //Obtenemos los pozos del acuifero
  263. Pozo[] pozos = _context.Pozos.Where(x => x.IdAquifero == acuiferoId).ToArray<Pozo>();
  264. foreach(Pozo p in pozos)
  265. {
  266. Pozo pozo = p.Copy(simulacionId);
  267. _context.Pozos.Add(pozo);
  268. }
  269. await _context.SaveChangesAsync();
  270. }
  271. [NonAction]
  272. public List<Pozo> GetPozosSimulacion(uint? simulacionId)
  273. {
  274. List <Pozo> pozos = _context.Pozos.Where(x => x.IdSimulacion == simulacionId).ToList<Pozo>();
  275. return pozos;
  276. }
  277. [NonAction]
  278. public List<TsPozo> GetTsPozoFromPozo(uint? pozo)
  279. {
  280. List<TsPozo> tsPozos = _context.TsPozos.Where(x => x.IdPozo == pozo).ToList<TsPozo>();
  281. return tsPozos;
  282. }
  283. [NonAction]
  284. public async Task<bool> DeletePozoFromSim(uint? idSimulacion)
  285. {
  286. List<Pozo> pozos = _context.Pozos.Where(x => x.IdSimulacion == idSimulacion).ToList();
  287. bool error = false;
  288. try
  289. {
  290. foreach(Pozo p in pozos)
  291. {
  292. await DeletePozo(p.Id);
  293. }
  294. } catch(Exception ex)
  295. {
  296. error = true;
  297. Logger.WriteLog("Fail at deleting well from a simulation (simulationID: " + idSimulacion+")");
  298. Logger.WriteLog(ex.Message);
  299. Logger.WriteLog(ex.InnerException.Message);
  300. }
  301. return error;
  302. }
  303. }
  304. }