123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.EntityFrameworkCore;
- using WebApplication3.Models;
- using WebApplication3.Clases;
-
- namespace WebApplication3.Controllers
- {
- [Route("api/[controller]")]
- [ApiController]
- public class BalanceHidricoController : ControllerBase
- {
- private readonly AquiferContext _context;
-
- public BalanceHidricoController(AquiferContext context)
- {
- _context = context;
- }
-
- // GET: api/BalanceHidrico
- [HttpGet]
- public async Task<ActionResult<IEnumerable<BalanceHidrico>>> GetBalances()
- {
- return await _context.BalancesHidricos.ToListAsync();
- }
-
- // GET: api/balance/5
- [HttpGet("{idSimulacion}")]
- public async Task<ActionResult<IEnumerable<ZoneBalance>>> GetBalance(uint idSimulacion)
- {
- List<BalanceHidrico> dbBalances = await _context.BalancesHidricos.Where( x=> x.IdSimulacion == idSimulacion).ToListAsync<BalanceHidrico>();
-
- List<ZoneBalance> balancesOutput = new List<ZoneBalance>();
-
- foreach (BalanceHidrico bh in dbBalances)
- {
- DatosBalanceIn dataIn = _context.DatosBalanceIns.Where(x => x.IdBalance == bh.Id).First();
- DatosBalanceOut dataOut = _context.DatosBalanceOuts.Where(x => x.IdBalance == bh.Id).First();
-
- List<ConexionZonasIn> connectionsIn = _context.ConexionZonasIns.Where(x => x.IdDatosBalanceIn == dataIn.Id).ToList();
- List<ConexionZonasOut> connectionsOut = _context.ConexionZonasOuts.Where(x => x.IdDatosBalanceOut == dataOut.Id).ToList();
-
-
- ZoneBalance zb = new ZoneBalance(bh);
- ZoneData dataBalanceIn = new ZoneData(dataIn);
- ZoneData dataBalanceOut = new ZoneData(dataOut);
-
- /*
- * 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 (?)
- */
- if (bh.Zona != "Zone 0")
- {
- List<ZoneConnection> zoneConnectionsIn = new List<ZoneConnection>();
- connectionsIn.ForEach(x => zoneConnectionsIn.Add(new ZoneConnection(x)));
- dataBalanceIn.ZoneConnections = zoneConnectionsIn;
-
- List<ZoneConnection> zoneConnectionsOut = new List<ZoneConnection>();
- connectionsOut.ForEach(x => zoneConnectionsOut.Add(new ZoneConnection(x)));
- dataBalanceOut.ZoneConnections = zoneConnectionsOut;
- }
-
- zb.SetInOutData(dataBalanceIn, dataBalanceOut);
- balancesOutput.Add(zb);
-
- }
-
- return balancesOutput;
- }
-
- [NonAction]
- public BalanceHidrico GetBalanceById(uint id)
- {
- var balance = _context.BalancesHidricos.Find(id);
-
- return balance;
- }
-
- [NonAction]
- public DatosBalanceIn GetBalanceIn(uint id)
- {
- var balanceIn = _context.DatosBalanceIns.Find(id);
-
- return balanceIn;
- }
-
- [NonAction]
- public DatosBalanceOut GetBalanceOut(uint id)
- {
- var balanceOut = _context.DatosBalanceOuts.Find(id);
-
- return balanceOut;
- }
-
- [NonAction]
- public ConexionZonasIn GetConnectionIn(uint id)
- {
- var connectionIn = _context.ConexionZonasIns.Find(id);
-
- return connectionIn;
- }
-
- [NonAction]
- public ConexionZonasOut GetConnectionOut(uint id)
- {
- var connectionOut = _context.ConexionZonasOuts.Find(id);
-
- return connectionOut;
- }
-
- // POST: api/TsPozo
- // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
- [HttpPost]
- [ProducesResponseType(200)]
- [ProducesResponseType(409)]
- public async Task<ActionResult<uint>> PostBalance(BalanceHidrico balance)
- {
- _context.BalancesHidricos.Add(balance);
- try
- {
- await _context.SaveChangesAsync();
- }
- catch (DbUpdateException)
- {
- if (BalanceExist(balance.Id))
- {
- return Conflict();
- }
- else
- {
- throw;
- }
- }
-
- return balance.Id;
- }
-
- [NonAction]
- public async Task<ActionResult<uint>> PostBalanceIn(DatosBalanceIn balanceIn)
- {
- _context.DatosBalanceIns.Add(balanceIn);
- try
- {
- await _context.SaveChangesAsync();
- }
- catch (DbUpdateException)
- {
- if (BalanceInExist(balanceIn.Id))
- {
- return Conflict();
- }
- else
- {
- throw;
- }
- }
-
- return balanceIn.Id;
- }
-
- [NonAction]
- public async Task<ActionResult<uint>> PostBalanceOut(DatosBalanceOut balanceOut)
- {
- _context.DatosBalanceOuts.Add(balanceOut);
- try
- {
- await _context.SaveChangesAsync();
- }
- catch (DbUpdateException)
- {
- if (BalanceOutExist(balanceOut.Id))
- {
- return Conflict();
- }
- else
- {
- throw;
- }
- }
-
- return balanceOut.Id;
- }
-
- [NonAction]
- public async Task<ActionResult<ConexionZonasIn>> PostConnectionIn(ConexionZonasIn connectionIn)
- {
- _context.ConexionZonasIns.Add(connectionIn);
- try
- {
- await _context.SaveChangesAsync();
- }
- catch (DbUpdateException)
- {
- if (ConnectionInExist(connectionIn.Id))
- {
- return Conflict();
- }
- else
- {
- throw;
- }
- }
-
- return CreatedAtAction("GetConnectionIn", new { id = connectionIn.Id }, connectionIn);
- }
-
- [NonAction]
- public async Task<ActionResult<bool>> PostMultipleConnectionIn(ConexionZonasIn[] connectionsIn)
- {
- _context.ConexionZonasIns.AddRange(connectionsIn);
- try
- {
- await _context.SaveChangesAsync();
- }
- catch (DbUpdateException)
- {
-
- throw;
-
- }
-
- return true;
- }
-
- [NonAction]
- public async Task<ActionResult<ConexionZonasOut>> PostConnectionOut(ConexionZonasOut connectionOut)
- {
- _context.ConexionZonasOuts.Add(connectionOut);
- try
- {
- await _context.SaveChangesAsync();
- }
- catch (DbUpdateException)
- {
- if (ConnectionOutExist(connectionOut.Id))
- {
- return Conflict();
- }
- else
- {
- throw;
- }
- }
-
- return CreatedAtAction("GetConnectionOut", new { id = connectionOut.Id }, connectionOut);
- }
-
- [NonAction]
- public async Task<ActionResult<bool>> PostMultipleConnectionOut(ConexionZonasOut[] connectionsOut)
- {
- _context.ConexionZonasOuts.AddRange(connectionsOut);
- try
- {
- await _context.SaveChangesAsync();
- }
- catch (DbUpdateException)
- {
- throw;
- }
-
- return true;
- }
-
- private bool BalanceExist(uint id)
- {
- return _context.BalancesHidricos.Any(e => e.Id == id);
- }
-
- private bool BalanceInExist(uint id)
- {
- return _context.DatosBalanceIns.Any(e => e.Id == id);
- }
-
- private bool BalanceOutExist(uint id)
- {
- return _context.DatosBalanceOuts.Any(e => e.Id == id);
- }
-
- private bool ConnectionInExist(uint id)
- {
- return _context.ConexionZonasIns.Any(e => e.Id == id);
- }
-
- private bool ConnectionOutExist(uint id)
- {
- return _context.ConexionZonasOuts.Any(e => e.Id == id);
- }
- }
- }
|