BalanceHidricoController.cs 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. public async Task<ActionResult<uint>> PostBalance(BalanceHidrico balance)
  93. {
  94. _context.BalancesHidricos.Add(balance);
  95. try
  96. {
  97. await _context.SaveChangesAsync();
  98. }
  99. catch (DbUpdateException)
  100. {
  101. if (BalanceExist(balance.Id))
  102. {
  103. return Conflict();
  104. }
  105. else
  106. {
  107. throw;
  108. }
  109. }
  110. return balance.Id;
  111. }
  112. [NonAction]
  113. public async Task<ActionResult<uint>> PostBalanceIn(DatosBalanceIn balanceIn)
  114. {
  115. _context.DatosBalanceIns.Add(balanceIn);
  116. try
  117. {
  118. await _context.SaveChangesAsync();
  119. }
  120. catch (DbUpdateException)
  121. {
  122. if (BalanceInExist(balanceIn.Id))
  123. {
  124. return Conflict();
  125. }
  126. else
  127. {
  128. throw;
  129. }
  130. }
  131. return balanceIn.Id;
  132. }
  133. [NonAction]
  134. public async Task<ActionResult<uint>> PostBalanceOut(DatosBalanceOut balanceOut)
  135. {
  136. _context.DatosBalanceOuts.Add(balanceOut);
  137. try
  138. {
  139. await _context.SaveChangesAsync();
  140. }
  141. catch (DbUpdateException)
  142. {
  143. if (BalanceOutExist(balanceOut.Id))
  144. {
  145. return Conflict();
  146. }
  147. else
  148. {
  149. throw;
  150. }
  151. }
  152. return balanceOut.Id;
  153. }
  154. [NonAction]
  155. public async Task<ActionResult<ConexionZonasIn>> PostConnectionIn(ConexionZonasIn connectionIn)
  156. {
  157. _context.ConexionZonasIns.Add(connectionIn);
  158. try
  159. {
  160. await _context.SaveChangesAsync();
  161. }
  162. catch (DbUpdateException)
  163. {
  164. if (ConnectionInExist(connectionIn.Id))
  165. {
  166. return Conflict();
  167. }
  168. else
  169. {
  170. throw;
  171. }
  172. }
  173. return CreatedAtAction("GetConnectionIn", new { id = connectionIn.Id }, connectionIn);
  174. }
  175. [NonAction]
  176. public async Task<ActionResult<bool>> PostMultipleConnectionIn(ConexionZonasIn[] connectionsIn)
  177. {
  178. _context.ConexionZonasIns.AddRange(connectionsIn);
  179. try
  180. {
  181. await _context.SaveChangesAsync();
  182. }
  183. catch (DbUpdateException)
  184. {
  185. throw;
  186. }
  187. return true;
  188. }
  189. [NonAction]
  190. public async Task<ActionResult<ConexionZonasOut>> PostConnectionOut(ConexionZonasOut connectionOut)
  191. {
  192. _context.ConexionZonasOuts.Add(connectionOut);
  193. try
  194. {
  195. await _context.SaveChangesAsync();
  196. }
  197. catch (DbUpdateException)
  198. {
  199. if (ConnectionOutExist(connectionOut.Id))
  200. {
  201. return Conflict();
  202. }
  203. else
  204. {
  205. throw;
  206. }
  207. }
  208. return CreatedAtAction("GetConnectionOut", new { id = connectionOut.Id }, connectionOut);
  209. }
  210. [NonAction]
  211. public async Task<ActionResult<bool>> PostMultipleConnectionOut(ConexionZonasOut[] connectionsOut)
  212. {
  213. _context.ConexionZonasOuts.AddRange(connectionsOut);
  214. try
  215. {
  216. await _context.SaveChangesAsync();
  217. }
  218. catch (DbUpdateException)
  219. {
  220. throw;
  221. }
  222. return true;
  223. }
  224. private bool BalanceExist(uint id)
  225. {
  226. return _context.BalancesHidricos.Any(e => e.Id == id);
  227. }
  228. private bool BalanceInExist(uint id)
  229. {
  230. return _context.DatosBalanceIns.Any(e => e.Id == id);
  231. }
  232. private bool BalanceOutExist(uint id)
  233. {
  234. return _context.DatosBalanceOuts.Any(e => e.Id == id);
  235. }
  236. private bool ConnectionInExist(uint id)
  237. {
  238. return _context.ConexionZonasIns.Any(e => e.Id == id);
  239. }
  240. private bool ConnectionOutExist(uint id)
  241. {
  242. return _context.ConexionZonasOuts.Any(e => e.Id == id);
  243. }
  244. }
  245. }