public async Task <ActionResult <StandardResponse> > ConsultaLuz(string codigo) { ServicioLuz servicioLuz = await _context.ServicioLuz.Where(b => b.Correlativo == codigo).FirstOrDefaultAsync(); if (servicioLuz == null) { return(NotFound()); } else { StandardResponse standardResponse = new StandardResponse(); standardResponse.Code = 200; standardResponse.Message = "Servicio Existe"; standardResponse.data = "Saldo:" + servicioLuz.Saldo + ",cuentaBancaria:" + servicioLuz.CuentaBancariaId; return(Ok(standardResponse)); } }
public async Task <ActionResult <StandardResponse> > PagoServicio(PagoServicio pagoServicio) { Tarjeta tarjeta = await _context.Tarjeta.Where(b => b.TarjetaId == pagoServicio.TarjetaId).FirstOrDefaultAsync(); if (tarjeta == null) { return(NotFound()); } CuentaBancaria cuentaBancariaOrigen = await _context.CuentaBancaria.Where(b => b.CuentaBancariaId == tarjeta.CuentaBancariaId).FirstOrDefaultAsync(); if (cuentaBancariaOrigen == null) { return(NotFound()); } StandardResponse standardResponse = new StandardResponse(); if ((cuentaBancariaOrigen.Saldo - pagoServicio.Monto) < 0) { standardResponse.Code = 200; standardResponse.Message = "Saldo Insuficiente"; standardResponse.data = tarjeta.TarjetaId.ToString(); return(Ok(standardResponse)); } long cuentaDestino; if (pagoServicio.TipoServicio.Equals("T")) { ServicioTelefono servicioTelefono = await _context.ServicioTelefono.Where(b => b.Telefono == pagoServicio.Correlativo).FirstOrDefaultAsync(); if (servicioTelefono == null) { return(NotFound()); } cuentaDestino = servicioTelefono.CuentaBancariaId; servicioTelefono.Saldo -= pagoServicio.Monto; _context.Entry(servicioTelefono).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { throw; } } else { ServicioLuz servicioLuz = await _context.ServicioLuz.Where(b => b.Correlativo == pagoServicio.Correlativo).FirstOrDefaultAsync(); if (servicioLuz == null) { return(NotFound()); } cuentaDestino = servicioLuz.CuentaBancariaId; servicioLuz.Saldo -= pagoServicio.Monto; _context.Entry(servicioLuz).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { throw; } } CuentaBancaria cuentaBancariaDestino = await _context.CuentaBancaria.Where(b => b.CuentaBancariaId == cuentaDestino).FirstOrDefaultAsync(); if (cuentaBancariaDestino == null) { return(NotFound()); } cuentaBancariaDestino.Saldo += pagoServicio.Monto; cuentaBancariaOrigen.Saldo -= pagoServicio.Monto; _context.Entry(cuentaBancariaDestino).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { throw; } _context.Entry(cuentaBancariaOrigen).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { throw; } //Guarda Bitacora Bitacora bitacora = new Bitacora(); bitacora.CuentaBancariaDestino = cuentaBancariaDestino.CuentaBancariaId; bitacora.CuentaBancariaOrigen = cuentaBancariaOrigen.CuentaBancariaId; bitacora.Monto = pagoServicio.Monto; _context.Bitacora.Add(bitacora); await _context.SaveChangesAsync(); //Devuelve Respuesta standardResponse.Code = 200; standardResponse.Message = "Pago Exitoso"; standardResponse.data = tarjeta.TarjetaId.ToString(); return(Ok(standardResponse)); }