-
Notifications
You must be signed in to change notification settings - Fork 3
System Center Pagination #919
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nbeatty-gpa
wants to merge
11
commits into
master
Choose a base branch
from
newPagination2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,375
−895
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7aab3db
use PageSize to determine RecordsPerPage in paged endpoints and overr…
nbeatty-gpa 4c90a33
paginate AssetGroup Asset tab
nbeatty-gpa 526c718
paginate AssetGroup Meter tab
nbeatty-gpa cd11c10
paginate AssetGroup Subgroup tab
nbeatty-gpa 76a6bf4
paginate Asset Channel tab
nbeatty-gpa a2c9522
paginate Asset LineSegment tab
nbeatty-gpa 864e188
paginate Meter Channel tabs
nbeatty-gpa 3f98514
paginate UserGroup Users tab
nbeatty-gpa e11918f
paginate SourceImpedance tab
nbeatty-gpa 1191980
paginate ValueListGroup Items tab
nbeatty-gpa fa11956
add whitelisting to postData OrderBy fields
nbeatty-gpa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,6 +39,7 @@ | |
| using GSF.Data; | ||
| using GSF.Data.Model; | ||
| using GSF.Web.Model; | ||
| using Newtonsoft.Json; | ||
| using Newtonsoft.Json.Linq; | ||
| using openXDA.Model; | ||
| using SystemCenter.Model; | ||
|
|
@@ -57,16 +58,24 @@ private class extendedAssetGroupView: AssetGroupView | |
|
|
||
| } | ||
|
|
||
| [HttpGet, Route("{assetGroupID:int}/Assets")] | ||
| public IHttpActionResult GetAssets(int assetGroupID) | ||
| [HttpPost, Route("{assetGroupID:int}/Assets/{page:int}")] | ||
| public IHttpActionResult GetAssets([FromBody] PostData postData, [FromUri] int assetGroupID, [FromUri] int page) | ||
| { | ||
| if (GetRoles == string.Empty || User.IsInRole(GetRoles)) | ||
| { | ||
|
|
||
| HashSet<string> validSortFields = new HashSet<string> { "assetname", "assetkey", "assettype" }; | ||
|
|
||
| if (!validSortFields.Contains(postData.OrderBy.ToLower())) | ||
| return BadRequest($"{postData.OrderBy} is not a valid sort field."); | ||
|
|
||
| using (AdoDataConnection connection = new AdoDataConnection(Connection)) | ||
| { | ||
| try | ||
| { | ||
| string sql = @"SELECT | ||
| int recordsPerPage = PageSize ?? 50; | ||
|
|
||
| string sql = @$"SELECT | ||
| DISTINCT | ||
| Asset.ID, | ||
| AssetAssetGroup.AssetGroupID, | ||
|
|
@@ -91,9 +100,30 @@ GROUP BY | |
| Asset.VoltageKV, | ||
| AssetType.Name, | ||
| AssetAssetGroup.AssetGroupID | ||
| HAVING AssetAssetGroup.AssetGroupID = {0}"; | ||
| HAVING AssetAssetGroup.AssetGroupID = {{0}} | ||
| ORDER BY {postData.OrderBy} {(postData.Ascending ? "ASC" : "DESC")} | ||
| OFFSET {recordsPerPage * page} ROWS | ||
| FETCH NEXT {recordsPerPage} ROWS ONLY | ||
| "; | ||
|
|
||
| return Ok(connection.RetrieveData(sql,assetGroupID)); | ||
| string countSql = @"SELECT | ||
| COUNT(DISTINCT AssetID) | ||
| FROM | ||
| AssetAssetGroup | ||
| WHERE | ||
| AssetGroupID = {0}"; | ||
|
|
||
| int count = connection.ExecuteScalar<int>(countSql, assetGroupID); | ||
|
|
||
| DataTable results = connection.RetrieveData(sql, assetGroupID); | ||
|
|
||
| return Ok(new PagedResults() | ||
| { | ||
| Data = JsonConvert.SerializeObject(results), | ||
| NumberOfPages = (count + recordsPerPage - 1) / recordsPerPage, | ||
| TotalRecords = count, | ||
| RecordsPerPage = recordsPerPage | ||
| }); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
|
|
@@ -159,16 +189,23 @@ public IHttpActionResult RemoveAsset(int assetGroupID, int assetID) | |
| } | ||
| } | ||
|
|
||
| [HttpGet, Route("{assetGroupID:int}/Meters")] | ||
| public IHttpActionResult GetMeters(int assetGroupID) | ||
| [HttpPost, Route("{assetGroupID:int}/Meters/{page:int}")] | ||
| public IHttpActionResult GetMeters([FromBody] PostData postData, [FromUri] int assetGroupID, [FromUri] int page) | ||
| { | ||
| if (GetRoles == string.Empty || User.IsInRole(GetRoles)) | ||
| { | ||
| string[] validSortFields = { "name", "location" }; | ||
|
|
||
| if (!validSortFields.Any(field => String.Equals(field, postData.OrderBy.ToLower()))) | ||
| return BadRequest($"{postData.OrderBy} is not a valid sort field."); | ||
|
|
||
| using (AdoDataConnection connection = new AdoDataConnection(Connection)) | ||
| { | ||
| try | ||
| { | ||
| string sql = @"SELECT DISTINCT | ||
| int recordsPerPage = PageSize ?? 50; | ||
|
|
||
| string sql = @$"SELECT DISTINCT | ||
| Meter.ID, | ||
| MeterAssetGroup.AssetGroupID, | ||
| Meter.AssetKey, | ||
|
|
@@ -177,6 +214,28 @@ public IHttpActionResult GetMeters(int assetGroupID) | |
| Meter.Model, | ||
| Location.Name as Location, | ||
| COUNT(DISTINCT MeterAsset.AssetID) as MappedAssets | ||
| FROM | ||
| Meter LEFT JOIN | ||
| Location ON Meter.LocationID = Location.ID LEFT JOIN | ||
| MeterAsset ON Meter.ID = MeterAsset.MeterID LEFT JOIN | ||
| Asset ON MeterAsset.AssetID = Asset.ID LEFT JOIN | ||
| MeterAssetGroup ON Meter.ID = MeterAssetGroup.MeterID | ||
| GROUP BY | ||
| Meter.ID, | ||
| Meter.AssetKey, | ||
| Meter.Name, | ||
| Meter.Make, | ||
| Meter.Model, | ||
| Location.Name, | ||
| MeterAssetGroup.AssetGroupID | ||
| HAVING MeterAssetGroup.AssetGroupID = {{0}} | ||
| ORDER BY {postData.OrderBy} {(postData.Ascending ? "ASC" : "DESC")} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This introduces SQLInjection {postData.OrderBy} |
||
| OFFSET {recordsPerPage * page} ROWS | ||
| FETCH NEXT {recordsPerPage} ROWS ONLY" | ||
| ; | ||
|
|
||
| string countSql = @"SELECT | ||
| COUNT(DISTINCT Meter.ID) | ||
| FROM | ||
| Meter LEFT JOIN | ||
| Location ON Meter.LocationID = Location.ID LEFT JOIN | ||
|
|
@@ -193,7 +252,17 @@ GROUP BY | |
| MeterAssetGroup.AssetGroupID | ||
| HAVING MeterAssetGroup.AssetGroupID = {0}"; | ||
|
|
||
| return Ok(connection.RetrieveData(sql,assetGroupID)); | ||
| int count = connection.ExecuteScalar<int>(countSql, assetGroupID); | ||
|
|
||
| DataTable results = connection.RetrieveData(sql, assetGroupID); | ||
|
|
||
| return Ok(new PagedResults() | ||
| { | ||
| Data = JsonConvert.SerializeObject(results), | ||
| TotalRecords = count, | ||
| RecordsPerPage = recordsPerPage, | ||
| NumberOfPages = (count + recordsPerPage - 1) / recordsPerPage | ||
| }); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
|
|
@@ -283,18 +352,32 @@ public IHttpActionResult GetUserAccounts(int assetGroupID) | |
| return Unauthorized(); | ||
| } | ||
|
|
||
| [HttpGet, Route("{assetGroupID:int}/AssetGroups")] | ||
| public IHttpActionResult GetSubGroups(int assetGroupID) | ||
| [HttpPost, Route("{assetGroupID:int}/AssetGroups/{page:int}")] | ||
| public IHttpActionResult GetSubGroupsPaged([FromBody] PostData postData, [FromUri] int assetGroupID, [FromUri] int page) | ||
| { | ||
| if (GetRoles == string.Empty || User.IsInRole(GetRoles)) | ||
| { | ||
| using (AdoDataConnection connection = new AdoDataConnection(Connection)) | ||
| { | ||
| try | ||
| { | ||
| IEnumerable<AssetGroupView> records = new TableOperations<AssetGroupView>(connection).QueryRecordsWhere("ID in (SELECT ChildAssetGroupID FROM AssetGroupAssetGroupView WHERE ParentAssetGroupID = {0})", assetGroupID); | ||
| int recordsPerPage = PageSize ?? 50; | ||
|
|
||
| return Ok(records); | ||
| TableOperations<AssetGroupView> table = new TableOperations<AssetGroupView>(connection); | ||
|
|
||
| RecordRestriction recordRestriction = new RecordRestriction("ID in (SELECT ChildAssetGroupID FROM AssetGroupAssetGroupView WHERE ParentAssetGroupID = {0})", assetGroupID); | ||
|
|
||
| int count = table.QueryRecordCount(recordRestriction); | ||
|
|
||
| IEnumerable<AssetGroupView> records = new TableOperations<AssetGroupView>(connection).QueryRecords(postData.OrderBy, postData.Ascending, page + 1, recordsPerPage, recordRestriction); | ||
|
|
||
| return Ok(new PagedResults() | ||
| { | ||
| Data = JsonConvert.SerializeObject(records), | ||
| TotalRecords = count, | ||
| RecordsPerPage = recordsPerPage, | ||
| NumberOfPages = (count + recordsPerPage - 1) / recordsPerPage | ||
| }); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -71,7 +71,7 @@ public IHttpActionResult GetAssetLocationsPaged([FromBody] PostData postData, [F | |
| if (!GetAuthCheck()) | ||
| return Unauthorized(); | ||
|
|
||
| int recordsPerPage = Take ?? 50; | ||
| int recordsPerPage = PageSize ?? 50; | ||
|
|
||
| PagedResults results = new PagedResults(); | ||
|
|
||
|
|
@@ -164,7 +164,7 @@ public IHttpActionResult GetAssetMetersPaged([FromBody] PostData postData, [From | |
| { | ||
| try | ||
| { | ||
| int recordsPerPage = Take ?? 50; | ||
| int recordsPerPage = PageSize ?? 50; | ||
|
|
||
| string[] sortFields = { "AssetKey", "Name", "Make", "Model" }; | ||
|
|
||
|
|
@@ -200,11 +200,16 @@ public IHttpActionResult GetAssetAssetConnections([FromBody] PostData postData, | |
| { | ||
| if (GetRoles == string.Empty || User.IsInRole(GetRoles)) | ||
| { | ||
| HashSet<string> validSortFields = new HashSet<string> { "assetname", "assetkey", "name" }; | ||
|
|
||
| if (!validSortFields.Contains(postData.OrderBy.ToLower())) | ||
| return BadRequest($"{postData.OrderBy} is not a valid sort field."); | ||
|
|
||
| using (AdoDataConnection connection = new AdoDataConnection(Connection)) | ||
| { | ||
| try | ||
| { | ||
| int recordsPerPage = Take ?? 50; | ||
| int recordsPerPage = PageSize ?? 50; | ||
|
|
||
| string[] sortFields = { "AssetName", "AssetKey", "Name" }; | ||
|
|
||
|
|
@@ -686,15 +691,17 @@ public IHttpActionResult PostExistingMeterForAsset(int assetID, int meterID) | |
| } | ||
| } | ||
|
|
||
| [HttpGet, Route("{assetID:int}/ConnectedChannels")] | ||
| public IHttpActionResult GetAssetChannels(int assetID) | ||
| [HttpPost, Route("{assetID:int}/ConnectedChannels/{page:int}")] | ||
| public IHttpActionResult GetAssetChannels([FromBody] PostData postData, [FromUri] int assetID, [FromUri] int page) | ||
| { | ||
| if (GetRoles == string.Empty || User.IsInRole(GetRoles)) | ||
| { | ||
| try | ||
| { | ||
| using (AdoDataConnection connection = new AdoDataConnection(Connection)) | ||
| { | ||
| int recordsPerPage = PageSize ?? 50; | ||
|
|
||
| Asset asset = new TableOperations<Asset>(connection).QueryRecordWhere("ID={0}", assetID); | ||
| if (asset is null) | ||
| throw (new Exception($"Asset ID={assetID} not found in OpenXDA database")); | ||
|
|
@@ -706,17 +713,43 @@ public IHttpActionResult GetAssetChannels(int assetID) | |
|
|
||
| if (connectedChannels.Count > 0) | ||
| { | ||
| TableOperations<ChannelDetail> tableOp = new TableOperations<ChannelDetail>(connection); | ||
| // Channels get triplicated from Series Type ID in ChannelDetail View | ||
| IEnumerable<ChannelDetail> uniqueChannels = new TableOperations<ChannelDetail>(connection) | ||
| .QueryRecordsWhere($"ID in ({string.Join(", ", connectedChannels.Select(channels => channels.ID))})") | ||
| .DistinctBy(c => c.ID); | ||
|
|
||
| return Ok(uniqueChannels); | ||
| string countSql = $@" | ||
| SELECT COUNT(*) | ||
| FROM ChannelDetail | ||
| WHERE ID in ({string.Join(", ", connectedChannels.Select(channels => channels.ID))}) | ||
| "; | ||
|
|
||
| string sql = @$" | ||
| SELECT * | ||
| FROM ChannelDetail | ||
| WHERE ID in ({string.Join(", ", connectedChannels.Select(channels => channels.ID))}) | ||
| ORDER BY {postData.OrderBy} {(postData.Ascending ? "ASC" : "DESC")} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This introduces SQLInjection {postData.OrderBy} |
||
| OFFSET {page * recordsPerPage} ROWS | ||
| FETCH NEXT {recordsPerPage} ROWS ONLY | ||
| "; | ||
|
|
||
| int count = connection.ExecuteScalar<int>(countSql); | ||
|
|
||
| DataTable results = connection.RetrieveData(sql); | ||
|
|
||
| return Ok(new PagedResults() | ||
| { | ||
| Data = JsonConvert.SerializeObject(results), | ||
| RecordsPerPage = recordsPerPage, | ||
| TotalRecords = count, | ||
| NumberOfPages = (count + recordsPerPage - 1) / recordsPerPage | ||
| }); | ||
| } | ||
| else | ||
| { | ||
| return Ok(new List<ChannelDetail>()); | ||
| return Ok(new PagedResults() | ||
| { | ||
| Data = JsonConvert.SerializeObject(new List<string>()), // just return an empty list | ||
| RecordsPerPage = recordsPerPage, | ||
| TotalRecords = 0, | ||
| NumberOfPages = 0 | ||
| }); | ||
| } | ||
| } | ||
| } catch (Exception ex) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This introduces SQLInjection {postData.OrderBy}