예제 #1
0
        public async Task <IActionResult> LikeAction(int userId, int activityId)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            //Primary Key userid+postid
            var activityLikes = await _activityRepo.GetActivityLikes(activityId);

            var like = new ActivityLike {
                ActivityId = activityId,
                UserId     = userId,
                Value      = 1
            };

            var dislike = new ActivityLike {
                ActivityId = activityId,
                UserId     = userId,
                Value      = -1
            };

            var likeDeleted = false;

            if (activityLikes.Any(a => a.UserId == userId))
            {
                if (activityLikes.Any(a => a.UserId == userId && a.Value == 1))
                {
                    likeDeleted = true;
                    _mainRepo.Delete <ActivityLike>(like);
                }
                else
                {
                    _mainRepo.Delete <ActivityLike>(dislike);
                }
            }

            if (!likeDeleted)
            {
                _mainRepo.Add <ActivityLike>(like);
            }

            try {
                if (await _mainRepo.SaveAll())
                {
                    return(NoContent());
                }
            }catch {
                return(BadRequest("Failed"));
            }

            return(BadRequest("Failed"));
        }
예제 #2
0
        public async Task <ResponseWrapper <Activity> > LikeActivity(ApplicationUser user, int activityId)
        {
            var response = new ResponseWrapper <Activity>
            {
                Success = true,
                Status  = "",
            };

            var activity = await _UserRepo.GetActivityById(activityId);

            if (activity == null)
            {
                response.Success = false;
                response.Status  = "Activity does not exist with id.";
                return(response);
            }

            var activityLike = new ActivityLike()
            {
                Activity = activity,
                User     = user
            };

            var updatedActivity = await _UserRepo.LikeActivity(activity, activityLike);

            if (updatedActivity == null)
            {
                response.Success = false;
                response.Status  = "Like activity action failed.";
                return(response);
            }

            response.Data = updatedActivity;

            return(response);
        }