public async Task <IActionResult> Index([FromQuery] FopQuery request) { var fopRequest = FopExpressionBuilder <Student> .Build(request.Filter, request.Order, request.PageNumber, request.PageSize); var(filteredStudents, totalCount) = await _studentRepository.RetrieveStudents(fopRequest); return(Ok(filteredStudents)); }
public void FopExpressionBuilder_Should_Failed_Returns_FilterOperatorNotFoundException() { // Arrange var filterQueryString = "Midterm>10;Name__A;and$IdentityNumber==100101;IdentityNumber==100001;or"; var orderQueryString = "Midterm;desc"; var pageNumber = 1; var pageSize = 50; // Act var ex = Record.Exception(() => FopExpressionBuilder <Student> .Build(filterQueryString, orderQueryString, pageNumber, pageSize)); // Assert Assert.True(ex is FilterOperatorNotFoundException); }
public async Task <IActionResult> GetListProducts([FromQuery] FopQuery request) { try { var fopRequest = FopExpressionBuilder <ProductForList> .Build(request.Filter, request.Order, request.PageNumber, request.PageSize); var(productToReturn, totalCount) = await _productServices.GetListProduct(fopRequest); var response = new PagedResult <IEnumerable <ProductForList> >((productToReturn), totalCount, request.PageNumber, request.PageSize);; return(Ok(response)); } catch (Exception ex) { return(new BadRequestObjectResult(new { Message = ex.Message.ToString() })); } }
public void FopExpressionBuilder_Should_Build_FopRequest_With_Single_FilterList() { // Arrange var filterQueryString = "Midterm>10;Name_=A;and"; var orderQueryString = "Midterm;desc"; var pageNumber = 1; var pageSize = 50; // Act var result = FopExpressionBuilder <Student> .Build(filterQueryString, orderQueryString, pageNumber, pageSize); // Assert Assert.True(result.PageNumber == 1); Assert.True(result.PageSize == 50); Assert.True(result.OrderBy == "midterm"); Assert.True(result.Direction == OrderDirection.Desc); Assert.True(result.FilterList.FirstOrDefault()?.Logic == FilterLogic.And); Assert.True(result.FilterList.FirstOrDefault()?.Filters.Any(x => x.Key == "Student.Name")); }
public async Task <IActionResult> GetBlogsByCateId(int blogCategoryId, [FromQuery] FopQuery request) { try { var fopRequest = FopExpressionBuilder <Cl_BlogForList> .Build(request.Filter, request.Order, request.PageNumber, request.PageSize); var(blogsReturn, totalCount) = await _blogServices.GetBlogsByCateId(blogCategoryId, fopRequest); if (blogsReturn.Count() == 0) { return(new BadRequestObjectResult(new { Message = "Tin tức hiện đang trống. Chúng tôi sẽ cập nhật trong thời gian sớm nhất" })); } var response = new PagedResult <IEnumerable <Cl_BlogForList> >((blogsReturn), totalCount, request.PageNumber, request.PageSize);; return(Ok(response)); } catch (Exception ex) { return(new BadRequestObjectResult(new { Message = ex.Message.ToString() })); } }
public async Task <IActionResult> GetOrdersByUserId(string userId, [FromQuery] FopQuery request) { try { var fopRequest = FopExpressionBuilder <OrderForList> .Build(request.Filter, request.Order, request.PageNumber, request.PageSize); var(ordersToReturn, totalCount) = await _orderServices.GetOrdersByUserId(userId, fopRequest); if (ordersToReturn.Count() == 0) { return(new BadRequestObjectResult(new { Message = "Không tìm thấy đơn hàng nào" })); } var response = new PagedResult <IEnumerable <OrderForList> >((ordersToReturn), totalCount, request.PageNumber, request.PageSize);; return(Ok(response)); } catch (Exception ex) { return(new BadRequestObjectResult(new { Message = ex.Message.ToString() })); } }
public async Task <IActionResult> GetBlogs(int blogCategoryId, [FromQuery] FopQuery request) { try { var fopRequest = FopExpressionBuilder <BlogForList> .Build(request.Filter, request.Order, request.PageNumber, request.PageSize); var(blogsByCateId, totalCount) = await _blogServices.GetBlogs(blogCategoryId, fopRequest); if (blogsByCateId == null) { return(new BadRequestObjectResult(new { Message = "Không tìm tháy tin tức nào!" })); } var response = new PagedResult <IEnumerable <BlogForList> >((blogsByCateId), totalCount, request.PageNumber, request.PageSize);; return(Ok(response)); } catch (Exception ex) { return(new BadRequestObjectResult(new { Message = ex.Message.ToString() })); } }
public void FopExpressionBuilder_Should_Build_FopRequest_With_Multiple_FilterList_With_DateTime() { // Arrange var filterQueryString = "Level==a;Birthday>1993-06-07 00:00:00;and"; var orderQueryString = "Midterm;desc"; var pageNumber = 1; var pageSize = 50; // Act var result = FopExpressionBuilder <Student> .Build(filterQueryString, orderQueryString, pageNumber, pageSize); // Assert Assert.True(result.PageNumber == 1); Assert.True(result.PageSize == 50); Assert.True(result.OrderBy == "midterm"); Assert.True(result.Direction == OrderDirection.Desc); Assert.True(result.FilterList.FirstOrDefault()?.Logic == FilterLogic.And); Assert.True(result.FilterList.FirstOrDefault()?.Filters.Any(x => x.DataType == FilterDataTypes.Char)); Assert.True(result.FilterList.FirstOrDefault()?.Filters.Any(x => x.Key == "Student.Birthday")); }
/// <summary> /// Apply pagination, sorting and filtering to the <see cref="IQueryable{T}"/>. /// </summary> /// <typeparam name="T">entity type</typeparam> /// <param name="source">list of entities</param> /// <param name="query">query options for pagination, filtering and sorting.</param> public static PaginationResult <T> ApplyQuery <T>( this IQueryable <T> source, IFopQuery query) where T : class { query.PageNumber = query.PageNumber <= 0 ? 1 : query.PageNumber; query.PageSize = GetPageSize(query.PageSize); var fopRequest = FopExpressionBuilder <Ticket> .Build( query.Filter, query.Order, query.PageNumber, query.PageSize); int totalCount; (source, totalCount) = source.ApplyFop(fopRequest); return(new PaginationResult <T> { PageNumber = query.PageNumber, PageSize = query.PageSize, TotalCount = totalCount, Data = source.AsEnumerable(), }); }