예제 #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_ReturnsTrueIfContentAndRestaurantAreIdentical()
        {
            //Arrange
              Review firstReview = new Review("McDonalds", 1, 2);
              Review secondReview = new Review("McDonalds", 1, 2);

              //Assert
              Assert.Equal(firstReview, secondReview);
        }
예제 #3
0
        public void Test_RatingIsPassedToDatabase()
        {
            // Arrange
              Review firstReview = new Review("McDonalds", 1, 2);
              firstReview.Save();

              //Act
              Review resultReview = Review.Find(firstReview.GetId());

              //Assert
              Assert.Equal(firstReview.GetRating(), resultReview.GetRating());
        }
예제 #4
0
        public void Test_Find_ReturnsASpecificReviewObject()
        {
            //Arrange
              Review newReview = new Review("McDonalds", 1, 2);
              newReview.Save();

              //Act
              Review foundReview = Review.Find(newReview.GetId());

              //Assert
              Assert.Equal(newReview, foundReview);
        }
예제 #5
0
        public void Test_DeleteOne_DeletesASpecificReviewObject()
        {
            //Arrange
              Review firstReview = new Review("McDonalds", 1, 2);
              firstReview.Save();
              Review secondReview = new Review("Dennys", 2, 2);
              secondReview.Save();

              //Act
              secondReview.Delete();
              List<Review> expectedReview = new List<Review> {firstReview};
              List<Review> testReview= Review.GetAll();

              //Assert
              Assert.Equal(expectedReview, testReview);
        }
예제 #6
0
        public void Test_GetReviews_FindsReviewsByUserId()
        {
            //Arrange
              User newUser = new User("Bob", 2);
              newUser.Save();

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

              firstReview.Save();
              Review secondReview = new Review("Dennys", 2, 2);
              secondReview.Save();
              List<Review> expectedResult = new List<Review> {firstReview};
              //Act
              List<Review> result = newUser.GetReviews();
              //Assert
              Assert.Equal(expectedResult, result);
        }
예제 #7
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);
        }
예제 #8
0
        public static Review Find(int reviewId)
        {
            SqlConnection conn = DB.Connection();
              SqlDataReader rdr;
              conn.Open();

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

              SqlParameter reviewIdParameter = new SqlParameter();
              reviewIdParameter.ParameterName = "@reviewId";
              reviewIdParameter.Value = reviewId.ToString();

              cmd.Parameters.Add(reviewIdParameter);

              int foundReviewId = 0;
              string reviewContent = null;
              int reviewRestaurantId = 0;
              int reviewUserId = 0;
              int reviewRating = 0;
              rdr = cmd.ExecuteReader();

              while(rdr.Read())
              {
            foundReviewId = rdr.GetInt32(0);
            reviewContent = rdr.GetString(1);
            reviewRestaurantId = rdr.GetInt32(2);
            reviewUserId = rdr.GetInt32(3);
            reviewRating = rdr.GetInt32(4);
              }
              Review newReview = new Review(reviewContent, reviewRestaurantId, reviewUserId, reviewRating, foundReviewId);
              if (rdr != null)
              {
            rdr.Close();
              }
              if (conn != null)
              {
            conn.Close();
              }
              return newReview;
        }
예제 #9
0
        public void Test_Delete_DeletesUserAndReviewsByUserId()
        {
            //Arrange
              User firstUser = new User("Bob", 2);
              firstUser.Save();
              User secondUser = new User("Larry", 2);
              secondUser.Save();

              Review firstReview = new Review("McDonalds", 2, firstUser.GetId());
              firstReview.Save();
              Review secondReview = new Review("Chipotle", 2, secondUser.GetId());
              secondReview.Save();

              List<User> expectedUser = new List<User>{firstUser};
              List<Review> expectedResult = new List<Review> {firstReview};
              //Act
              secondUser.Delete();

              List<User> resultingUser = User.GetAll();
              List<Review> result = Review.GetAll();
              //Assert
              Assert.Equal(expectedUser, resultingUser);
              Assert.Equal(expectedResult, result);
        }
예제 #10
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];
            }
              };
        }
예제 #11
0
        public static List<Review> GetAll()
        {
            List<Review> allReviews = new List<Review>{};
              SqlConnection conn = DB.Connection();
              SqlDataReader rdr = null;
              conn.Open();

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

              while(rdr.Read())
              {
            int reviewId = rdr.GetInt32(0);
            string reviewContent = rdr.GetString(1);
            int reviewRestaurantId = rdr.GetInt32(2);
            int reviewUserId = rdr.GetInt32(3);
            int reviewRating = rdr.GetInt32(4);
            Review newReview = new Review(reviewContent, reviewRestaurantId, reviewUserId, reviewRating, reviewId);
            allReviews.Add(newReview);
              }
              if (rdr != null)
              {
            rdr.Close();
              }
              if (conn != null)
              {
            conn.Close();
              }
              return allReviews;
        }
예제 #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 void Test_Save_SavesCorrectObjectToDatabase()
        {
            //Arrange
              Review newReview = new Review("McDonalds", 1, 2);

              //Act
              newReview.Save();
              Review savedReview = Review.GetAll()[0];

              //Assert
              Assert.Equal(newReview, savedReview);
        }
예제 #14
0
        public void Test_SetContentAndId_AdjustsDatabaseCorrectly()
        {
            // Arrange
              Review firstReview = new Review("McDonalds", 1, 2);
              firstReview.Save();

              //Act
              firstReview.SetContent("Chipotle");
              firstReview.SetRestaurantId(2);
              Review resultReview = Review.Find(firstReview.GetId());

              //Assert
              Assert.Equal(firstReview.GetContent(), resultReview.GetContent());
              Assert.Equal(firstReview.GetRestaurantId(), resultReview.GetRestaurantId());
        }
예제 #15
0
        public void Test_Save_SavesSomethingToDatabase()
        {
            //Arrange
              Review newReview = new Review("McDonalds", 1, 2);

              //Act
              newReview.Save();
              int result = Review.GetAll().Count;

              //Assert
              Assert.Equal(1, result);
        }
예제 #16
0
        public List<Review> GetReviews()
        {
            List<Review> allReviewsMatchingRestaurant = new List<Review>{};
              SqlConnection conn = DB.Connection();
              SqlDataReader rdr = null;
              conn.Open();

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

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

              cmd.Parameters.Add(restaurantIdParameter);

              rdr = cmd.ExecuteReader();

              while(rdr.Read())
              {
            int reviewId = rdr.GetInt32(0);
            string reviewName = rdr.GetString(1);
            int reviewRestaurantId = rdr.GetInt32(2);
            int reviewUserId = rdr.GetInt32(3);
            int reviewRating = rdr.GetInt32(4);
            Review newReview = new Review(reviewName, reviewRestaurantId, reviewUserId, reviewRating, reviewId);
            allReviewsMatchingRestaurant.Add(newReview);
              }
              if (rdr != null)
              {
            rdr.Close();
              }
              if (conn != null)
              {
            conn.Close();
              }
              return allReviewsMatchingRestaurant;
        }