public void AcceptFriendRequest(AddFriendDto addFriend) { var existingFriendship = _friendsRepository.GetFirstWhere(x => x.UserId == addFriend.UserId && x.FriendId == addFriend.FriendId); if (existingFriendship != null) { throw new FlowException("Already friends!"); } var newFriend = new Friends() { UserId = addFriend.UserId, FriendId = addFriend.FriendId }; var newFriendTwo = new Friends() { UserId = addFriend.FriendId, FriendId = addFriend.UserId }; _friendsRepository.Add(newFriend); _friendsRepository.Add(newFriendTwo); _friendsRepository.SaveEntities(); }
public async Task <IActionResult> AddFriend(AddFriendDto addFriendDto) { var result = await _mediator.Send(new AddFriendCommand { AddFriendDto = addFriendDto }); return(Ok(result)); }
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()); } }
public ActionResult AddFriend(AddFriendDto addFriend) { try { _friendsService.AcceptFriendRequest(addFriend); } catch (FlowException ex) { return(BadRequest(ex.Message)); } catch (Exception) { return(StatusCode(500, "An error has occured!Try again later!")); } return(Ok()); }