Пример #1
0
        public void Test_Delete_DeletesCuisineAndRestaurantsAndReviewsByCuisineId()
        {
            //Arrange
              Cuisine firstCuisine = new Cuisine("Fast Food");
              firstCuisine.Save();
              Cuisine secondCuisine = new Cuisine("Mexican");
              secondCuisine.Save();

              Restaurant firstRestaurant = new Restaurant("McDonalds", firstCuisine.GetId());
              firstRestaurant.Save();
              Restaurant secondRestaurant = new Restaurant("Chipotle", secondCuisine.GetId());
              secondRestaurant.Save();

              Review firstReview = new Review("Test", firstRestaurant.GetId(), 2);
              firstReview.Save();
              Review secondReview = new Review("Test2", secondRestaurant.GetId(), 2);
              secondReview.Save();

              List<Cuisine> expectedCuisine = new List<Cuisine>{firstCuisine};
              List<Restaurant> expectedRestaurant = new List<Restaurant> {firstRestaurant};
              List<Review> expectedReview = new List<Review> {firstReview};

              //Act
              secondCuisine.Delete();
              List<Cuisine> resultingCuisine = Cuisine.GetAll();
              List<Restaurant> resultingRestaurant = Restaurant.GetAll();
              List<Review> resultingReview = Review.GetAll();

              //Assert
              Assert.Equal(expectedCuisine, resultingCuisine);
              Assert.Equal(expectedRestaurant, resultingRestaurant);
              Assert.Equal(expectedReview, resultingReview);
        }
Пример #2
0
        public void Test_Equals_ReturnsTrueIfNameAndCuisineAreIdentical()
        {
            //Arrange
              Restaurant firstRestaurant = new Restaurant("McDonalds", 1);
              Restaurant secondRestaurant = new Restaurant("McDonalds", 1);

              //Assert
              Assert.Equal(firstRestaurant, secondRestaurant);
        }
Пример #3
0
        public void Test_DeleteOne_DeletesASpecificRestaurantObject()
        {
            //Arrange
              Restaurant firstRestaurant = new Restaurant("McDonalds", 1);
              firstRestaurant.Save();
              Restaurant secondRestaurant = new Restaurant("Dennys", 2);
              secondRestaurant.Save();

              //Act
              secondRestaurant.Delete();
              List<Restaurant> expectedRestaurant = new List<Restaurant> {firstRestaurant};
              List<Restaurant> testRestaurant= Restaurant.GetAll();

              //Assert
              Assert.Equal(expectedRestaurant, testRestaurant);
        }
Пример #4
0
        public void Test_CalculateAverageRating_CorrectlySavesAveragesRatings()
        {
            //Arrange
              Restaurant newRestaurant = new Restaurant("McDonalds", 1);
              newRestaurant.Save();

              Review firstReview = new Review("Yummy", newRestaurant.GetId(), 2, 4);
              firstReview.Save();
              Review secondReview = new Review("Ok", newRestaurant.GetId(), 1, 3);
              secondReview.Save();

              //Act
              double result = newRestaurant.CalculateAverageRating();
              newRestaurant.SetAverageRating(result);
              double resultToCheck = newRestaurant.GetAverageRating();
              //Assert
              Assert.Equal(3.5, resultToCheck);
        }
Пример #5
0
        public static Restaurant Find(int restaurantId)
        {
            SqlConnection conn = DB.Connection();
              SqlDataReader rdr;
              conn.Open();

              SqlCommand cmd = new SqlCommand("SELECT * FROM restaurants WHERE id = @restaurantId;", conn);

              SqlParameter restaurantIdParameter = new SqlParameter();
              restaurantIdParameter.ParameterName = "@restaurantId";
              restaurantIdParameter.Value = restaurantId.ToString();

              cmd.Parameters.Add(restaurantIdParameter);

              int foundRestaurantId = 0;
              string restaurantName = null;
              int restaurantCuisineId = 0;
              double foundRestaurantAverageRating = 0;
              rdr = cmd.ExecuteReader();

              while(rdr.Read())
              {
            foundRestaurantId = rdr.GetInt32(0);
            restaurantName = rdr.GetString(1);
            restaurantCuisineId = rdr.GetInt32(2);
            foundRestaurantAverageRating = rdr.GetDouble(3);
              }
              Restaurant newRestaurant = new Restaurant(restaurantName, restaurantCuisineId, foundRestaurantId, foundRestaurantAverageRating);
              if (rdr != null)
              {
            rdr.Close();
              }
              if (conn != null)
              {
            conn.Close();
              }
              return newRestaurant;
        }
