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

            var sourceUser = await _likesRepository.GetUsersWithLikes(sourceUserId);

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

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

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

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

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

            sourceUser.LikedUsers.Add(userLike);

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

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