Esempio n. 1
0
        public HttpResponseMessage pistasReserva(PistasReservaRequest datos)
        {
            var PistaDA = new PistaDataAccess();
            var Pistas  = PistaDA.pistasParaReserva(datos);

            return(Request.CreateResponse(HttpStatusCode.OK, Pistas));
        }
        //RECUPERAR PISTAS DISPONIBLES PARA RESERVA
        //recibimos deporte y fecha en datos
        //devolvemos array de pistas con las horas libres y reservadas para fecha y deporte recibidos
        public IEnumerable <PistasReservaResponse> pistasParaReserva(PistasReservaRequest datos)
        {
            List <PistasReservaResponse> pistasConReserva = new List <PistasReservaResponse>();
            List <PistasReservaResponse> listaPistas      = null;//lista de pistas disponibles para el deporte
            List <PistasReservaResponse> response         = new List <PistasReservaResponse>();

            try
            {
                //recuperamos listado pistas
                using (var context = new BDReservasEntities())
                {
                    listaPistas = (from i in context.V_INSTALACIONES_HORARIOS
                                   where i.actividad == datos.Actividad
                                   where i.pista_operativa == true
                                   where i.instalación_operativa == true
                                   select new PistasReservaResponse
                    {
                        Pista = i.pista.Trim(),
                        Id_pista = i.id_pista,
                        Horario = i.horario.Trim(),
                        Instalacion = i.instalacion.Trim(),
                        Precio_hora = i.precio_hora,
                        Fecha = datos.Fecha.Trim()
                    }).ToList();

                    //si no se encuentran pistas se devuelve msje informativo
                    if (listaPistas.Count < 1)
                    {
                        pistasConReserva.Add(new PistasReservaResponse()
                        {
                            Mensaje = "No existen pistas disponibles "
                        });
                    }
                    else
                    {
                        //recuperamos horas reservadas/libres para cada pista el dia indicado
                        foreach (var p in listaPistas)
                        {
                            List <PistasReservaResponse> tmp = (from i in context.V_RESERVAS_PISTAS
                                                                where i.pista == p.Pista
                                                                where i.fecha == datos.Fecha
                                                                where i.estado != "cancelada"
                                                                select new PistasReservaResponse
                            {
                                Pista = i.pista.Trim(),
                                Id_pista = i.id_pista,
                                H_ini = i.h_ini.Trim(),
                                H_fin = i.h_fin.Trim(),
                                Horas = (decimal)i.horas,
                                Horario = p.Horario.Trim(),
                                Instalacion = p.Instalacion.Trim(),
                                Precio_hora = p.Precio_hora,
                                Fecha = datos.Fecha,
                            }).ToList();

                            //si no hay reservas para la pista
                            if (tmp.Count < 1)
                            {
                                List <PistasReservaResponse> vacia = new List <PistasReservaResponse> {
                                    p
                                };
                                pistasConReserva.AddRange(vacia);
                            }
                            else
                            {
                                pistasConReserva.AddRange(tmp);
                            }
                        }
                        string[] ph                  = new string[13];
                        string   nomPista            = "";
                        PistasReservaResponse pisres = null;
                        //recorremos las pistas con reserva encontradas
                        for (var x = 0; x <= pistasConReserva.Count(); x++)
                        {
                            if (x != pistasConReserva.Count())
                            {
                                if (pistasConReserva[x].Pista != nomPista)//es pista distinta
                                {
                                    if (pisres != null)
                                    {
                                        pisres.H_ini = null;
                                        pisres.H_fin = null;
                                        pisres.Horas = 0;
                                        response.Add(pisres);//guardamos pista con sus reservas
                                    }
                                    ph       = new string[13];
                                    pisres   = null;
                                    nomPista = pistasConReserva[x].Pista;
                                    pisres   = pistasConReserva[x];

                                    ph = this.checkHorasReserva(pistasConReserva[x], ph);
                                    pisres.LibresReservadas = ph;
                                }
                                else
                                {
                                    ph = this.checkHorasReserva(pistasConReserva[x], ph);
                                    pisres.LibresReservadas = ph;
                                }
                            }
                            else
                            {
                                pisres.H_ini = null;
                                pisres.H_fin = null;
                                pisres.Horas = 0;
                                response.Add(pisres);//guardamos pista con sus reservas
                            }
                        }
                    }

                    return(response);
                }
            }
            catch (Exception ex)
            {
                response.Add(new PistasReservaResponse()
                {
                    Mensaje = "No se pudo realizar la consulta. -- " + ex.Message
                });

                return(pistasConReserva);
            }
        }