public async Task <ActionResult> Post([FromBody] ReservaDto reservaCreated) { if (reservaCreated.Hora < 8 || reservaCreated.Hora > 22) { return(NotFound(new Result(404, false, "La hora pasada esta fuera del rango de horas de reservas").GetResultJson())); } var socio = await context.Socios.FirstOrDefaultAsync(x => x.Id == reservaCreated.SocioId); if (socio == null) { return(NotFound(new Result(404, false, "No se ha introducido un id Socio válido").GetResultJson())); } var pista = await context.Pistas.FirstOrDefaultAsync(x => x.Id == reservaCreated.PistaId); if (pista == null) { return(NotFound(new Result(404, false, "No se ha introducido un id Pista válido").GetResultJson())); } if (UtilReservas.GetReservasSocio(context, reservaCreated).Count() > 2) { return(NotFound(new Result(404, false, "No se permite más de 3 reservas al día para un socio").GetResultJson())); } if (UtilReservas.GetReservaPistaHora(context, reservaCreated).Any()) { return(NotFound(new Result(404, false, "La pista esta reservada ya").GetResultJson())); } if (UtilReservas.GetReservaSocioHora(context, reservaCreated).Any()) { return(NotFound(new Result(404, false, "El socio ya tiene una pista a esa hora").GetResultJson())); } var reserva = mapper.Map <Reserva>(reservaCreated); context.Add(reserva); await context.SaveChangesAsync(); var reservaViewModel = mapper.Map <ReservaViewModel>(reserva); return(new CreatedAtRouteResult("ObtenerSocio", new { id = reserva.Id }, reservaViewModel)); }
public async Task <ActionResult> Put(int id, [FromBody] ReservaDto reservaModificated) { if (reservaModificated.Hora < 8 || reservaModificated.Hora > 22) { return(NotFound(new Result(404, false, "La hora pasada esta fuera del rango de horas de reservas").GetResultJson())); } var socio = await context.Socios.FirstOrDefaultAsync(x => x.Id == reservaModificated.SocioId); if (socio == null) { return(NotFound(new Result(404, false, "No se ha introducido un id Socio válido").GetResultJson())); } var pista = await context.Pistas.FirstOrDefaultAsync(x => x.Id == reservaModificated.PistaId); if (pista == null) { return(NotFound(new Result(404, false, "No se ha introducido un id Pista válido").GetResultJson())); } if (UtilReservas.GetReservasSocio(context, reservaModificated).Count() > 2) { return(NotFound(new Result(404, false, "No se permite más de 3 reservas al día para un socio").GetResultJson())); } if (UtilReservas.GetReservaPistaHora(context, reservaModificated).Any()) { return(NotFound(new Result(404, false, "La pista esta reservada ya").GetResultJson())); } if (UtilReservas.GetReservaSocioHora(context, reservaModificated).Any()) { return(NotFound(new Result(404, false, "El socio ya tiene una pista a esa hora").GetResultJson())); } var reserva = mapper.Map <Reserva>(reservaModificated); reserva.Id = id; context.Entry(reserva).State = EntityState.Modified; await context.SaveChangesAsync(); return(NoContent()); }