Пример #6
0
        public void Test_GetRestaurants_FindsRestaurantsByCuisineId()
        {
            //Arrange
              Cuisine newCuisine = new Cuisine("Fast Food");
              newCuisine.Save();

              Restaurant firstRestaurant = new Restaurant("McDonalds", newCuisine.GetId());

              firstRestaurant.Save();
              Restaurant secondRestaurant = new Restaurant("Dennys", 2);
              secondRestaurant.Save();
              List<Restaurant> expectedResult = new List<Restaurant> {firstRestaurant};
              //Act
              List<Restaurant> result = newCuisine.GetRestaurants();
              //Assert
              Assert.Equal(expectedResult, result);
        }
Пример #7
0
        public HomeModule()
        {
            Get["/"]=_=>{
            if (User.GetAll().Count == 0)
            {
              User admin = new User("Admin", 1);
              admin.Save();
              User guest = new User("Guest", 1);
              guest.Save();
              guest.SetCurrentUser();
            }
            User currentUser = User.GetCurrentUser();
            return View["index.cshtml", currentUser];
              };

              Delete["/"]=_=>{
            Cuisine.DeleteAll();
            Restaurant.DeleteAll();
            Review.DeleteAll();
            User.DeleteAll();
            User admin = new User("Admin", 1);
            admin.Save();
            User guest = new User("Guest", 1);
            guest.Save();
            guest.SetCurrentUser();
            User currentUser = User.GetCurrentUser();
            return View["index.cshtml", currentUser];
              };

              Get["/login"]=_=>{
            List<User> allUsers = User.GetAll();
            Dictionary<string, object> model = new Dictionary<string, object>();
            User currentUser = User.GetCurrentUser();
            model.Add("currentUser", currentUser);
            model.Add("allUsers", allUsers);
            return View["log_in.cshtml", model];
              };

              Post["/login/success"]=_=>{
            User currentUser = User.Find(Request.Form["user-id"]);
            currentUser.SetCurrentUser();
            return View["index.cshtml", currentUser];
              };

              Get["/cuisines"]=_=>{
            List<Cuisine> allCuisines = Cuisine.GetAll();
            Dictionary<string, object> model = new Dictionary<string, object>();
            User currentUser = User.GetCurrentUser();
            model.Add("currentUser", currentUser);
            model.Add("allCuisines", allCuisines);
            return View["cuisines.cshtml", model];
              };

              Get["/cuisine/{id}"] = parameters => {
            Dictionary<string, object> model = new Dictionary<string, object>();
            var selectedCuisine = Cuisine.Find(parameters.id);
            var cuisineRestaurants = selectedCuisine.GetRestaurants();
            var currentUser= User.GetCurrentUser();
            model.Add("currentUser", currentUser);
            model.Add("cuisine", selectedCuisine);
            model.Add("restaurants", cuisineRestaurants);
            return View ["cuisine.cshtml", model];
              };

              Delete["/cuisine/deleted/{id}"]=parameters=>{
            Cuisine selectedCuisine = Cuisine.Find(parameters.id);
            selectedCuisine.Delete();
            List<Cuisine> allCuisines = Cuisine.GetAll();
            Dictionary<string, object> model = new Dictionary<string, object>();
            User currentUser = User.GetCurrentUser();
            model.Add("currentUser", currentUser);
            model.Add("allCuisines", allCuisines);
            return View["cuisines.cshtml", model];
              };

              Get["/cuisine/new"]=_=>{
            User currentUser = User.GetCurrentUser();
            return View["cuisine_new.cshtml", currentUser];
              };

              Post["/cuisine/success"]=_=>{
            Cuisine newCuisine = new Cuisine(Request.Form["cuisine-name"]);
            newCuisine.Save();
            List<Cuisine> allCuisines = Cuisine.GetAll();
            Dictionary<string, object> model = new Dictionary<string, object>();
            User currentUser = User.GetCurrentUser();
            model.Add("currentUser", currentUser);
            model.Add("allCuisines", allCuisines);
            return View["cuisines.cshtml", model];
              };

              Get["/restaurants"]=_=>{
            List<Restaurant> allRestaurants = Restaurant.GetAll();
            Dictionary<string, object> model = new Dictionary<string, object>();
            User currentUser = User.GetCurrentUser();
            model.Add("currentUser", currentUser);
            model.Add("allRestaurants", allRestaurants);
            return View["restaurants.cshtml", model];
              };

              Get["/restaurant/{id}"] = parameters => {
            Dictionary<string, object> model = new Dictionary<string, object>();
            var selectedRestaurant = Restaurant.Find(parameters.id);
            var selectedReviews = selectedRestaurant.GetReviews();
            var allUsers = User.GetAll();
            Dictionary<int, string> usernames = new Dictionary<int, string>{};
            foreach(User reviewer in allUsers)
            {
              usernames.Add(reviewer.GetId(), reviewer.GetName());
            }
            var currentUser= User.GetCurrentUser();
            model.Add("currentUser", currentUser);
            model.Add("users", usernames);
            model.Add("restaurant", selectedRestaurant);
            model.Add("reviews", selectedReviews);
            return View["restaurant.cshtml", model];
              };

              Get["/restaurant/edit/{id}"] = parameters => {
            Dictionary<string, object> model = new Dictionary<string, object>();
            var selectedRestaurant = Restaurant.Find(parameters.id);
            var allCuisines = Cuisine.GetAll();
            var currentUser= User.GetCurrentUser();
            model.Add("currentUser", currentUser);
            model.Add("restaurant", selectedRestaurant);
            model.Add("cuisines", allCuisines);
            return View["restaurant_edit.cshtml", model];
              };

              Delete["/restaurant/deleted/{id}"]=parameters=>{
            Restaurant selectedRestaurant = Restaurant.Find(parameters.id);
            selectedRestaurant.Delete();
            List<Restaurant> allRestaurants = Restaurant.GetAll();
            Dictionary<string, object> model = new Dictionary<string, object>();
            User currentUser = User.GetCurrentUser();
            model.Add("currentUser", currentUser);
            model.Add("allRestaurants", allRestaurants);
            return View["restaurants.cshtml", model];
              };

              Get["/restaurant/new"]=_=>{
            List<Cuisine> allCuisines = Cuisine.GetAll();
            Dictionary<string, object> model = new Dictionary<string, object>();
            User currentUser = User.GetCurrentUser();
            model.Add("currentUser", currentUser);
            model.Add("allCuisines", allCuisines);
            return View["restaurant_new.cshtml", model];
              };

              Post["/restaurant/success"]=_=>{
            Restaurant newRestaurant = new Restaurant(Request.Form["restaurant-name"], Request.Form["cuisine-id"]);
            newRestaurant.Save();
            List<Restaurant> allRestaurants = Restaurant.GetAll();
            Dictionary<string, object> model = new Dictionary<string, object>();
            User currentUser = User.GetCurrentUser();
            model.Add("currentUser", currentUser);
            model.Add("allRestaurants", allRestaurants);
            return View["restaurants.cshtml", model];
              };

              Patch["/restaurant/success/{id}"]=parameters=>{
            var selectedRestaurant = Restaurant.Find(Request.Form["restaurant-id"]);
            selectedRestaurant.SetName(Request.Form["restaurant-name"]);
            selectedRestaurant.SetCuisineId(Request.Form["cuisine-id"]);
            Dictionary<string, object> model = new Dictionary<string, object>();
            var currentUser= User.GetCurrentUser();
            model.Add("currentUser", currentUser);
            var selectedReviews = selectedRestaurant.GetReviews();
            var allUsers = User.GetAll();
            Dictionary<int, string> usernames = new Dictionary<int, string>{};
            foreach(User reviewer in allUsers)
            {
              usernames.Add(reviewer.GetId(), reviewer.GetName());
            }
            model.Add("users", usernames);
            model.Add("restaurant", selectedRestaurant);
            model.Add("reviews", selectedReviews);
            return View["restaurant.cshtml", model];
              };

              Get["/review/new/{id}"]= parameters =>{
            Dictionary<string, object> model = new Dictionary<string, object>();
            var allUsers = User.GetAll();
            var restaurantToBeReviewed = Restaurant.Find(parameters.id);
            var currentUser= User.GetCurrentUser();
            model.Add("currentUser", currentUser);
            model.Add("users", allUsers);
            model.Add("restaurant", restaurantToBeReviewed);
            return View["review_new.cshtml", model];
              };

              Post["/review/success"]=_=>{
            Review newReview = new Review(Request.Form["review-content"], Request.Form["restaurant-id"], Request.Form["user-id"], Request.Form["rating"]);
            newReview.Save();
            Dictionary<string, object> model = new Dictionary<string, object>();
            var selectedRestaurant = Restaurant.Find(Request.Form["restaurant-id"]);
            double newRating = selectedRestaurant.CalculateAverageRating();
            selectedRestaurant.SetAverageRating(newRating);
            selectedRestaurant = Restaurant.Find(Request.Form["restaurant-id"]);
            var selectedReviews = selectedRestaurant.GetReviews();
            var currentUser= User.GetCurrentUser();
            model.Add("currentUser", currentUser);
            var allUsers = User.GetAll();
            Dictionary<int, string> usernames = new Dictionary<int, string>{};
            foreach(User reviewer in allUsers)
            {
              usernames.Add(reviewer.GetId(), reviewer.GetName());
            }
            model.Add("users", usernames);
            model.Add("restaurant", selectedRestaurant);
            model.Add("reviews", selectedReviews);
            return View["restaurant.cshtml", model];
              };

              Get["/user/new"]=_=>{
            User currentUser = User.GetCurrentUser();
            return View["user_new.cshtml", currentUser];
              };

              Post["/user/success"]=_=>{
            bool isNameTaken = User.IsUserNameTaken(Request.Form["user-name"]);
            User currentUser = User.GetCurrentUser();
            if(isNameTaken)
            {
              return View["user_taken.cshtml", currentUser];
            }
            else
            {
              User newUser = new User(Request.Form["user-name"], Request.Form["display-preference"]);
              newUser.Save();
              newUser.SetCurrentUser();
              User currentUser2 = User.GetCurrentUser();
              return View["index.cshtml", currentUser2];
            }
              };
        }
