public async Task <ConversationForListDto> CreateConversation(string userId, ConversationForCreationDto conversationForCreationDto)
        {
            if (conversationForCreationDto.FirstUserId != userId)
            {
                throw new InvalidUserIdException(typeof(Conversation));
            }

            if (conversationForCreationDto.FirstUserId == conversationForCreationDto.SecondUserId)
            {
                throw new ModelValidationException("Can't create Conversation with the same user IDs.");
            }

            var conversationExisting = await _unitOfWork.ConversationRepository.IsConversationExistsByUsersId(
                conversationForCreationDto.FirstUserId, conversationForCreationDto.SecondUserId);

            if (conversationExisting)
            {
                throw new EntityExistsException(typeof(Conversation));
            }

            var conversation = _mapper.Map <ConversationForCreationDto, Conversation>(conversationForCreationDto);

            _unitOfWork.ConversationRepository.AddConversation(conversation);
            await _unitOfWork.Commit();

            var conversationForListDto = _mapper.Map <Conversation, ConversationForListDto>(conversation);

            return(conversationForListDto);
        }
Example #2
0
        public async Task <IActionResult> CreateConversation(ConversationForCreationDto conversationForCreationDto)
        {
            var userId = HttpContext.GetUserId();

            var conversation = await _conversationService.CreateConversation(userId, conversationForCreationDto);

            return(CreatedAtAction(nameof(GetConversation), new { id = conversation.Id }, conversation));
        }