public HttpResponse <GastoResponse> ValidarGasto(ValidacionRequest request) { if (request == null) { return(newHttpErrorResponse(new Error("El gasto que se intenta validar es invalido"))); } using (var db = new GeviApiContext()) { var gasto = db.Gastos .Where(g => g.Id == request.Id) .Include(g => g.Tipo) .Include(g => g.Empleado) .Include(g => g.Viaje.Proyecto) .Include(g => g.Moneda) .FirstOrDefault(); if (gasto != null) { gasto.Estado = request.Estado; db.Entry(gasto).State = EntityState.Modified; db.SaveChanges(); var response = new GastoResponse() { Id = gasto.Id, Moneda = gasto.Moneda?.Nombre, Estado = gasto.Estado, Fecha = gasto.Fecha, Tipo = gasto.Tipo?.Nombre, Total = gasto.Total, Empleado = gasto.Empleado?.Nombre, Proyecto = gasto.Viaje?.Proyecto?.Nombre, ViajeId = gasto.Viaje == null ? 0 : gasto.Viaje.Id }; return(newHttpResponse(response)); } return(newHttpErrorResponse(new Error("No existe el gasto"))); } }
public HttpResponse <ViajeResponse> ValidarViaje(ValidacionRequest request) { if (request == null) { return(newHttpErrorResponse(new Error("La request es invalida."))); } using (var db = new GeviApiContext()) { var viaje = db.Viajes .Where(v => v.Id == request.Id) .Include(u => u.Empleado) .Include(w => w.Proyecto.Cliente) .FirstOrDefault(); if (viaje != null) { viaje.Estado = request.Estado; db.Entry(viaje).State = EntityState.Modified; db.SaveChanges(); var response = new ViajeResponse() { Id = viaje.Id, EmpleadoId = viaje.Empleado.Id, EmpleadoNombre = viaje.Empleado?.Nombre, Estado = request.Estado, FechaInicio = viaje.FechaInicio, FechaFin = viaje.FechaFin, Gastos = null, Proyecto = viaje.Proyecto?.Nombre, ClienteProyectoNombre = viaje.Proyecto?.Cliente?.Nombre }; if (viaje.Gastos != null) { var gastos = new List <GastoResponse>(); foreach (var g in viaje.Gastos) { var nuevoGasto = new GastoResponse() { Id = g.Id, Estado = g.Estado, Fecha = g.Fecha, Moneda = g.Moneda?.Nombre, Tipo = g.Tipo?.Nombre, Total = g.Total, ViajeId = viaje.Id, Proyecto = g.Viaje?.Proyecto?.Nombre, Empleado = g.Empleado?.Nombre }; gastos.Add(nuevoGasto); } response.Gastos = gastos; } return(newHttpResponse(response)); } return(newHttpErrorResponse(new Error("No existe el viaje"))); } }