Skip to content

Commit c9e499c

Browse files
committed
check OrderBy values
1 parent f209485 commit c9e499c

2 files changed

Lines changed: 23 additions & 6 deletions

File tree

Source/Applications/SystemCenter/Controllers/OpenXDA/AssetGroups/OpenXDAAssetGroupsController.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ public IHttpActionResult GetAssetsPaged([FromBody] PostData postData, [FromUri]
116116
PagedResults results = new PagedResults();
117117
results.RecordsPerPage = recordsPerPage;
118118

119+
string orderBy = "ID";
120+
121+
if (typeof(Asset).GetProperties().Select(prop => prop.Name).Contains(postData.OrderBy) || postData.OrderBy == "AssetType")
122+
{
123+
orderBy = postData.OrderBy;
124+
}
125+
119126
using (AdoDataConnection connection = new AdoDataConnection(Connection))
120127
{
121128
string sql = $@"SELECT
@@ -144,7 +151,7 @@ GROUP BY
144151
AssetType.Name,
145152
AssetAssetGroup.AssetGroupID
146153
HAVING AssetAssetGroup.AssetGroupID = {{0}}
147-
ORDER BY {postData.OrderBy} {(postData.Ascending ? "ASC" : "DESC")}
154+
ORDER BY {orderBy} {(postData.Ascending ? "ASC" : "DESC")}
148155
";
149156

150157
DataTable records = connection.RetrieveData(sql, assetGroupID);
@@ -280,6 +287,11 @@ public IHttpActionResult GetMetersPaged([FromBody] PostData postData, [FromUri]
280287
PagedResults results = new PagedResults();
281288
results.RecordsPerPage = recordsPerPage;
282289

290+
string orderBy = "ID";
291+
292+
if (typeof(Meter).GetProperties().Select(prop => prop.Name).Contains(postData.OrderBy))
293+
orderBy = postData.OrderBy;
294+
283295
using (AdoDataConnection connection = new AdoDataConnection(Connection))
284296
{
285297
string sql = $@"SELECT DISTINCT
@@ -306,7 +318,7 @@ GROUP BY
306318
Location.Name,
307319
MeterAssetGroup.AssetGroupID
308320
HAVING MeterAssetGroup.AssetGroupID = {{0}}
309-
ORDER BY {postData.OrderBy} {(postData.Ascending ? "ASC" : "DESC")}
321+
ORDER BY {orderBy} {(postData.Ascending ? "ASC" : "DESC")}
310322
";
311323

312324
DataTable records = connection.RetrieveData(sql, assetGroupID);
@@ -446,9 +458,9 @@ public IHttpActionResult GetSubGroupsPaged([FromBody] PostData postData, [FromUr
446458
{
447459
IEnumerable<AssetGroupView> records = new TableOperations<AssetGroupView>(connection).QueryRecordsWhere("ID in (SELECT ChildAssetGroupID FROM AssetGroupAssetGroupView WHERE ParentAssetGroupID = {0})", assetGroupID);
448460
if (postData.Ascending)
449-
records = records.OrderBy(record => record.GetType().GetProperty(postData.OrderBy).GetValue(record));
461+
records = records.OrderBy(record => record.GetType().GetProperty(postData.OrderBy).GetValue(record) ?? record.ID);
450462
else
451-
records = records.OrderByDescending(record => record.GetType().GetProperty(postData.OrderBy).GetValue(record));
463+
records = records.OrderByDescending(record => record.GetType().GetProperty(postData.OrderBy).GetValue(record) ?? record.ID);
452464

453465
results.TotalRecords = records.Count();
454466
results.NumberOfPages = (records.Count() + recordsPerPage - 1) / recordsPerPage;

Source/Applications/SystemCenter/Controllers/OpenXDA/Assets/OpenXDAAssetController.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,11 @@ public IHttpActionResult GetAssetAssetConnectionsPaged([FromBody] PostData postD
230230

231231
PagedResults results = new PagedResults();
232232

233+
string orderBy = "ID";
234+
235+
if (typeof(Asset).GetProperties().Select(prop => prop.Name).Contains(postData.OrderBy))
236+
orderBy = postData.OrderBy;
237+
233238
using (AdoDataConnection connection = new AdoDataConnection(Connection))
234239
{
235240
DataTable records = connection.RetrieveData(@$"
@@ -242,15 +247,15 @@ public IHttpActionResult GetAssetAssetConnectionsPaged([FromBody] PostData postD
242247
FROM
243248
AssetRelationship JOIN
244249
AssetRelationshipType ON AssetRelationship.AssetRelationshipTypeID = AssetRelationshipType.ID JOIN
245-
ASset ON Asset.ID = (
250+
Asset ON Asset.ID = (
246251
CASE
247252
WHEN ParentID = {{0}} THEN AssetRelationship.ChildID
248253
ELSE AssetRelationship.ParentID
249254
END
250255
)
251256
WHERE
252257
ParentID = {{0}} OR ChildID = {{0}}
253-
ORDER BY {postData.OrderBy} {(postData.Ascending ? "ASC" : "DESC")}
258+
ORDER BY {orderBy} {(postData.Ascending ? "ASC" : "DESC")}
254259
", assetID);
255260

256261
int totalRecords = records.Rows.Count;

0 commit comments

Comments
 (0)