Esempio n. 1
0
        private async Task ValidateFriendshipForCreation(string userId, FriendshipForCreationDto friendshipDto)
        {
            if (userId != friendshipDto.SenderId)
            {
                throw new InvalidUserIdException(typeof(Friendship));
            }

            var isUserExists = await _unitOfWork.UserRepository.IsUserWithIdExists(friendshipDto.ReceiverId);

            if (!isUserExists)
            {
                throw new EntityNotFoundException(typeof(User), friendshipDto.ReceiverId);
            }

            if (friendshipDto.ReceiverId == friendshipDto.SenderId)
            {
                throw new ModelValidationException("Can't create Friendship with the same user IDs.");
            }

            var friendshipExisting = await _unitOfWork.FriendshipRepository.IsFriendshipExistsBySenderAndReceiverId(
                friendshipDto.SenderId, friendshipDto.ReceiverId);

            if (friendshipExisting)
            {
                throw new EntityExistsException(typeof(Friendship));
            }
        }
        public async Task <IActionResult> CreateFriendship([FromBody] FriendshipForCreationDto friendshipDto)
        {
            var userId = HttpContext.GetUserId();

            var friendship = await _friendshipService.CreateFriendshipRequest(userId, friendshipDto);

            return(CreatedAtAction(nameof(GetFriendship), new { id = friendship.Id }, friendship));
        }
Esempio n. 3
0
        public async Task <FriendshipDto> CreateFriendshipRequest(string userId, FriendshipForCreationDto friendshipForCreation)
        {
            await ValidateFriendshipForCreation(userId, friendshipForCreation);

            var friendship = _mapper.Map <Friendship>(friendshipForCreation);

            friendship.Status            = FriendshipStatus.Pending;
            friendship.StatusChangedDate = DateTime.UtcNow;

            _unitOfWork.FriendshipRepository.AddFriendship(friendship);
            await _unitOfWork.Commit();

            var friendshipDto = _mapper.Map <FriendshipDto>(friendship);

            return(friendshipDto);
        }