Пример #8
0
        public void Test_Find_ReturnsASpecificRestaurantObject()
        {
            //Arrange
              Restaurant newRestaurant = new Restaurant("McDonalds", 1);
              newRestaurant.Save();

              //Act
              Restaurant foundRestaurant = Restaurant.Find(newRestaurant.GetId());

              //Assert
              Assert.Equal(newRestaurant, foundRestaurant);
        }
Пример #9
0
        public void Test_SetNameAndId_AdjustsDatabaseCorrectly()
        {
            // Arrange
              Restaurant firstRestaurant = new Restaurant("McDonalds", 1);
              firstRestaurant.Save();

              //Act
              firstRestaurant.SetName("Chipotle");
              firstRestaurant.SetCuisineId(2);
              Restaurant resultRestaurant = Restaurant.Find(firstRestaurant.GetId());

              //Assert
              Assert.Equal(firstRestaurant.GetName(), resultRestaurant.GetName());
              Assert.Equal(firstRestaurant.GetCuisineId(), resultRestaurant.GetCuisineId());
        }
Пример #10
0
        public void Test_Save_SavesSomethingToDatabase()
        {
            //Arrange
              Restaurant newRestaurant = new Restaurant("McDonalds", 1);

              //Act
              newRestaurant.Save();
              int result = Restaurant.GetAll().Count;

              //Assert
              Assert.Equal(1, result);
        }
