0% found this document useful (0 votes)
7 views2 pages

Paging Get

The document defines an API for getting orders from a database. It includes a controller method that calls an order service, which queries the database using a repository layer. It applies filtering, sorting, pagination and includes related data in the response.

Uploaded by

Lam Tra Nguyen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Paging Get

The document defines an API for getting orders from a database. It includes a controller method that calls an order service, which queries the database using a repository layer. It applies filtering, sorting, pagination and includes related data in the response.

Uploaded by

Lam Tra Nguyen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

//Controller

[HttpGet]
public async Task<IActionResult> GetList([FromQuery]OrderQueryModel model)
{
return ResponseHelper.Success(await orderService.GetOrders(model));
}

//helper
public static class ResponseHelper
{
public static ObjectResult Success(Object data = null)
{
return ToObjectResult(data, HttpStatusCode.OK);
}
}

//service
Task<object> GetOrders (OrderQueryModel query);
public async Task<object> GetOrders (OrderQueryModel query) {
var predicate = new List<Expression<Func<Order, bool>>> { x => true };

if (!string.IsNullOrEmpty (query.TextSearch))
predicate.Add (x => x.ShippingAddress.Address.Contains (query.TextSearch)
||
x.ShippingAddress.FullName.Contains (query.TextSearch) ||
x.ShippingAddress.FullName.Contains (query.TextSearch));

if (query.Status.HasValue)
predicate.Add (x => x.Status == query.Status);

var (Total, Entities) = await GetPaging (predicate.Aggregate ((a, b) => a.And


(b)),
new Dictionary<Expression<Func<Order, object>>, bool > { { x => x.Status,
true } },
query.Page,
query.Size,
includes : new List<Expression<Func<Order, object>>> { x =>
x.ShippingAddress });

var data = new {


total = Total,
orders = Entities.Select (x => new {
id = x.Id,
receiveName = x.ShippingAddress.FullName,
receivePhone = x.ShippingAddress.PhoneNumber,
address = x.ShippingAddress.Address,
createdAt = x.CreatedAt,
totalPrice = x.Total,
transactStatus = new {
id = x.Status,
name = EnumHelper.GetDescription (x.Status)
}
})
};

return data;
}

//interface
Task<(int Total, List<T> Entities)> GetPaging(Expression<Func<T, bool>> predicate =
null, Dictionary<Expression<Func<T, object>>, bool> orderBy = null, int? skip =
null, int? take = null, List<Expression<Func<T, object>>> includes = null);
public virtual async Task<(int Total, List<T> Entities)>
GetPaging(Expression<Func<T, bool>> predicate = null,
Dictionary<Expression<Func<T, object>>, bool> orderBy = null,
int? skip = null, int? take = null,
List<Expression<Func<T, object>>> includes = null)
{
return await repository.GetPaging(predicate, orderBy, skip, take, includes);
}

responsitory
public async Task<(int Total, List<T> Entities)> GetPaging(Expression<Func<T,
bool>> predicate = null,
Dictionary<Expression<Func<T, object>>, bool> orderBy = null,
int? skip = null, int? take = null,
List<Expression<Func<T, object>>> includes = null)
{
var query = entities.AsQueryable();
if (predicate != null)
query = query.Where(predicate);

if (includes != null && includes.Any())


includes.ForEach(x => query = query.Include(x));

if (orderBy != null && orderBy.Any())


{
query = orderBy.First().Value ? query.OrderBy(orderBy.First().Key) :
query.OrderByDescending(orderBy.First().Key);
orderBy.Remove(orderBy.First().Key);
query = orderBy.Aggregate(query, (current, o) => o.Value
? (current as IOrderedQueryable<T>).ThenBy(o.Key)
: (current as IOrderedQueryable<T>).ThenByDescending(o.Key));
}

int total = await query.CountAsync();

if (skip.HasValue && take.HasValue)


query = query.Skip((skip.Value - 1) * take.Value).Take(take.Value);

return (total, await query.ToListAsync());


}

You might also like