Skip to content

BigBadJock/REST-Parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

139 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

REST-Parser

A C# Parser for REST requests generating Linq expressions

The REST query allows for filtering, sorting and paging.

Filters

Filters work with all types

Operator Description Example Field Types Comparison Operators
eq Equal
  • city[eq]=Redmond
  • id=[eq]=673171EA-813F-4CF8-B9D3-CBCBACBD4F89
  • birthDate[eq]=2001-01-01
  • price[eq]=5.99
  • isActive[eq]=true
string, date, int, decimal, double, boolean
ne Not equal
  • city[ne]=London
string, date, int, decimal, double, boolean
gt Greater than
  • price[gt]=20
  • birthDate[gt]=2001-01-01
date, int, decimal, double
ge Greater than or equal
  • price[ge]=10
  • birthDate[ge]=2001-01-01
date, int, decimal, double
lt Less than
  • price[lt]=20
  • birthDate[lt]=2001-01-01
date, int, decimal, double
le Less than or equal
  • price[le]=100
  • birthDate[le]=2001-01-01
date, int, decimal, double
contains contains the value
  • surname[contains]=McA
string
notcontains does not contain the value
  • surname[notcontains]=Mc
string
startswith starts with the value
  • surname[startswith]=Mc
string
isnull checks if the value is null
  • surname[isnull]=false
string

Note: The default filter is [eq] It is case insensitive

Sorting

You can sort by multiple fields ascending or descending.

  • GET /items?code=xxx&rest=$sort_by=surname
  • GET /items?code=xxx&rest=$sort_by[asc]=surname
  • GET /items?code=xxx&rest=$sort_by[desc]=surname
  • GET /items?code=xxx&rest=$sort_by[asc]=surname&$sort_by[desc]=firstname

Pagination

Pagination is optional, by default all records are returned. If pagination is required, a sort_by should also be added

  • GET /items?code=xxx&rest=$page=1&$pageSize=10
  • GET /items?code=xxx&rest=$page=2&$pageSize=10
  • GET /items?code=xxx&rest=$page=5&$pageSize=25

All Together

GET /items?code=xxx&rest=surname[contains]=Smi
    &hometown[eq]=Edinburgh
  &$sort_by[asc]=surname
  &$sort_by[desc]=firstname
  &$page=1
  &$pageSize=10

Results The result is a JSON object with two properties Data, and Pagination:

{
    "data" : [
        {  "id":"1", "firstName":"Bob", "lastName": "Smith"..... },
        {  "id":"2", "firstName":"Rob", "lastName": "Smith"..... },
        ...
        {  }
    ],
    "Pagination": {
        "PageNumber": 1,
        "PageSize": 10,
        "PageCount": 2,
        "TotalCount": 19
    }
}

Progress

21/04/2020

Fixed three issues including the [contains] on a nullable string.

30/12/2019

Updated to handle nullable types. There is a remaining issue throwing an exception when using [contains] on a nullable string.

30/11/2019

Changed result from parse to RestResult object, which contains expressions, sort expressions with paging to come. Added Run command to parser which takes a IQueryable data source, and the rest request, parses, runs against the data and returns the results.

25/11/2019

Refactored. Removed SQL generator.

22/11/2019

Removed Specflow tests. Added unit tests. tests added for RestToLinq String Equals

About

A C# Parser for REST requests generating Linq expressions to be used in an API

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages