PiezometriaController.cs 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. [ApiExplorerSettings(IgnoreApi = true)]
  15. public class PiezometriaController : ControllerBase
  16. {
  17. private readonly AquiferContext _context;
  18. private TsPozoController _tsPozoController;
  19. public PiezometriaController(AquiferContext context)
  20. {
  21. _context = context;
  22. _tsPozoController = new TsPozoController(context);
  23. }
  24. // GET: api/Pozo
  25. [HttpGet]
  26. public async Task<ActionResult<IEnumerable<Piezometria>>> GetPozos()
  27. {
  28. return await _context.Piezometria.ToListAsync();
  29. }
  30. // GET: api/Pozo/5
  31. [HttpGet("{id}")]
  32. [ProducesResponseType(200)]
  33. [ProducesResponseType(404)]
  34. public async Task<ActionResult<Piezometria>> GetPozo(uint id)
  35. {
  36. var piezometria = await _context.Piezometria.FindAsync(id);
  37. if (piezometria == null)
  38. {
  39. return NotFound();
  40. }
  41. return piezometria;
  42. }
  43. // PUT: api/Pozo/5
  44. // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
  45. //[HttpPut("{id}")]
  46. //public async Task<IActionResult> PutPozo(uint id, [FromBody] DatosPozoRecarga datos)
  47. //{
  48. // Pozo pozo = _context.Pozos.Find(id);
  49. // if (id != pozo.Id)
  50. // {
  51. // return BadRequest();
  52. // }
  53. // pozo.IdAquifero = (datos.Acuifero != null && pozo.IdAquifero != datos.Acuifero) ? datos.Acuifero : pozo.IdAquifero;
  54. // pozo.IdSimulacion = (datos.Simulacion != null && pozo.IdSimulacion != datos.Simulacion) ? datos.Simulacion : pozo.IdSimulacion;
  55. // pozo.Latitud = (datos.Latitud != null && pozo.Latitud != datos.Latitud) ? datos.Latitud : pozo.Latitud;
  56. // pozo.Longitud = (datos.Longitud != null && pozo.Longitud != datos.Longitud) ? datos.Longitud : pozo.Longitud;
  57. // pozo.Maximo = (datos.Maximo != null && pozo.Maximo != datos.Maximo) ? datos.Maximo : pozo.Maximo;
  58. // pozo.Nombre = (datos.Nombre != null && pozo.Nombre != datos.Nombre) ? datos.Nombre : pozo.Nombre;
  59. // _context.Entry(pozo).State = EntityState.Modified;
  60. // try
  61. // {
  62. // await _context.SaveChangesAsync();
  63. // }
  64. // catch (DbUpdateConcurrencyException)
  65. // {
  66. // if (!PiezometriaExists(id))
  67. // {
  68. // return NotFound();
  69. // }
  70. // else
  71. // {
  72. // throw;
  73. // }
  74. // }
  75. // return CreatedAtAction("GetPozo", new { id = pozo.Id }, pozo);
  76. //}
  77. // POST: api/Pozo
  78. // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
  79. [HttpPost]
  80. [ProducesResponseType(200)]
  81. [ProducesResponseType(409)]
  82. public async Task<ActionResult<Piezometria>> PostPiezometria(Piezometria piezometria)
  83. {
  84. Logger.WriteLog("Se procede a guardar piezometria");
  85. _context.Piezometria.Add(piezometria);
  86. try
  87. {
  88. Logger.WriteLog("Guardar cambios async");
  89. await _context.SaveChangesAsync();
  90. }
  91. catch (DbUpdateException ex)
  92. {
  93. if (PiezometriaExists(piezometria.Id))
  94. {
  95. return Conflict();
  96. }
  97. else
  98. {
  99. Logger.WriteLog("FALLO AL GUARDAR PIEZOMETRIA");
  100. Logger.WriteLog(ex.Message);
  101. Logger.WriteLog(ex.InnerException.Message);
  102. throw;
  103. }
  104. }
  105. return Ok(piezometria);
  106. //return CreatedAtAction("GetPiezometria", new { id = piezometria.Id }, piezometria);
  107. }
  108. // DELETE: api/Pozo/5
  109. [HttpDelete("{id}")]
  110. [ProducesResponseType(404)]
  111. [ProducesResponseType(204)]
  112. public async Task<IActionResult> DeletePiezometria(uint id)
  113. {
  114. var piezometria = await _context.Piezometria.FindAsync(id);
  115. if (piezometria == null)
  116. {
  117. return NotFound();
  118. }
  119. _context.Piezometria.Remove(piezometria);
  120. await _context.SaveChangesAsync();
  121. return NoContent();
  122. }
  123. private bool PiezometriaExists(uint id)
  124. {
  125. return _context.Piezometria.Any(e => e.Id == id);
  126. }
  127. [NonAction]
  128. public void GetDimensions(uint idSimulacion, out int rows, out int cols)
  129. {
  130. Piezometria piezometria = _context.Piezometria.Where(x => x.IdSimulacion == idSimulacion).FirstOrDefault();
  131. rows = piezometria.Rows;
  132. cols = piezometria.Columns;
  133. }
  134. }
  135. }