From 7d764f966d52d73e158963069d7d378b1182b856 Mon Sep 17 00:00:00 2001 From: Adam Moussa <166072409+amoussa1229@users.noreply.github.com> Date: Fri, 17 Apr 2026 12:50:23 -0400 Subject: [PATCH] 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 --- .../Controllers/WorkOrderController.cs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/Api.SeaHavenIndustries/Controllers/WorkOrderController.cs b/Api.SeaHavenIndustries/Controllers/WorkOrderController.cs index 929d29a..0bb4a43 100644 --- a/Api.SeaHavenIndustries/Controllers/WorkOrderController.cs +++ b/Api.SeaHavenIndustries/Controllers/WorkOrderController.cs @@ -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 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