コード例 #1
0
        private async Task ValidatePrivateChatAsync(CreatePrivateChatDto chatDto, CancellationToken ct)
        {
            User user =
                await _unitOfWork.UserRepository.GetAsync(chatDto.ContactId, ct);

            if (user == null)
            {
                _logger.LogWarning("User {UserId} not found", chatDto.ContactId);

                throw new UserNotFoundException();
            }

            PrivateChat dbChat =
                await _unitOfWork.PrivateChatRepository.GetChatByUserIdAsync(chatDto.UserId, chatDto.ContactId, ct);

            if (dbChat != null)
            {
                _logger.LogWarning(
                    "User {UserId} already has private chat with Contact {ContactId}",
                    chatDto.UserId,
                    chatDto.ContactId);

                throw new PrivateChatAlreadyExistException();
            }
        }
コード例 #2
0
        public async Task <GetPrivateChatDto> CreatePrivateChatAsync(CreatePrivateChatDto chatDto, CancellationToken ct = default)
        {
            _logger.LogInformation("Create private chat {PrivateChat}", chatDto);

            await ValidatePrivateChatAsync(chatDto, ct);

            PrivateChat chat = _mapper.Map <PrivateChat>(chatDto);

            chat.CreatedAt = chat.UpdatedAt = DateTime.UtcNow;

            _unitOfWork.PrivateChatRepository.Create(chat);

            await _unitOfWork.CommitAsync(ct);

            return(_mapper.Map <GetPrivateChatDto>(chat));
        }
コード例 #3
0
        public async Task <IActionResult> CreateChatAsync([FromBody] CreatePrivateChatDto privateChatDto)
        {
            try
            {
                privateChatDto.UserId = User.GetUserId();

                GetPrivateChatDto chat = await _privateChatService.CreatePrivateChatAsync(privateChatDto);

                return(Ok(chat));
            }
            catch (UserNotFoundException)
            {
                return(BadRequest());
            }
            catch (PrivateChatAlreadyExistException)
            {
                return(BadRequest());
            }
        }