public async Task <PagedResult <TodoDto> > SearchAsync(SearchTodo query) { var result = _todoRepository.GetAllAsync(); if (!(query is null)) { if (!string.IsNullOrWhiteSpace(query.Title)) { result = result.Where(x => x.Title.Value.Contains(query.Title)); } if (!string.IsNullOrWhiteSpace(query.Description)) { result = result.Where(x => x.Description.Value.Contains(query.Description)); } if (!string.IsNullOrWhiteSpace(query.Priority)) { if (Enum.TryParse <Priority>(query.Priority, true, out var priority)) { result = result.Where(x => x.Priority == priority); } } if (!string.IsNullOrWhiteSpace(query.State)) { if (Enum.TryParse <State>(query.State, true, out var state)) { result = result.Where(x => x.State == state); } } if (!(query.CreatedAt is null)) { result = result.Where(x => x.CreatedAt.Date == query.CreatedAt.Value.Date); } }
public async Task search_todo_should_given_no_filter_should_return_default_paged_result() { SearchTodo query = null; _todoRepository.GetAllAsync().Returns(todoList.AsQueryable()); var expected = PagedResult <Todo> .Create(todoList, 1, 10, 1, 4).Map(x => x.AsDto()); var actuals = await _todosService.SearchAsync(query); _todoRepository.Received().GetAllAsync(); expected.Items.Count().ShouldBe(actuals.Items.Count()); }
public async Task search_todo_should_given_search_filter_should_return_data_based_on_paging_option_requested() { SearchTodo query = new SearchTodo { Title = "Title #1" }; _todoRepository.GetAllAsync().Returns(todoList.AsQueryable()); var filtered = todoList.Where(x => x.Title.Value.Contains(query.Title)); var expected = PagedResult <Todo> .Create(filtered, 1, 10, 1, 4).Map(x => x.AsDto()); var actuals = await _todosService.SearchAsync(query); _todoRepository.Received().GetAllAsync(); expected.Items.Count().ShouldBe(actuals.Items.Count()); }
public async Task search_todo_should_given_filter_should_return_data_based_on_paging_option_requested() { SearchTodo query = new SearchTodo { Page = 1, PageSize = 2 }; _todoRepository.GetAllAsync().Returns(todoList.AsQueryable()); var filtered = todoList.Skip(0).Take(2); var expected = PagedResult <Todo> .Create(filtered, 1, 10, 2, 4).Map(x => x.AsDto()); var actuals = await _todosService.SearchAsync(query); _todoRepository.Received().GetAllAsync(); expected.Items.Count().ShouldBe(actuals.Items.Count()); }
public async Task<ActionResult<IEnumerable<TodoDto>>> GetAll([FromQuery] SearchTodo query) { return Ok(await _todoService.SearchAsync(query)); }