Beispiel #1
0
        public async Task <IActionResult> AddFriend([FromBody] AddFriendDto addFriendDto)
        {
            var currentUser = await _signInManager.UserManager.GetUserAsync(User);

            var user = _unitOfWork.Users.GetByID(addFriendDto.UserId);

            if (user == null)
            {
                return(BadRequest());
            }
            if (currentUser.Id != user.Id)
            {
                return(Unauthorized());
            }
            var friend = _unitOfWork.Users.GetByName(addFriendDto.FriendsName);

            if (friend == null)
            {
                return(NotFound());
            }
            if (user == friend)
            {
                return(BadRequest());
            }
            bool result = await _friendsManager.AddFriendAsync(user, friend);

            if (result)
            {
                var chat = new Chat()
                {
                    IsGroupChat = false
                };
                var userChats1 = new UsersChats()
                {
                    User = user,
                    Chat = chat
                };
                var userChats2 = new UsersChats()
                {
                    User = friend,
                    Chat = chat
                };
                chat.UsersChats.Add(userChats1);
                chat.UsersChats.Add(userChats2);
                await _chatsManager.CreateChatAsync(chat);

                return(Created("", addFriendDto));
            }
            else
            {
                return(NotFound());
            }
        }
Beispiel #2
0
 public async Task AddFriendAsync(int friendId)
 {
     var isSuccessResponce = await _friendsManager.AddFriendAsync(friendId);
 }