TsPozoController.cs 4.1KB

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