BalanceHidricoController.cs 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 BalanceHidricoController : ControllerBase
  15. {
  16. private readonly AquiferContext _context;
  17. public BalanceHidricoController(AquiferContext context)
  18. {
  19. _context = context;
  20. }
  21. // GET: api/BalanceHidrico
  22. [HttpGet]
  23. public async Task<ActionResult<IEnumerable<BalanceHidrico>>> GetBalances()
  24. {
  25. return await _context.BalancesHidricos.ToListAsync();
  26. }
  27. // GET: api/balance/5
  28. [HttpGet("{idSimulacion}")]
  29. public async Task<ActionResult<IEnumerable<ZoneBalance>>> GetBalance(uint idSimulacion)
  30. {
  31. List<BalanceHidrico> dbBalances = await _context.BalancesHidricos.Where( x=> x.IdSimulacion == idSimulacion).ToListAsync<BalanceHidrico>();
  32. List<ZoneBalance> balancesOutput = new List<ZoneBalance>();
  33. foreach (BalanceHidrico bh in dbBalances)
  34. {
  35. DatosBalanceIn dataIn = _context.DatosBalanceIns.Where(x => x.IdBalance == bh.Id).First();
  36. DatosBalanceOut dataOut = _context.DatosBalanceOuts.Where(x => x.IdBalance == bh.Id).First();
  37. List<ConexionZonasIn> connectionsIn = _context.ConexionZonasIns.Where(x => x.IdDatosBalanceIn == dataIn.Id).ToList();
  38. List<ConexionZonasOut> connectionsOut = _context.ConexionZonasOuts.Where(x => x.IdDatosBalanceOut == dataOut.Id).ToList();
  39. ZoneBalance zb = new ZoneBalance(bh);
  40. ZoneData dataBalanceIn = new ZoneData(dataIn);
  41. ZoneData dataBalanceOut = new ZoneData(dataOut);
  42. /*
  43. * TODO: Hacer la discretización por tipo de zona, AGREGAR EL CAMPO A LA TABLA DE LA BBDD Y GUARDARLO TAMBIEN, que no se quede solo en la IMPORTACIÓN (?)
  44. */
  45. if (bh.Zona != "Zone 0")
  46. {
  47. List<ZoneConnection> zoneConnectionsIn = new List<ZoneConnection>();
  48. connectionsIn.ForEach(x => zoneConnectionsIn.Add(new ZoneConnection(x)));
  49. dataBalanceIn.ZoneConnections = zoneConnectionsIn;
  50. List<ZoneConnection> zoneConnectionsOut = new List<ZoneConnection>();
  51. connectionsOut.ForEach(x => zoneConnectionsOut.Add(new ZoneConnection(x)));
  52. dataBalanceOut.ZoneConnections = zoneConnectionsOut;
  53. }
  54. zb.SetInOutData(dataBalanceIn, dataBalanceOut);
  55. balancesOutput.Add(zb);
  56. }
  57. return balancesOutput;
  58. }
  59. [NonAction]
  60. public BalanceHidrico GetBalanceById(uint id)
  61. {
  62. var balance = _context.BalancesHidricos.Find(id);
  63. return balance;
  64. }
  65. [NonAction]
  66. public DatosBalanceIn GetBalanceIn(uint id)
  67. {
  68. var balanceIn = _context.DatosBalanceIns.Find(id);
  69. return balanceIn;
  70. }
  71. [NonAction]
  72. public DatosBalanceOut GetBalanceOut(uint id)
  73. {
  74. var balanceOut = _context.DatosBalanceOuts.Find(id);
  75. return balanceOut;
  76. }
  77. [NonAction]
  78. public ConexionZonasIn GetConnectionIn(uint id)
  79. {
  80. var connectionIn = _context.ConexionZonasIns.Find(id);
  81. return connectionIn;
  82. }
  83. [NonAction]
  84. public ConexionZonasOut GetConnectionOut(uint id)
  85. {
  86. var connectionOut = _context.ConexionZonasOuts.Find(id);
  87. return connectionOut;
  88. }
  89. // POST: api/TsPozo
  90. // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
  91. [HttpPost]
  92. [ProducesResponseType(200)]
  93. [ProducesResponseType(409)]
  94. public async Task<ActionResult<uint>> PostBalance(BalanceHidrico balance)
  95. {
  96. _context.BalancesHidricos.Add(balance);
  97. try
  98. {
  99. await _context.SaveChangesAsync();
  100. }
  101. catch (DbUpdateException)
  102. {
  103. if (BalanceExist(balance.Id))
  104. {
  105. return Conflict();
  106. }
  107. else
  108. {
  109. throw;
  110. }
  111. }
  112. return balance.Id;
  113. }
  114. [NonAction]
  115. public async Task<ActionResult<uint>> PostBalanceIn(DatosBalanceIn balanceIn)
  116. {
  117. _context.DatosBalanceIns.Add(balanceIn);
  118. try
  119. {
  120. await _context.SaveChangesAsync();
  121. }
  122. catch (DbUpdateException)
  123. {
  124. if (BalanceInExist(balanceIn.Id))
  125. {
  126. return Conflict();
  127. }
  128. else
  129. {
  130. throw;
  131. }
  132. }
  133. return balanceIn.Id;
  134. }
  135. [NonAction]
  136. public async Task<ActionResult<uint>> PostBalanceOut(DatosBalanceOut balanceOut)
  137. {
  138. _context.DatosBalanceOuts.Add(balanceOut);
  139. try
  140. {
  141. await _context.SaveChangesAsync();
  142. }
  143. catch (DbUpdateException)
  144. {
  145. if (BalanceOutExist(balanceOut.Id))
  146. {
  147. return Conflict();
  148. }
  149. else
  150. {
  151. throw;
  152. }
  153. }
  154. return balanceOut.Id;
  155. }
  156. [NonAction]
  157. public async Task<ActionResult<ConexionZonasIn>> PostConnectionIn(ConexionZonasIn connectionIn)
  158. {
  159. _context.ConexionZonasIns.Add(connectionIn);
  160. try
  161. {
  162. await _context.SaveChangesAsync();
  163. }
  164. catch (DbUpdateException)
  165. {
  166. if (ConnectionInExist(connectionIn.Id))
  167. {
  168. return Conflict();
  169. }
  170. else
  171. {
  172. throw;
  173. }
  174. }
  175. return CreatedAtAction("GetConnectionIn", new { id = connectionIn.Id }, connectionIn);
  176. }
  177. [NonAction]
  178. public async Task<ActionResult<bool>> PostMultipleConnectionIn(ConexionZonasIn[] connectionsIn)
  179. {
  180. _context.ConexionZonasIns.AddRange(connectionsIn);
  181. try
  182. {
  183. await _context.SaveChangesAsync();
  184. }
  185. catch (DbUpdateException)
  186. {
  187. throw;
  188. }
  189. return true;
  190. }
  191. [NonAction]
  192. public async Task<ActionResult<ConexionZonasOut>> PostConnectionOut(ConexionZonasOut connectionOut)
  193. {
  194. _context.ConexionZonasOuts.Add(connectionOut);
  195. try
  196. {
  197. await _context.SaveChangesAsync();
  198. }
  199. catch (DbUpdateException)
  200. {
  201. if (ConnectionOutExist(connectionOut.Id))
  202. {
  203. return Conflict();
  204. }
  205. else
  206. {
  207. throw;
  208. }
  209. }
  210. return CreatedAtAction("GetConnectionOut", new { id = connectionOut.Id }, connectionOut);
  211. }
  212. [NonAction]
  213. public async Task<ActionResult<bool>> PostMultipleConnectionOut(ConexionZonasOut[] connectionsOut)
  214. {
  215. _context.ConexionZonasOuts.AddRange(connectionsOut);
  216. try
  217. {
  218. await _context.SaveChangesAsync();
  219. }
  220. catch (DbUpdateException)
  221. {
  222. throw;
  223. }
  224. return true;
  225. }
  226. private bool BalanceExist(uint id)
  227. {
  228. return _context.BalancesHidricos.Any(e => e.Id == id);
  229. }
  230. private bool BalanceInExist(uint id)
  231. {
  232. return _context.DatosBalanceIns.Any(e => e.Id == id);
  233. }
  234. private bool BalanceOutExist(uint id)
  235. {
  236. return _context.DatosBalanceOuts.Any(e => e.Id == id);
  237. }
  238. private bool ConnectionInExist(uint id)
  239. {
  240. return _context.ConexionZonasIns.Any(e => e.Id == id);
  241. }
  242. private bool ConnectionOutExist(uint id)
  243. {
  244. return _context.ConexionZonasOuts.Any(e => e.Id == id);
  245. }
  246. }
  247. }