public async Task <IActionResult> RemoveFromChat([FromBody] AddOrRemoveUserToChatDto removeUserFromChatDto) { var user = await _userManager.GetUserAsync(User); var userToRemove = _unitOfWork.Users.GetByID(removeUserFromChatDto.UserId); if (userToRemove == null) { return(NotFound()); } if (user == userToRemove) { await _chatsManager.RemoveUserFromChatAsync(removeUserFromChatDto.ChatId, removeUserFromChatDto.UserId); return(Ok(removeUserFromChatDto)); } var chat = _unitOfWork.Chats.GetByID(removeUserFromChatDto.ChatId); if (chat.Owner == user) { await _chatsManager.RemoveUserFromChatAsync(removeUserFromChatDto.ChatId, removeUserFromChatDto.UserId); return(Ok(removeUserFromChatDto)); } return(Unauthorized()); }
public async Task <IActionResult> AddToChat([FromBody] AddOrRemoveUserToChatDto addUserToChatDto) { var user = await _userManager.GetUserAsync(User); var chat = _unitOfWork.Chats.GetByID(addUserToChatDto.ChatId); if (chat == null) { return(NotFound()); } if (chat.Owner != user) { return(Unauthorized()); } if (chat.UsersChats.Where(uc => uc.User.Id == addUserToChatDto.UserId).Count() > 0) { return(Conflict(addUserToChatDto)); } await _chatsManager.AddUserToChatAsync(addUserToChatDto.ChatId, addUserToChatDto.UserId); return(Ok(addUserToChatDto)); }