Beispiel #1
0
        public void GetByQueryAsync_UsingNullAsQuery_MustThrowException()
        {
            var           mockTodoDbContext = new DbContextMock <TodoDbContext>(DummyOptions);
            var           mockLogger        = new Mock <ILogger <TodoService> >();
            var           todoService       = new TodoService(mockTodoDbContext.Object, mockLogger.Object);
            TodoItemQuery todoItemQuery     = null;

            // ReSharper disable once ExpressionIsAlwaysNull
            Func <Task <IList <TodoItemInfo> > > getByQueryAsync = async() =>
                                                                   await todoService.GetByQueryAsync(todoItemQuery).ConfigureAwait(false);

            getByQueryAsync.Should().Throw <ArgumentNullException>("service cannot fetch data using a null query")
            .And.ParamName.Should().Be("instance", "the query is null");
        }
        public async Task <IList <TodoItemInfo> > GetByQueryAsync(TodoItemQuery todoItemQuery)
        {
            Validator.ValidateObject(todoItemQuery, new ValidationContext(todoItemQuery), true);

            IQueryable <TodoItem> todoItems = FilterItems(todoItemQuery);

            todoItems = SortItems(todoItems, todoItemQuery);
            todoItems = PaginateItems(todoItems, todoItemQuery);
            IQueryable <TodoItemInfo> todoItemInfos = ProjectItems(todoItems);
            IList <TodoItemInfo>      result        = await todoItemInfos.ToListAsync().ConfigureAwait(false);

            logger.LogInformation("Fetched {TodoItemsCount} todo item(s) for user {UserId} using query {TodoItemQuery}",
                                  result.Count, todoItemQuery.Owner.GetUserId(), todoItemQuery);

            return(result);
        }
        private IQueryable <TodoItem> FilterItems(TodoItemQuery todoItemQuery)
        {
            IQueryable <TodoItem> todoItems =
                todoDbContext.TodoItems.Where(todoItem => todoItem.CreatedBy == todoItemQuery.Owner.GetUserId());

            if (todoItemQuery.Id.HasValue)
            {
                todoItems = todoItems.Where(todoItem => todoItem.Id == todoItemQuery.Id.Value);
            }

            if (!string.IsNullOrWhiteSpace(todoItemQuery.NamePattern))
            {
                todoItems = todoItems.Where(todoItem => EF.Functions.Like(todoItem.Name, todoItemQuery.NamePattern));
            }

            if (todoItemQuery.IsComplete.HasValue)
            {
                todoItems = todoItems.Where(todoItem => todoItem.IsComplete == todoItemQuery.IsComplete.Value);
            }

            return(todoItems);
        }
Beispiel #4
0
        public async Task <IList <TodoItemInfo> > GetByQueryAsync(TodoItemQuery todoItemQuery)
        {
            Validator.ValidateObject(todoItemQuery, new ValidationContext(todoItemQuery), true);

            IQueryable <TodoItem> todoItems = FilterItems(todoItemQuery)
                                              // Read more about query tags here:
                                              // https://docs.microsoft.com/en-us/ef/core/querying/tags
                                              .TagWith($"{nameof(TodoService)}#{nameof(GetByQueryAsync)}")
                                              // Read more about no tracking queries here:
                                              // https://docs.microsoft.com/en-us/ef/core/querying/tracking#no-tracking-queries
                                              .AsNoTracking();

            todoItems = SortItems(todoItems, todoItemQuery);
            todoItems = PaginateItems(todoItems, todoItemQuery);
            IQueryable <TodoItemInfo> todoItemInfos = ProjectItems(todoItems);
            IList <TodoItemInfo>      result        = await todoItemInfos.ToListAsync().ConfigureAwait(false);

            logger.LogInformation("Fetched {TodoItemsCount} todo item(s) for user {UserId} using query {TodoItemQuery}",
                                  result.Count, todoItemQuery.Owner.GetUserId(), todoItemQuery);

            return(result);
        }
        public async Task GetByQueryAsync_UsingNullAsQuery_MustThrowException()
        {
            var           mockTodoDbContext = new DbContextMock <TodoDbContext>(DummyOptions);
            var           mockLogger        = new Mock <ILogger <TodoService> >();
            var           todoService       = new TodoService(mockTodoDbContext.Object, mockLogger.Object);
            TodoItemQuery todoItemQuery     = null;

            try
            {
                // ReSharper disable once ExpressionIsAlwaysNull
                await todoService.GetByQueryAsync(todoItemQuery).ConfigureAwait(false);

                Assert.Fail($"Expected to not be able to call method {nameof(todoService.GetByQueryAsync)} using null as argument");
            }
            catch (Exception expectedException)
            {
                expectedException.Should()
                .NotBeNull()
                .And.BeAssignableTo <ArgumentNullException>()
                .Subject.As <ArgumentNullException>()
                .ParamName.Should().Be("instance");
            }
        }
        private static IQueryable <TodoItem> PaginateItems(IQueryable <TodoItem> todoItems, TodoItemQuery todoItemQuery)
        {
            IQueryable <TodoItem> result = todoItems.Skip(todoItemQuery.PageIndex).Take(todoItemQuery.PageSize);

            return(result);
        }
        private static IQueryable <TodoItem> SortItems(IQueryable <TodoItem> todoItems, TodoItemQuery todoItemQuery)
        {
            Expression <Func <TodoItem, object> > keySelector = GetKeySelectorBy(todoItemQuery.SortBy);

            if (todoItemQuery.IsSortAscending.HasValue && !todoItemQuery.IsSortAscending.Value)
            {
                todoItems = todoItems.OrderByDescending(keySelector);
            }
            else
            {
                todoItems = todoItems.OrderBy(keySelector);
            }

            return(todoItems);
        }
Beispiel #8
0
        private static IQueryable <TodoItem> PaginateItems(IQueryable <TodoItem> todoItems, TodoItemQuery todoItemQuery)
        {
            IQueryable <TodoItem> result = todoItems;
            int pageIndex = TodoItemQuery.DefaultPageIndex;
            int pageSize  = TodoItemQuery.DefaultPageSize;

            if (todoItemQuery.PageIndex.HasValue)
            {
                pageIndex = todoItemQuery.PageIndex.Value;
            }

            if (todoItemQuery.PageSize.HasValue)
            {
                pageSize = todoItemQuery.PageSize.Value;
            }

            result = result.Skip(pageIndex * pageSize).Take(pageSize);
            return(result);
        }