예제 #1
0
        public IActionResult ToggleLike(Like like)
        {
            User user = userDAO.GetUser(User.Identity.Name);

            like.UserId = user.Id;
            LikedByUser output = likeDAO.ToggleLike(like.PhotoId, like.UserId);

            return(Ok(output));
        }
예제 #2
0
        /// <summary>
        /// Allows a site user to add a "like" to a given photo, or remove a like they have previously added
        /// </summary>
        /// <param name="photoId"></param>
        /// <param name="userId"></param>
        public LikedByUser ToggleLike(int photoId, int userId)
        {
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();
                LikedByUser output = new LikedByUser();
                // Find out if photo is already liked by user
                SqlCommand AddOrDeleteLike = new SqlCommand("SELECT * FROM likes WHERE photoId = @photoId and userId = @userId", conn);
                AddOrDeleteLike.Parameters.AddWithValue("@photoId", photoId);
                AddOrDeleteLike.Parameters.AddWithValue("@userId", userId);

                if (AddOrDeleteLike.ExecuteScalar() != null)
                {
                    AddOrDeleteLike = new SqlCommand("DELETE likes WHERE photoId = @photoId AND userId = @userId", conn);
                    AddOrDeleteLike.Parameters.AddWithValue("@photoId", photoId);
                    AddOrDeleteLike.Parameters.AddWithValue("@userId", userId);
                    AddOrDeleteLike.ExecuteReader();
                }
                else
                {
                    AddOrDeleteLike = new SqlCommand("INSERT likes (photoId, userId) VALUES (@photoId, @userId)", conn);
                    AddOrDeleteLike.Parameters.AddWithValue("@photoId", photoId);
                    AddOrDeleteLike.Parameters.AddWithValue("@userId", userId);
                    AddOrDeleteLike.ExecuteReader();
                }

                SqlCommand TotalLikes = new SqlCommand("SELECT COUNT(*) FROM likes WHERE photoId = @photoId", conn);
                TotalLikes.Parameters.AddWithValue("@photoId", photoId);
                TotalLikes.Parameters.AddWithValue("@userId", userId);
                output.TotalLikes = Convert.ToInt32(TotalLikes.ExecuteScalar());

                output.PhotoId = photoId;

                SqlCommand Liked = new SqlCommand("SELECT * FROM likes WHERE photoId = @photoId and userId = @userId", conn);
                Liked.Parameters.AddWithValue("@photoId", photoId);
                Liked.Parameters.AddWithValue("@userId", userId);
                output.Liked = Convert.ToBoolean(Liked.ExecuteScalar());


                return(output);
            }
        }