public async Task <FriendshipResponse> Create(Friendship friendship) { var existingFriendship = await friendshipRepository.Find(friendship.SrcUserID, friendship.DestUserID); if (existingFriendship != null) { return(new FriendshipResponse(false, "Friendship already exists")); } if (existingFriendship.Status == FriendshipStatus.Blocked) { return(new FriendshipResponse(false, "User doesn't exist")); } var friend = userRepository.FindById(friendship.DestUserID); if (friend == null) { return(new FriendshipResponse(false, "User doesn't exist")); } await friendshipRepository.Add(friendship).ConfigureAwait(false); await unitOfWork.Complete().ConfigureAwait(false); return(new FriendshipResponse(true, null)); }
public async Task <IActionResult> CreateMessage(int userID, MessageForCreationDto messageForCreationDto) { if (userID != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } messageForCreationDto.SenderId = userID; var recipient = await repository.GetUser(messageForCreationDto.RecipientId); if (recipient == null) { return(BadRequest("no user found")); } var message = mapper.Map <Message>(messageForCreationDto); repository.Add(message); if (await repository.SaveAll()) { var messageToReturn = mapper.Map <MessageForCreationDto>(message); return(CreatedAtRoute("GetMessage", new { userID, id = message.Id }, messageToReturn)); } throw new Exception("creating new message failed to save"); }
public async Task <IActionResult> AddUser(int id, int recipientId) { if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var add = await repo.GetAdd(id, recipientId); if (add != null) { return(BadRequest(" you added this user")); } if (await repo.GetUser(recipientId) == null) { return(NotFound()); } add = new Add { AdderId = id, AddedId = recipientId }; repo.Add <Add>(add); if (await repo.SaveAll()) { return(Ok()); } return(BadRequest("failed to add user")); }