TsPozoController.cs 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 TsPozoController : ControllerBase
  16. {
  17. private readonly AquiferContext _context;
  18. public TsPozoController(AquiferContext context)
  19. {
  20. _context = context;
  21. }
  22. // GET: api/TsPozo
  23. [HttpGet]
  24. public async Task<ActionResult<IEnumerable<TsPozo>>> GetTsPozos()
  25. {
  26. return await _context.TsPozos.ToListAsync();
  27. }
  28. // GET: api/TsPozo/5
  29. [HttpGet("{id}")]
  30. //public async Task<ActionResult<TsPozo>> GetTsPozo(uint id)
  31. public TimeSerie[] GetTsPozo(uint id)
  32. {
  33. //var tsPozo = await _context.TsPozos.FindAsync(id);
  34. var tsPozos = _context.TsPozos.Where(x => x.IdPozo == id);
  35. List<TimeSerie> timeSeries = new List<TimeSerie>();
  36. TimeSerie _timeSerie = null;
  37. if (tsPozos == null)
  38. {
  39. return null;
  40. }
  41. foreach(TsPozo tsp in tsPozos)
  42. {
  43. _timeSerie = new TimeSerie();
  44. _timeSerie.Hora = tsp.MarcaTiempo;
  45. _timeSerie.Valor = tsp.Valor;
  46. timeSeries.Add(_timeSerie);
  47. }
  48. return timeSeries.ToArray();
  49. }
  50. // PUT: api/TsPozo/5
  51. // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
  52. [HttpPut("{id}")]
  53. public async Task<IActionResult> PutTsPozo(uint id, TsPozo tsPozo)
  54. {
  55. if (id != tsPozo.Id)
  56. {
  57. return BadRequest();
  58. }
  59. _context.Entry(tsPozo).State = EntityState.Modified;
  60. try
  61. {
  62. await _context.SaveChangesAsync();
  63. }
  64. catch (DbUpdateConcurrencyException)
  65. {
  66. if (!TsPozoExists(id))
  67. {
  68. return NotFound();
  69. }
  70. else
  71. {
  72. throw;
  73. }
  74. }
  75. return NoContent();
  76. }
  77. // POST: api/TsPozo
  78. // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
  79. [HttpPost]
  80. public async Task<ActionResult<TsPozo>> PostTsPozo(TsPozo tsPozo)
  81. {
  82. _context.TsPozos.Add(tsPozo);
  83. try
  84. {
  85. await _context.SaveChangesAsync();
  86. }
  87. catch (DbUpdateException)
  88. {
  89. if (TsPozoExists(tsPozo.Id))
  90. {
  91. return Conflict();
  92. }
  93. else
  94. {
  95. throw;
  96. }
  97. }
  98. return CreatedAtAction("GetTsPozo", new { id = tsPozo.Id }, tsPozo);
  99. }
  100. // DELETE: api/TsPozo/5
  101. [HttpDelete("{id}")]
  102. public async Task<IActionResult> DeleteTsPozo(int id)
  103. {
  104. var tsPozo = await _context.TsPozos.FindAsync(id);
  105. if (tsPozo == null)
  106. {
  107. return NotFound();
  108. }
  109. _context.TsPozos.Remove(tsPozo);
  110. await _context.SaveChangesAsync();
  111. return NoContent();
  112. }
  113. private bool TsPozoExists(uint id)
  114. {
  115. return _context.TsPozos.Any(e => e.Id == id);
  116. }
  117. [NonAction]
  118. public async Task<IActionResult> DeleteTsPozoFromPozo(uint id)
  119. {
  120. List<TsPozo> tsPozos = _context.TsPozos.Where(x => x.IdPozo == id).ToList<TsPozo>();
  121. if (tsPozos == null)
  122. {
  123. return NotFound();
  124. }
  125. tsPozos.ForEach( x =>
  126. _context.TsPozos.Remove(x)
  127. );
  128. await _context.SaveChangesAsync();
  129. return NoContent();
  130. }
  131. }
  132. }