Skip to content

Instantly share code, notes, and snippets.

@MarshalOfficial
Created May 12, 2024 06:13
Show Gist options
  • Save MarshalOfficial/9003fbd0ff6611b31b2cb10da25cb04a to your computer and use it in GitHub Desktop.
Save MarshalOfficial/9003fbd0ff6611b31b2cb10da25cb04a to your computer and use it in GitHub Desktop.
a generic implementation of C# paged list to be used by linq iqueriable
[Authorize(Roles = "Mayor,BudgetSecretary,CenterBudgetAdmin")]
[HttpGet("budget_process_definition")]
public async Task<PagedList<BudgetProcessDefinitionReportModel>> BudgetProcessDefinitionGetAll([FromQuery] PageParameters page_parameters, [FromQuery] BudgetProcessDefinitionSearchModel search_model)
{
var result = await budgetService.BudgetProcessDefinitionGetAll(page_parameters, search_model, HttpContext.User.Identity.Name);
var metadata = new
{
result.total_count,
result.page_size,
result.current_page,
result.total_pages,
result.has_next,
result.has_previous
};
Response.Headers.Add("X-Pagination", JsonSerializer.Serialize(metadata));
return result;
}
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAll",
builder => { builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().WithExposedHeaders("*"); });
});
IQueryable<BudgetProcessDefinitionReportModel> query = from proc in
coreContext.budget_process_definition.AsNoTracking()
.Include(a => a.create_user)
.Include(a => a.update_user)
.Include(a => a.chart)
where
(model.accounting_year_id == null || proc.accounting_year_id == model.accounting_year_id)
&& (model.chart_id == null || proc.chart_id == model.chart_id)
&& (hard_chart_ids == null || hard_chart_ids.Contains(proc.chart_id))
select new BudgetProcessDefinitionReportModel
{
id = proc.id,
chart_name = proc.chart.title,
create_date = proc.create_date.ConvertToIranTimeZone().ToPersianDigitalDateTimeString(),
update_date = proc.update_date == null
? null
: proc.update_date.Value.ConvertToIranTimeZone().ToPersianDigitalDateTimeString(),
accounting_year_id = proc.accounting_year_id,
create_user = proc.create_user.full_name,
update_user = proc.update_user == null
? string.Empty
: proc.update_user.full_name,
check_budget_balance = proc.data.check_budget_balance,
check_construction_limit = proc.data.check_construction_limit,
construction_projects_limit1 = proc.data.construction_projects_limit1,
construction_projects_limit2 = proc.data.construction_projects_limit2,
construction_projects_limit3 = proc.data.construction_projects_limit3,
deadline_from_date_fa = proc.data.deadline_from_date.ConvertToIranTimeZone()
.ToPersianDigitalDateTimeString(),
deadline_to_date_fa = proc.data.deadline_to_date.ConvertToIranTimeZone()
.ToPersianDigitalDateTimeString(),
income_coding_ids_count = proc.data.income_coding_ids.Count,
consumption_coding_ids_count = proc.data.consumption_coding_ids.Count,
construction_consumption_coding_ids_count = proc.data.construction_consumption_coding_ids.Count,
};
return await PagedList<BudgetProcessDefinitionReportModel>.ToPagedListAsync(
query
.OrderByDescending(on => on.id),
page_parameters.page_number,
page_parameters.page_size);
public class PagedList<T> : List<T>
{
public int current_page { get; set; }
public int total_pages { get; set; }
public int page_size { get; set; }
public int total_count { get; set; }
public bool has_previous => current_page > 1;
public bool has_next => current_page < total_pages;
public PagedList()
{
}
public PagedList(List<T> items, int count, int page_number, int page_size)
{
total_count = count;
this.page_size = page_size;
current_page = page_number;
total_pages = (int)Math.Ceiling(count / (double)page_size);
AddRange(items);
}
public static async Task<PagedList<T>> ToPagedListAsync(IQueryable<T> source, int page_number, int page_size)
{
var count = source.Count();
var items = await source.Skip((page_number - 1) * page_size).Take(page_size).ToListAsync();
return new PagedList<T>(items, count, page_number, page_size);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment