TsPiezometerController.cs 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 TsPiezometerController: ControllerBase
  16. {
  17. private readonly AquiferContext _context;
  18. public TsPiezometerController(AquiferContext context)
  19. {
  20. _context = context;
  21. }
  22. // GET: api/TsPiezometer
  23. [HttpGet]
  24. public async Task<ActionResult<IEnumerable<TsPiezometer>>> GetTsPiezometers()
  25. {
  26. return await _context.TsPiezometers.ToListAsync();
  27. }
  28. // GET: api/TsPiezometer/5
  29. [HttpGet("{id}")]
  30. //public async Task<ActionResult<TsPozo>> GetTsPozo(uint id)
  31. public TimeSerie[] GetTsPiezometer(uint id)
  32. {
  33. //var tsPozo = await _context.TsPozos.FindAsync(id);
  34. var tsPiezometers = _context.TsPiezometers.Where(x => x.PiezometerId == id);
  35. List<TimeSerie> timeSeries = new ();
  36. TimeSerie timeSerie = null;
  37. if (tsPiezometers == null)
  38. {
  39. return null;
  40. }
  41. foreach(TsPiezometer tsp in tsPiezometers)
  42. {
  43. timeSerie = new ();
  44. timeSerie.Hora = tsp.Day;
  45. timeSerie.Valor = tsp.Quote;
  46. timeSeries.Add(timeSerie);
  47. }
  48. return timeSeries.ToArray();
  49. }
  50. // PUT: api/TsPiezometer/5
  51. // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
  52. [HttpPut("{id}")]
  53. public async Task<IActionResult> PutTsPiezometer(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 (!TsPiezometerExists(id))
  67. {
  68. return NotFound();
  69. }
  70. else
  71. {
  72. throw;
  73. }
  74. }
  75. return NoContent();
  76. }
  77. // POST: api/TsPiezometer
  78. // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
  79. [HttpPost]
  80. public async Task<ActionResult<TsPozo>> PostTsPiezometer(TsPiezometer tsPiezometer)
  81. {
  82. _context.TsPiezometers.Add(tsPiezometer);
  83. try
  84. {
  85. await _context.SaveChangesAsync();
  86. }
  87. catch (DbUpdateException)
  88. {
  89. if (TsPiezometerExists(tsPiezometer.Id))
  90. {
  91. return Conflict();
  92. }
  93. else
  94. {
  95. throw;
  96. }
  97. }
  98. return CreatedAtAction("GetTsPozo", new { id = tsPiezometer.Id }, tsPiezometer);
  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 TsPiezometerExists(uint id)
  114. {
  115. return _context.TsPozos.Any(e => e.Id == id);
  116. }
  117. }
  118. }