Пример #11
0
        public void Test_Save_SavesCorrectObjectToDatabase()
        {
            //Arrange
              Restaurant newRestaurant = new Restaurant("McDonalds", 1);

              //Act
              newRestaurant.Save();
              Restaurant savedRestaurant = Restaurant.GetAll()[0];

              //Assert
              Assert.Equal(newRestaurant, savedRestaurant);
        }
Пример #12
0
        public void Test_GetReviews_FindsReviewsByRestaurantId()
        {
            //Arrange
              Restaurant newRestaurant = new Restaurant("Fast Food", 1);
              newRestaurant.Save();

              Review firstReview = new Review("McDonalds", newRestaurant.GetId(), 2);

              firstReview.Save();
              Review secondReview = new Review("Dennys", 2, 2);
              secondReview.Save();
              List<Review> expectedResult = new List<Review> {firstReview};
              //Act
              List<Review> result = newRestaurant.GetReviews();
              //Assert
              Assert.Equal(expectedResult, result);
        }
Пример #13
0
        public List<Restaurant> GetRestaurants()
        {
            List<Restaurant> allRestaurantsMatchingCuisine = new List<Restaurant>{};
              SqlConnection conn = DB.Connection();
              SqlDataReader rdr = null;
              conn.Open();

              SqlCommand cmd = new SqlCommand("SELECT * FROM restaurants WHERE cuisine_id = @cuisineId;", conn);

              SqlParameter cuisineIdParameter = new SqlParameter();
              cuisineIdParameter.ParameterName = "@cuisineId";
              cuisineIdParameter.Value = this.GetId().ToString();

              cmd.Parameters.Add(cuisineIdParameter);

              rdr = cmd.ExecuteReader();

              while(rdr.Read())
              {
            int restaurantId = rdr.GetInt32(0);
            string restaurantName = rdr.GetString(1);
            int restaurantCuisineId = rdr.GetInt32(2);
            double averageRating = rdr.GetDouble(3);
            Restaurant newRestaurant = new Restaurant(restaurantName, restaurantCuisineId, restaurantId, averageRating);
            allRestaurantsMatchingCuisine.Add(newRestaurant);
              }
              if (rdr != null)
              {
            rdr.Close();
              }
              if (conn != null)
              {
            conn.Close();
              }
              return allRestaurantsMatchingCuisine;
        }
Пример #14
0
        public static List<Restaurant> GetAll()
        {
            List<Restaurant> allRestaurants = new List<Restaurant>{};
              SqlConnection conn = DB.Connection();
              SqlDataReader rdr = null;
              conn.Open();

              SqlCommand cmd = new SqlCommand("SELECT * FROM restaurants;", conn);
              rdr = cmd.ExecuteReader();

              while(rdr.Read())
              {
            int restaurantId = rdr.GetInt32(0);
            string restaurantName = rdr.GetString(1);

            int restaurantCuisineId = rdr.GetInt32(2);
            double restaurantAverageRating = rdr.GetDouble(3);
            Restaurant newRestaurant = new Restaurant(restaurantName, restaurantCuisineId, restaurantId, restaurantAverageRating);
            allRestaurants.Add(newRestaurant);
              }
              if (rdr != null)
              {
            rdr.Close();
              }
              if (conn != null)
              {
            conn.Close();
              }
              return allRestaurants;
        }