示例#1
0
        public ActionResult Availability()
        {
            BusAvailabilityViewModel mod = new BusAvailabilityViewModel();

            mod.OriginLocations      = db.Locations.ToList();
            mod.DestinationLocations = db.Locations.ToList();
            mod.BusTypes             = db.BusTypes.ToList();
            mod.DateOfJourney        = DateTime.Now.Date;
            return(View(mod));
        }
示例#2
0
        public ActionResult GetBuses(BusAvailabilityViewModel mod)
        {
            if (mod.DestinationLocation == mod.OriginLocation)
            {
                ModelState.AddModelError("DestinationLocation", "Destination location cannot be same as the origin location");
            }



            if (ModelState.IsValid)
            {
                if (User.IsInRole("Customer"))
                {
                    if (mod.BusTypeId == null)
                    {
                        if (mod.DateOfJourney.DayOfWeek.ToString() == "Monday")
                        {
                            mod.AvailabilityDetails =
                                from details in db.AvailabilityDetails.Include("BusDetails").ToList()
                                where details.OriginLocation == mod.OriginLocation && details.DestinationLocation == mod.DestinationLocation && details.Monday == true
                                select details;
                        }

                        else if (mod.DateOfJourney.DayOfWeek.ToString() == "Tuesday")
                        {
                            mod.AvailabilityDetails =
                                from details in db.AvailabilityDetails.Include("BusDetails").ToList()
                                where details.OriginLocation == mod.OriginLocation && details.DestinationLocation == mod.DestinationLocation && details.Tuesday == true
                                select details;
                        }


                        else if (mod.DateOfJourney.DayOfWeek.ToString() == "Wednesday")
                        {
                            mod.AvailabilityDetails =
                                from details in db.AvailabilityDetails.Include("BusDetails").ToList()
                                where details.OriginLocation == mod.OriginLocation && details.DestinationLocation == mod.DestinationLocation && details.Wednesday == true
                                select details;
                        }


                        else if (mod.DateOfJourney.DayOfWeek.ToString() == "Thursday")
                        {
                            mod.AvailabilityDetails =
                                from details in db.AvailabilityDetails.Include("BusDetails").ToList()
                                where details.OriginLocation == mod.OriginLocation && details.DestinationLocation == mod.DestinationLocation && details.Thursday == true
                                select details;
                        }

                        else if (mod.DateOfJourney.DayOfWeek.ToString() == "Friday")
                        {
                            mod.AvailabilityDetails =
                                from details in db.AvailabilityDetails.Include("BusDetails").ToList()
                                where details.OriginLocation == mod.OriginLocation && details.DestinationLocation == mod.DestinationLocation && details.Friday == true
                                select details;
                        }

                        else if (mod.DateOfJourney.DayOfWeek.ToString() == "Saturday")
                        {
                            mod.AvailabilityDetails =
                                from details in db.AvailabilityDetails.Include("BusDetails").ToList()
                                where details.OriginLocation == mod.OriginLocation && details.DestinationLocation == mod.DestinationLocation && details.Saturday == true
                                select details;
                        }

                        else if (mod.DateOfJourney.DayOfWeek.ToString() == "Sunday")
                        {
                            mod.AvailabilityDetails =
                                from details in db.AvailabilityDetails.Include("BusDetails").ToList()
                                where details.OriginLocation == mod.OriginLocation && details.DestinationLocation == mod.DestinationLocation && details.Sunday == true
                                select details;
                        }
                    }

                    else
                    {
                        if (mod.DateOfJourney.DayOfWeek.ToString() == "Monday")
                        {
                            mod.AvailabilityDetails =
                                from details in db.AvailabilityDetails.Include("BusDetails").ToList()
                                where details.OriginLocation == mod.OriginLocation && details.DestinationLocation == mod.DestinationLocation && details.Monday == true && details.BusDetails.BusTypeId == mod.BusTypeId
                                select details;
                        }

                        else if (mod.DateOfJourney.DayOfWeek.ToString() == "Tuesday")
                        {
                            mod.AvailabilityDetails =
                                from details in db.AvailabilityDetails.Include("BusDetails").ToList()
                                where details.OriginLocation == mod.OriginLocation && details.DestinationLocation == mod.DestinationLocation && details.Tuesday == true && details.BusDetails.BusTypeId == mod.BusTypeId
                                select details;
                        }


                        else if (mod.DateOfJourney.DayOfWeek.ToString() == "Wednesday")
                        {
                            mod.AvailabilityDetails =
                                from details in db.AvailabilityDetails.Include("BusDetails").ToList()
                                where details.OriginLocation == mod.OriginLocation && details.DestinationLocation == mod.DestinationLocation && details.Wednesday == true && details.BusDetails.BusTypeId == mod.BusTypeId
                                select details;
                        }


                        else if (mod.DateOfJourney.DayOfWeek.ToString() == "Thursday")
                        {
                            mod.AvailabilityDetails =
                                from details in db.AvailabilityDetails.Include("BusDetails").ToList()
                                where details.OriginLocation == mod.OriginLocation && details.DestinationLocation == mod.DestinationLocation && details.Thursday == true && details.BusDetails.BusTypeId == mod.BusTypeId
                                select details;
                        }

                        else if (mod.DateOfJourney.DayOfWeek.ToString() == "Friday")
                        {
                            mod.AvailabilityDetails =
                                from details in db.AvailabilityDetails.Include("BusDetails").ToList()
                                where details.OriginLocation == mod.OriginLocation && details.DestinationLocation == mod.DestinationLocation && details.Friday == true && details.BusDetails.BusTypeId == mod.BusTypeId
                                select details;
                        }

                        else if (mod.DateOfJourney.DayOfWeek.ToString() == "Saturday")
                        {
                            mod.AvailabilityDetails =
                                from details in db.AvailabilityDetails.Include("BusDetails").ToList()
                                where details.OriginLocation == mod.OriginLocation && details.DestinationLocation == mod.DestinationLocation && details.Saturday == true && details.BusDetails.BusTypeId == mod.BusTypeId
                                select details;
                        }

                        else if (mod.DateOfJourney.DayOfWeek.ToString() == "Sunday")
                        {
                            mod.AvailabilityDetails =
                                from details in db.AvailabilityDetails.Include("BusDetails").ToList()
                                where details.OriginLocation == mod.OriginLocation && details.DestinationLocation == mod.DestinationLocation && details.Sunday == true && details.BusDetails.BusTypeId == mod.BusTypeId
                                select details;
                        }
                    }
                }

                else
                {
                    if (mod.BusTypeId == null)
                    {
                        mod.AvailabilityDetails =
                            from details in db.AvailabilityDetails.Include("BusDetails").ToList()
                            where details.OriginLocation == mod.OriginLocation && details.DestinationLocation == mod.DestinationLocation
                            select details;
                    }

                    else
                    {
                        mod.AvailabilityDetails =
                            from details in db.AvailabilityDetails.Include("BusDetails").ToList()
                            where details.OriginLocation == mod.OriginLocation && details.DestinationLocation == mod.DestinationLocation && details.BusDetails.BusTypeId == mod.BusTypeId
                            select details;
                    }
                }
            }


            // check if any buses were found
            if (mod.AvailabilityDetails == null || !mod.AvailabilityDetails.Any())
            {
                ModelState.AddModelError("NoBuses", "No buses found on this route");
            }

            mod.OriginLocations      = db.Locations.ToList();
            mod.DestinationLocations = db.Locations.ToList();
            mod.BusTypes             = db.BusTypes.ToList();
            mod.BusCompanyNames      = db.BusCompanyNames.ToList();
            return(View("Availability", mod));
        }