Exemplo n.º 1
0
        public async Task GetMessagesAsync_ChannelId3_ReturnsEmptyList(MessagesResourceParameters messagesResourceParameters)
        {
            // Arrange
            var options = DatabaseUtilities.GetTestDbConextOptions <CompanyNameProjectNameContext>();

            var user = new User()
            {
                Email     = "*****@*****.**",
                FirstName = "Test",
                LastName  = "User"
            };

            var messageOne = new Message()
            {
                Text      = "Test Message One",
                ChannelId = 1,
            };
            var messageTwo = new Message()
            {
                Text      = "Test Message Two",
                ChannelId = 2,
            };

            using (var context = new CompanyNameProjectNameContext(options))
            {
                context.Database.OpenConnection();
                context.Database.EnsureCreated();

                // Add a user because we need a UserId foreign key for the messages
                var usersRepository = new UsersRepository(context, MapperUtilities.GetTestMapper());
                await usersRepository.CreateAsync(user);
            }

            using (var context = new CompanyNameProjectNameContext(options))
            {
                var messagesRepository = new MessagesRepository(context, MapperUtilities.GetTestMapper());

                messageOne.UserId = user.Id;
                messageTwo.UserId = user.Id;

                // Add the messages
                await messagesRepository.CreateAsync(messageOne);

                await messagesRepository.CreateAsync(messageTwo);
            }

            using (var context = new CompanyNameProjectNameContext(options))
            {
                var messagesRepository = new MessagesRepository(context, MapperUtilities.GetTestMapper());

                // Act
                var results = await messagesRepository.GetMessagesAsync(messagesResourceParameters);

                // Assert
                Assert.AreEqual(results.Count(), 0);
            }
        }
        public async Task <IEnumerable <Message> > GetMessagesAsync(MessagesResourceParameters parameters)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(MessagesResourceParameters));
            }

            var messageEntities = await Search()
                                  .Apply(parameters, Context.Messages as IQueryable <MessageEntity>)
                                  .ToListAsync();

            return(Mapper.Map <IEnumerable <Message> >(messageEntities));
        }
Exemplo n.º 3
0
        public async Task GetMessagesAsync_AllPropertiesNull_ReturnsAllMessages()
        {
            // Arrange
            var options = DatabaseUtilities.GetTestDbConextOptions <CompanyNameProjectNameContext>();

            var user = new User()
            {
                Email     = "*****@*****.**",
                FirstName = "Test",
                LastName  = "User"
            };

            var messageOne = new Message()
            {
                Text      = "Test Message One",
                ChannelId = 1,
            };
            var messageTwo = new Message()
            {
                Text      = "Test Message Two",
                ChannelId = 1,
            };

            using (var context = new CompanyNameProjectNameContext(options))
            {
                context.Database.OpenConnection();
                context.Database.EnsureCreated();

                // Add a user because we need a UserId foreign key for the messages
                var usersRepository = new UsersRepository(context, MapperUtilities.GetTestMapper());
                await usersRepository.CreateAsync(user);
            }

            using (var context = new CompanyNameProjectNameContext(options))
            {
                var messagesRepository = new MessagesRepository(context, MapperUtilities.GetTestMapper());

                messageOne.UserId = user.Id;
                messageTwo.UserId = user.Id;

                // Add the messages
                await messagesRepository.CreateAsync(messageOne);

                await messagesRepository.CreateAsync(messageTwo);
            }

            using (var context = new CompanyNameProjectNameContext(options))
            {
                var messagesRepository = new MessagesRepository(context, MapperUtilities.GetTestMapper());

                // Act
                var parameters = new MessagesResourceParameters {
                    ChannelId = 0, SearchQuery = null
                };

                // Get messages with null properties for parameters
                var results = await messagesRepository.GetMessagesAsync(parameters);

                // Assert
                Assert.AreEqual(results.Count(), 2);
                Assert.IsTrue(results.FirstOrDefault(x => x.Text == messageOne.Text) != null);
                Assert.IsTrue(results.FirstOrDefault(x => x.Text == messageTwo.Text) != null);
            }
        }
        public async Task <ActionResult <IEnumerable <ReadMessage> > > GetMessages([FromQuery] MessagesResourceParameters parameters)
        {
            var messages = await messagesService.MessagesRepository.GetMessagesAsync(parameters);

            return(messages == null?NotFound() : (ActionResult)Ok(mapper.Map <IEnumerable <ReadMessage> >(messages)));
        }