public IHttpActionResult RateExistingRestaurant(int id, [FromBody]RateBindingModel model) { if (model == null) { return this.BadRequest("Model cannot be null (no data in request)"); } if (!this.ModelState.IsValid) { return this.BadRequest("Invalid rating. The rating value must be between 1 and 10."); } var restaurant = this.Data.Restaurants.Find(id); if (restaurant == null) { return this.NotFound(); } var loggedUserId = this.User.Identity.GetUserId(); var user = this.Data.Users.Find(loggedUserId); if (restaurant.OwnerId == loggedUserId) { return this.BadRequest("The owner cannot rate his own restaurant."); } if (user.GivenRatings .Any(r => r.RestaurantId == id)) { var ratingFromDb = this.Data.Ratings.All() .FirstOrDefault(r => r.RestaurantId == id && r.UserId == loggedUserId); if (ratingFromDb != null) { ratingFromDb.Stars = model.Stars; } this.Data.SaveChanges(); return this.Ok(); }; var rating = new Rating() { RestaurantId = id, Restaurant = restaurant, Stars = model.Stars, User = user, UserId = loggedUserId }; this.Data.Ratings.Add(rating); this.Data.SaveChanges(); return this.Ok(); }
public IHttpActionResult RateRestaurant(int id, RatingBindingModel ratingBindingModel) { var userId = this.User.Identity.GetUserId(); var restaurant = this.Data.Restaurants.GetAll().FirstOrDefault(r => r.Id == id); if (restaurant == null) { return this.NotFound(); } if (ratingBindingModel == null) { return this.BadRequest("Data is missing"); } if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } if (restaurant.OwnerId == userId) { return this.BadRequest("Owner cannot rate its restaurants."); } var rating = this.Data.Ratings.GetAll() .FirstOrDefault(r => r.UserId == userId && r.RestaurantId == restaurant.Id); if (rating == null) { rating = new Rating { Stars = ratingBindingModel.Stars, UserId = userId, RestaurantId = restaurant.Id }; this.Data.Ratings.Add(rating); } else { rating.Stars = ratingBindingModel.Stars; } this.Data.SaveChanges(); return this.Ok(); }
public IHttpActionResult AddRating(int id, RateInputModel ratingData) { if (ratingData == null) { return BadRequest("Missing rating data."); } if (!ModelState.IsValid) { return BadRequest(ModelState); } if(ratingData.Stars < 1 || ratingData.Stars > 10) { return this.BadRequest("Incorrect stars"); } var restaurant = db.Restaurants.Find(id); if (restaurant == null) { return this.NotFound(); } var currentUser = User.Identity.IsAuthenticated; var userName = User.Identity.Name; if (!currentUser) { return this.Unauthorized(); } var userId = db.Users .Where(u => u.UserName == userName) .Select(u => u.Id).FirstOrDefault(); var ownerRestaurantId = db.Restaurants .Where(r => r.OwnerId == userId && r.Id == id) .Select(r => r.Id).FirstOrDefault(); if(ownerRestaurantId == id) { return this.BadRequest("The restaurant owner can not rates his own restaurant!!!"); } var rating = new Rating() { UserId = userId, RestaurantId = id, Stars = ratingData.Stars }; db.Ratings.Add(rating); db.SaveChanges(); return this.Ok(); }
public IHttpActionResult RateRestaurant(int restaurantId, RatingBindingModel ratingModel) { if (ratingModel == null) { return this.BadRequest("Rating data cannot be null."); } if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } var loggedUserId = this.UserIdProvider.GetUserId(); if (loggedUserId == null) { return this.Unauthorized(); } var loggedUser = this.Data.ApplicationUsers.All() .FirstOrDefault(u => u.Id == loggedUserId); var existingRestaurant = this.Data.Restaurants.All() .FirstOrDefault(r => r.Id == restaurantId); if (existingRestaurant == null) { return this.NotFound(); } if (existingRestaurant.OwnerId == loggedUserId) { return this.BadRequest("Restaurant owner cannot rate his own restaurant."); } if (loggedUser.GivenRatings.Any(r => r.RestaurantId == restaurantId)) { return this.BadRequest("Already rated that restaurant."); } var rating = new Rating { Stars = ratingModel.Stars, RestaurantId = restaurantId, Restaurant = existingRestaurant, User = loggedUser, UserId = loggedUserId }; this.Data.Ratings.Add(rating); existingRestaurant.Ratings.Add(rating); loggedUser.GivenRatings.Add(rating); this.Data.ApplicationUsers.Update(loggedUser); this.Data.Restaurants.Update(existingRestaurant); this.Data.SaveChanges(); return this.Ok(); }
public IHttpActionResult PostRestaurant(int id, RestaurantRateBindingModel restaurantRating) { if (!User.Identity.IsAuthenticated) { return Unauthorized(); } int stars = int.Parse(restaurantRating.Stars); if (stars < 0 || stars > 10) { return this.BadRequest(); } if (db.Restaurants.Find(id) == null) { return this.NotFound(); } var currentUserId = User.Identity.GetUserId(); var currentUser = this.db.Users.Find(currentUserId); if (db.Ratings.All().Any(r => r.RestaurantId == id && r.UserId == currentUserId)) { return this.BadRequest(); } if (db.Restaurants.Find(id).OwnerId == currentUserId) { return this.BadRequest(); } Rating rating = new Rating() { Stars = stars, Restaurant = db.Restaurants.Find(id), RestaurantId = id, User = currentUser, UserId = currentUserId }; db.Ratings.Add(rating); db.SaveChanges(); return this.Ok(); }
public IHttpActionResult RateRestaurants(int id, RatingBindingModel m) { if (!this.Data.Restaurants.Any(r => r.Id == id)) return this.NotFound(); if (this.Data.Restaurants.FirstOrDefault(r => r.Id == id).OwnerId == User.Identity.GetUserId()) return this.BadRequest("You can't rate your own resturant."); if (m.Stars > 10 || m.Stars <= 0) return this.BadRequest("Invalid value for stars."); var uId = User.Identity.GetUserId(); if (this.Data.Ratings.Any(r => r.UserId == uId && r.RestaurantId == id)) { var restaurant = this.Data.Ratings.FirstOrDefault( r => r.UserId == uId && r.RestaurantId == id); restaurant.Stars = m.Stars; this.Data.SaveChanges(); return this.Ok(); } var rating = new Rating { RestaurantId = id, UserId = uId, Stars = m.Stars }; this.Data.Ratings.Add(rating); this.Data.SaveChanges(); return this.Ok(); }
public IHttpActionResult PostRatingByRestaurantId(int id, [FromBody]RatingBindingModel model) { var userId = this.User.Identity.GetUserId(); if (userId == null) { return this.Unauthorized(); } var restaurant = db.Restaurants.FirstOrDefault(r => r.Id == id); if (restaurant == null) { return this.NotFound(); } var ownerId = restaurant.OwnerId; if (model.Stars < 1 || model.Stars > 10 || userId == ownerId) { return this.BadRequest(); } var ratingToAdd = new Rating() { UserId = userId, RestaurantId = restaurant.Id, Stars = model.Stars }; db.Ratings.Add(ratingToAdd); db.SaveChanges(); return this.Ok(); }
public IHttpActionResult Rate(int id, RateRestaurantBindingModel model) { var restaurant = this.Data.Restaurants.Find(id); if (restaurant == null) { return this.NotFound(); } if (model == null) { return this.BadRequest("Model cannot be null."); } if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } var loggedInUserId = this.User.Identity.GetUserId(); if (loggedInUserId == restaurant.OwnerId) { return this.BadRequest("Cannot rate own restaurant."); } var rating = restaurant.Ratings.FirstOrDefault(r => r.UserId == loggedInUserId); if (rating != null) { rating.Stars = model.Stars; } else { rating = new Rating { RestaurantId = restaurant.Id, UserId = loggedInUserId, Stars = model.Stars }; this.Data.Ratings.Add(rating); } this.Data.SaveChanges(); return this.Ok(); }
public IHttpActionResult RateRestaurant(int id, RateRestaurantModel ratingRestaurantModel) { if (ratingRestaurantModel.Stars < 1 || ratingRestaurantModel.Stars > 10) { return this.BadRequest("Rating must be in range 1 - 10"); } var currentUserId = User.Identity.GetUserId(); if (currentUserId == null) { return this.Unauthorized(); } var restaurant = db.Restaurants.FirstOrDefault(r => r.Id == id); if (restaurant == null) { return this.NotFound(); } if (currentUserId == restaurant.OwnerId) { return this.BadRequest("Restaurant owner not rates his own restaurant."); } var user = db.Users.FirstOrDefault(u => u.Id == currentUserId); var currentRating = db.Ratings.FirstOrDefault(r => r.RestaurantId == restaurant.Id && r.UserId == currentUserId); if (currentRating == null) { var rating = new Rating() { User = user, Restaurant = restaurant, Stars = ratingRestaurantModel.Stars }; db.Ratings.Add(rating); } else { currentRating.Stars = ratingRestaurantModel.Stars; } db.SaveChanges(); return this.Ok(); }
public IHttpActionResult RateResraunt(int id, [FromBody]RateRestaurantBindingModel model) { if (model == null) { return BadRequest("Missing restraunt rating data."); } if (!ModelState.IsValid) { return this.BadRequest(this.ModelState); } Restaurant restrauntDB = db.Restaurants.FirstOrDefault(o => o.Id == id); if (restrauntDB == null) { return NotFound(); } var currentUserId = User.Identity.GetUserId(); var currentUser = this.db.Users.Find(currentUserId); if (currentUserId == restrauntDB.OwnerId) { return this.Content(HttpStatusCode.BadRequest, new { Message = "Your cannot rate your own restraunt." }); } //if (!restrauntDB.Ratings.Any()) //{ // ratings.Add(model.Stars); //} if (model.Stars < 0 || model.Stars > 10) { return this.Content(HttpStatusCode.BadRequest, new { Message = "Your rating should be in [1..10] interval" }); } var rating = new Rating() { Stars = model.Stars, RestaurantId = restrauntDB.Id, UserId = currentUserId }; db.Ratings.Add(rating); db.SaveChanges(); return this.Ok(); }
public IHttpActionResult RateExistingRestaurant([FromUri] int id, [FromBody] RateBindingModel model) { if (model == null) { return this.BadRequest(); } if (!ModelState.IsValid) { return BadRequest(ModelState); } var restaurant = this.db.Restaurants.All().FirstOrDefault(r => r.Id == id); if (restaurant == null) { return this.NotFound(); } var currUserId = User.Identity.GetUserId(); var currUser = db.Users.Find(currUserId); var rating = this.db.Ratings.All().FirstOrDefault(rat => rat.RestaurantId == restaurant.Id && rat.UserId == currUserId); if (rating == null) { rating = new Rating() { Stars = model.Stars, Restaurant = restaurant, User = currUser ?? null }; } else { rating.Stars = model.Stars; } this.db.Ratings.Update(rating); this.db.SaveChanges(); return this.Ok(); }
public IHttpActionResult RateRestaurant(int id, [FromBody] RateBindingModel model) { var restaurant = this.Data.Restaurants.Find(id); if (restaurant == null) { return this.NotFound(); } if (model == null) { return this.BadRequest("Model cannot be null"); } if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } string loggedUserId = this.User.Identity.GetUserId(); if (restaurant.OwnerId == loggedUserId) { return this.BadRequest("You cannot rate your own restaurant."); } var existingRating = this.Data.Ratings.All().FirstOrDefault(r=>r.UserId==loggedUserId && r.RestaurantId==id); if (existingRating == null) { var rating = new Rating() { Stars = model.Stars, RestaurantId = id, UserId = loggedUserId }; this.Data.Ratings.Add(rating); } else { existingRating.Stars = model.Stars; } this.Data.SaveChanges(); return this.Ok(); }
public IHttpActionResult RateRestaurant(int id, [FromUri]int stars) { var restaurant = this.Data.Restaurants.All() .Where(r => r.Id == id) .Select(r => new { Id = r.Id, OwnerId = r.OwnerId, AlreadyRatedUsersIds = r.Ratings.Select(ra => ra.UserId) }) .FirstOrDefault(); if (restaurant == null) { return this.NotFound(); } string loggedUserId = this.User.Identity.GetUserId(); var dbUser = this.Data.Users.All() .FirstOrDefault(u => u.Id == loggedUserId); if (dbUser == null) { return this.BadRequest("Invalid Token"); } if (loggedUserId == restaurant.OwnerId) { return this.BadRequest("You cannot rate your own restaurant."); } if (restaurant.AlreadyRatedUsersIds.Contains(loggedUserId)) { return this.BadRequest("You already rated this restaurant."); } if (stars < 1 || stars > 10) { return this.BadRequest("Rate must be from 1 to 10."); } var rate = new Rating { Stars = stars, RestaurantId = restaurant.Id, UserId = loggedUserId }; this.Data.Ratings.Add(rate); this.Data.SaveChanges(); return this.Ok(); }