public async Task <IActionResult> CreateChat([FromBody] ChatCreateDto chatCreateDto)
        {
            var user = _unitOfWork.Users.GetByID(chatCreateDto.OwnerId);

            if (user == null)
            {
                return(NotFound());
            }
            var chat = new Chat
            {
                Name        = chatCreateDto.Name,
                IsGroupChat = true,
                Owner       = user
            };
            var userChat = new UsersChats
            {
                User = user,
                Chat = chat
            };

            chat.UsersChats.Add(userChat);
            await _chatsManager.CreateChatAsync(chat);

            return(Created("", chatCreateDto));
        }
Beispiel #2
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());
            }
        }