Пример #1
0
        public async Task <ActionResult> AddLike(string username)
        {
            var sourceUserId = User.GetUserId();
            var likedUser    = await _userRepository.GetUserByUserNameAsync(username);

            var sourceUser = await _likesRepository.GetUserWithLikes(sourceUserId);

            if (likedUser == null)
            {
                return(NotFound());
            }

            if (sourceUser.UserName == username)
            {
                return(BadRequest("You cannot like yourself"));
            }

            var userLike = await _likesRepository.GetUserLiked(sourceUserId, likedUser.Id);

            if (userLike != null)
            {
                return(BadRequest("You already liked this user"));
            }

            userLike = new UserLike
            {
                SourceUserId = sourceUserId,
                LikesUserId  = likedUser.Id
            };

            if (await _userRepository.SaveAllAsync())
            {
                return(Ok());
            }

            return(BadRequest("falied to like user"));
        }