Пример #1
0
        public async Task <IActionResult> PostRestaurantVote(int id, string upOrDown)
        {
            // If there is already an existing vote, return a bad request
            var existingVote = await _context.RestaurantVotes.AnyAsync(restaurantVote => restaurantVote.UserId == GetCurrentUserId() && restaurantVote.RestaurantId == id);

            if (existingVote)
            {
                return(BadRequest());
            }

            // Add the restaurant vote to the table
            var restaurantVote = new RestaurantVote
            {
                RestaurantId = id,
                UserId       = GetCurrentUserId(),
                UpOrDown     = upOrDown
            };
            await _context.RestaurantVotes.AddAsync(restaurantVote);


            // Find the restaurant in the database using `FindAsync` to look it up by id
            var restaurant = await _context.Restaurants.FindAsync(id);

            // If we didn't find anything, we receive a `null` in return
            if (restaurant == null)
            {
                // Return a `404` response to the client indicating we could not find a restaurant with this id
                return(NotFound());
            }

            switch (upOrDown)
            {
            case "upvote":
                restaurant.IncrementUpvoteCount();
                break;

            case "downvote":
                restaurant.IncrementDownvoteCount();
                break;

            default:
                return(BadRequest());
            }

            // Tell the database to consider everything in restaurant to be _updated_ values. When
            // the save happens the database will _replace_ the values in the database with the ones from restaurant
            _context.Entry(restaurant).State = EntityState.Modified;

            // Try to save these changes.
            await _context.SaveChangesAsync();

            // return NoContent to indicate the update was done.
            return(NoContent());
        }
Пример #2
0
        public IActionResult AddLikeOrDislike(RestaurantVote restaurantVote)
        {
            bool result = false;

            if (restaurantVote.WasLiked == true)
            {
                result = restLikesDislikesDAO.AddLike(restaurantVote.RestaurantId);
            }
            else
            {
                result = restLikesDislikesDAO.AddDislike(restaurantVote.RestaurantId);
            }

            return(Ok(result));
        }