// GET: Restaurant/Details/5
        public ActionResult Details(PLC.Restaurant rest)
        {
            if (isValid(rest))
            {
                if (func == null)
                {
                    func = new PLC.Functionality();
                }
                // Grab all of the selected Restaurant's reviews
                var reviews = func.GetReviews(rest.RestaurantID);

                // Add the RestaurantID to each of the review objects for future reference
                foreach (var review in reviews)
                {
                    review.RestaurantID = rest.RestaurantID;
                }

                if (reviews.Count == 0)
                {
                    reviews.Add(new PLC.Review()
                    {
                        RestaurantID = rest.RestaurantID
                    });
                }

                return(View(reviews));
            }
            else
            {
                return(RedirectToAction("Index"));
            }
        }
        public ActionResult Edit(PLC.Review rev, Nullable <int> rID)
        {
            PLC.Restaurant rest = new PLC.Restaurant();
            try
            {
                if (func == null)
                {
                    func = new PLC.Functionality();
                }

                rest = func.GetRestaurant(rev.RestaurantID);

                if (isValid(rev))
                {
                    func.UpdateReview(rev.RestaurantID, rev);

                    return(RedirectToAction("Details", "Restaurant", rest));
                }

                return(RedirectToAction("Details", "Restaurant", rest));
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
                return(RedirectToAction("Index", "Restaurant", rest));
            }
        }
 // Add a Restaurant to the database
 public void AddRestaurant(PLC.Restaurant restaurant)
 {
     using (db = new RestaurantDBEntities())
     {
         db.Restaurants.Add(LibraryToData(restaurant));
         db.SaveChanges();
     }
 }
        // Convert a PLC.Restaurant object to a Restuarant.Restuarant table object
        public Restaurant LibraryToData(PLC.Restaurant restaurant)
        {
            Restaurant rest = new Restaurant()
            {
                Name    = restaurant.Name,
                Address = restaurant.Address,
                City    = restaurant.City,
                State   = restaurant.State,
                ZipCode = restaurant.Zipcode,
                Rating  = restaurant.Rating,
            };

            return(rest);
        }
        // Convert a Restaurant.Restaurant table object to a PLC.Restaurant object
        public PLC.Restaurant DataToLibrary(Restaurant rest)
        {
            PLC.Restaurant r = new PLC.Restaurant()
            {
                RestaurantID = rest.RestaurantID,
                Name         = rest.Name,
                Address      = rest.Address,
                City         = rest.City,
                State        = rest.State,
                Zipcode      = rest.ZipCode,
                Rating       = (double)rest.Rating,
            };

            return(r);
        }
        // GET: Restaurant/Delete/5
        public ActionResult Delete(PLC.Restaurant rest)
        {
            try
            {
                if (func == null)
                {
                    func = new PLC.Functionality();
                }

                func.DeleteRestaurant(rest);
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
            }
            return(RedirectToAction("Index"));
        }
        // GET: Restaurant/Edit/5
        public ActionResult Edit(int restID)
        {
            PLC.Restaurant rest = new PLC.Restaurant();
            try
            {
                if (func == null)
                {
                    func = new PLC.Functionality();
                }

                rest = func.GetRestaurant(restID);
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
            }
            return(View(rest));
        }
        public ActionResult Create(PLC.Restaurant Model)
        {
            try
            {
                if (isValid(Model))
                {
                    // Add the Restaurant into the database
                    if (func == null)
                    {
                        func = new PLC.Functionality();
                    }

                    func.AddRestaurant(Model);
                }

                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                return(View("Error", ex));
            }
        }
        public bool isValid(PLC.Restaurant rest)
        {
            Regex         nameRegex    = new Regex(@"^[^A-Za-z0-9.']+$");
            Regex         addressRegex = new Regex(@"^[^A-Za-z0-9.']+$");
            Regex         cityRegex    = new Regex(@"^[^A-Za-z]+$");
            List <string> states       = new List <string>()
            {
                "Alaska", "Alabama", "Arkansas", "Arizona", "California", "Colorado", "Connecticut", "Delaware",
                "Florida", "Georgia", "Hawaii", "Iowa", "Idaho", "Illinois", "Indiana", "Kansas", "Kentucky", "Louisiana",
                "Massachusetts", "Maryland", "Maine", "Michigan", "Minnesota", "Missouri", "Mississippi", "Montana",
                "North Carolina", "North Dakota", "Nebraska", "New Hampshire", "New Jersey", "New Mexico", "Nevada",
                "New York", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota",
                "Tennessee", "Texas", "Utah", "Virginia", "Vermont", "Washington", "Wisconsin", "West Virginia", "Wyoming"
            };

            if (nameRegex.Matches(rest.Name).Count > 0)
            {
                return(false); // Invalid name is passed to the model
            }
            else if (addressRegex.Matches(rest.Address).Count > 0)
            {
                return(false); // Invalid address
            }
            else if (cityRegex.Matches(rest.City).Count > 0)
            {
                return(false); // Invalid city
            }
            else if (!states.Contains(rest.State))
            {
                return(false);
            }
            else if (rest.Zipcode < 1 || rest.Zipcode > 99999) // Zipcode should be 5 digits between 00001-99999 no extended zipcodes
            {
                return(false);
            }

            return(true);
        }
        public ActionResult Edit(PLC.Restaurant rest, int restID)
        {
            try
            {
                if (isValid(rest))
                {
                    if (func == null)
                    {
                        func = new PLC.Functionality();
                    }
                    // Make that an Edit did take place
                    var original = func.GetRestaurant(restID);
                    if (original.Equals(rest))
                    {
                        // It's the same so just go back to the Index without updating the database
                        return(RedirectToAction("Index"));
                    }
                    else
                    {
                        // The Restaurant's details have been changed so we need to update the database
                        rest.RestaurantID = restID; // Making sure the ID is the right one
                        func.UpdateRestaurant(rest);

                        return(RedirectToAction("Index"));
                    }
                }
                else
                {
                    return(RedirectToAction("Index"));
                }
            }
            catch (Exception ex)
            {
                return(View("Error", ex));
            }
        }