-
Notifications
You must be signed in to change notification settings - Fork 14
2.1. Fop usage in Api
Aybars edited this page Sep 2, 2019
·
2 revisions
Basically this wiki helps you to integrate Fop your API easily. 💡
- Install
Fop
NuGet package from here.
PM> Install-Package Fop
-
Add FopQuery parameter to your HttpGet Method. **FopQuery **already has needed properties to build an IFopRequest
-
Then generate the great **IFopRequest **with using FopExpressionBuilder
[HttpGet]
public async Task<IActionResult> Index([FromQuery] FopQuery request)
{
// Build IFopRequest from query
var fopRequest = FopExpressionBuilder<Student>.Build(request.Filter, request.Order, request.PageNumber, request.PageSize);
// Apply Fop (filter, order, paging)
var (filteredStudents, totalCount) = _context.Students.ApplyFop(fopRequest);
var response = await filteredStudents.ToListAsync();
return Ok(filteredStudents);
}
It's my simple solution. Normally Fop is not focused to solve your paged response. Please contribute to sample API if you have better solutions.
-
Don't forget in the Fun Begin part
-
Just I added my paged result solution strategy to my api infrastructure. here is mine.
public class PagedResult<T>
{
public PagedResult(T data, int totalCount, int pageNumber, int pageSize)
{
Data = data;
TotalCount = totalCount;
PageNumber = pageNumber;
PageSize = pageSize;
TotalPages = ((totalCount - 1) / pageSize) + 1;
}
public T Data { get; set; }
public int TotalCount { get; set; }
public int PageNumber { get; set; }
public int PageSize { get; set; }
public int TotalPages { get; set; }
}
3.Then just create paged result like below
new PagedResult<List<Student>>(filteredStudents, totalCount, request.PageNumber, request.PageSize);
Full Action
// You can implement paged result strategy depends on your strategy
// It's simple
// please contribute to improve it
[HttpGet("PagedResult")]
public async Task<IActionResult> PagedResult([FromQuery] FopQuery request)
{
// Build IFopRequest from query
var fopRequest = FopExpressionBuilder<Student>.Build(request.Filter, request.Order, request.PageNumber, request.PageSize);
// Apply Fop (filter, order, paging)
var (filteredStudents, totalCount) = _context.Students.ApplyFop(fopRequest);
// Create my paged response
var response = new PagedResult<List<Student>>((await filteredStudents,ToListAsync()), totalCount, request.PageNumber, request.PageSize);
return Ok(response);
}
That's all