コード例 #1
0
        public async Task <ActionResult> Create([FromBody] ChatRoomCreationRequestDTO chatRoomCreationRequestDTO)
        {
            _logger.LogInformation($"New chat room request: {chatRoomCreationRequestDTO}");

            var validator        = new ChatRoomCreationValidator();
            var validationResult = validator.Validate(chatRoomCreationRequestDTO);

            if (!validationResult.IsValid)
            {
                return(BadRequest(validationResult.Errors));
            }

            try
            {
                var result = await _chatRoomService.CreateChatRoom(chatRoomCreationRequestDTO);

                return(StatusCode(201, result));
            }
            catch (ObjectAlreadyExistsException e)
            {
                return(BadRequest(new ServiceErrorDTO {
                    Message = e.Message
                }));
            }
        }
コード例 #2
0
        public async Task TestChatRoomServiceShouldCreateAndRetrieveNewChatRoom()
        {
            // arrange
            var chatRoomService    = ServiceProvider.GetRequiredService <IChatRoomService>();
            var newChatRoomRequest = new ChatRoomCreationRequestDTO
            {
                Name = "New chat room",
                ActiveConnectionsLimit = 4
            };

            // act
            var result = await chatRoomService.CreateChatRoom(newChatRoomRequest);

            // assert
            var persistedChatRoom = await DB.ChatRooms.FindAsync(result.Id);  // from db

            var serviceResult = await chatRoomService.GetChatRoom(result.Id); // from chat service

            Assert.Equal(newChatRoomRequest.Name, persistedChatRoom.Name);
            Assert.Equal(newChatRoomRequest.ActiveConnectionsLimit, persistedChatRoom.ActiveConnectionsLimit);

            Assert.Equal(newChatRoomRequest.Name, serviceResult.Name);
            Assert.Equal(newChatRoomRequest.ActiveConnectionsLimit, serviceResult.ActiveConnectionsLimit);
            Assert.Equal(persistedChatRoom.Id, serviceResult.Id);
        }
コード例 #3
0
        //
        // Summary:
        //   Creates a new chat room. If the room name is already taken, then it raises an exception.
        public async Task <ChatRoomResponseDTO> CreateChatRoom(ChatRoomCreationRequestDTO chatRoomCreationRequestDTO)
        {
            var roomWithSameName = await _db.ChatRooms.Where(room => room.Name == chatRoomCreationRequestDTO.Name).FirstOrDefaultAsync();

            if (roomWithSameName != null)
            {
                throw new ObjectAlreadyExistsException("A chat room with this name already exists!");
            }

            var newChatRoom = _mapper.Map <ChatRoomCreationRequestDTO, ChatRoom>(chatRoomCreationRequestDTO);

            await _db.ChatRooms.AddAsync(newChatRoom);

            await _db.SaveChangesAsync();

            return(_mapper.Map <ChatRoom, ChatRoomResponseDTO>(newChatRoom));
        }