Add server-side sorting to GetWorkOrderList

- Accept sortBy and sortDir query parameters
- Supports sorting by: number, title, location, priority, status,
  createdDate, assignedTo, lastUpdated
- Default sort: lastUpdated desc
- Sort applied in SQL before pagination
This commit is contained in:
Adam Moussa 2026-04-17 12:50:23 -04:00
parent 091390fcad
commit 7d764f966d

View file

@ -369,7 +369,7 @@ namespace Api.SeaHavenIndustries.Controllers
// GET: api/WorkOrder/GetWorkOrderList
// -----------------------------------
[HttpGet("GetWorkOrderList")]
public IActionResult GetWorkOrderList(string? search = "", int page = 1, int pageSize = 12)
public IActionResult GetWorkOrderList(string? search = "", int page = 1, int pageSize = 12, string? sortBy = "lastUpdated", string? sortDir = "desc")
{
search ??= "";
@ -392,8 +392,21 @@ namespace Api.SeaHavenIndustries.Controllers
var totalCount = query.Count();
var data = query
.OrderByDescending(w => w.Id)
var isAsc = sortDir?.ToLower() == "asc";
IOrderedQueryable<WorkOrder> ordered = (sortBy?.ToLower()) switch
{
"number" => isAsc ? query.OrderBy(w => w.InternalWONumber) : query.OrderByDescending(w => w.InternalWONumber),
"title" => isAsc ? query.OrderBy(w => w.WorkerOrderTitle) : query.OrderByDescending(w => w.WorkerOrderTitle),
"location" => isAsc ? query.OrderBy(w => w.Locations != null ? w.Locations.Name : "") : query.OrderByDescending(w => w.Locations != null ? w.Locations.Name : ""),
"priority" => isAsc ? query.OrderBy(w => w.Priority) : query.OrderByDescending(w => w.Priority),
"status" => isAsc ? query.OrderBy(w => w.Status) : query.OrderByDescending(w => w.Status),
"createddate" => isAsc ? query.OrderBy(w => w.CreatedDate) : query.OrderByDescending(w => w.CreatedDate),
"assignedto" => isAsc ? query.OrderBy(w => w.AssignToUser != null ? w.AssignToUser.FirstName : "") : query.OrderByDescending(w => w.AssignToUser != null ? w.AssignToUser.FirstName : ""),
"lastupdated" => isAsc ? query.OrderBy(w => w.LastModificationTime) : query.OrderByDescending(w => w.LastModificationTime),
_ => query.OrderByDescending(w => w.Id)
};
var data = ordered
.Skip((page - 1) * pageSize)
.Take(pageSize)
.Select(w => new