HttpDataController

module:most-web.controllers. HttpDataController

HttpDataController class describes a common MOST Web Framework data controller. This controller is inherited by default from all data models. It offers a set of basic actions for CRUD operations against data objects and allows filtering, paging, sorting and grouping data objects with options similar to OData.

Basic Features

Data Filtering ($filter query option)

Logical Operators

The following table contains the logical operators supported in the query language:

OperatorDescriptionExample
eqEqual/Order/index.json?$filter=customer eq 353
neNot Equal/Order/index.json?$filter=orderStatus/alternateName ne 'OrderDelivered'
gtGreater than/Order/index.json?$filter=orderedItem/price gt 1000
geGreater than or equal/Order/index.json?$filter=orderedItem/price ge 500
ltLower than/Order/index.json?$filter=orderedItem/price lt 500
leLower than or equal/Order/index.json?$filter=orderedItem/price le 1000
andLogical and/Order/index.json?$filter=orderedItem/price gt 1000 and orderStatus/alternateName eq 'OrderPickup'
orLogical or/Order/index.json?$filter=orderStatus/alternateName eq 'OrderPickup' or orderStatus/alternateName eq 'OrderProcessing'

Arithmetic Operators

The following table contains the arithmetic operators supported in the query language:

OperatorDescriptionExample
addAddition/Order/index.json?$filter=(orderedItem/price add 10) gt 1560
subSubtraction/Order/index.json?$filter=(orderedItem/price sub 10) gt 1540
mulMultiplication/Order/index.json?$filter=(orderedItem/price mul 1.20) gt 1000
divDivision/Order/index.json?$filter=(orderedItem/price div 2) le 500
modModulo/Order/index.json?$filter=(orderedItem/price mod 2) eq 0

Functions

A set of functions are also defined for use in $filter query option:

FunctionExample
String Functions
startswith(field,string)/Product/index.json?$filter=startswith(name,'Apple') eq true
endswith(field,string)/Product/index.json?$filter=endswith(name,'Workstation') eq true
contains(field,string)/Product/index.json?$filter=contains(name,'MacBook') eq true
length(field)/Product/index.json?$filter=length(name) gt 40
indexof(field,string)/Product/index.json?$filter=indexof(name,'Air') gt 1
substring(field,number)/Product/index.json?$filter=substring(category,1) eq 'aptops'
substring(field,number,number)/Product/index.json?$filter=substring(category,1,2) eq 'ap'
tolower(field)/Product/index.json?$filter=tolower(category) eq 'laptops'
toupper(field)/Product/index.json?$filter=toupper(category) eq 'LAPTOPS'
trim(field)/Product/index.json?$filter=trim(category) eq 'Laptops'
Date Functions
day(field)/Order/index.json?$filter=day(orderDate) eq 4
month(field)/Order/index.json?$filter=month(orderDate) eq 6
year(field)/Order/index.json?$filter=year(orderDate) ge 2014
hour(field)/Order/index.json?$filter=hour(orderDate) ge 12 and hour(orderDate) lt 14
minute(field)/Order/index.json?$filter=minute(orderDate) gt 15 and minute(orderDate) le 30
second(field)/Order/index.json?$filter=second(orderDate) ge 0 and second(orderDate) le 45
date(field)/Order/index.json?$filter=date(orderDate) eq '2015-03-20'
Math Functions
round(field)/Product/index.json?$filter=round(price) le 389
floor(field)/Product/index.json?$filter=floor(price) eq 389
ceiling(field)/Product/index.json?$filter=ceiling(price) eq 390

Attribute Selection ($select query option)

The following table contains attribute selection expressions supported in the query language:

DescriptionExample
Select attribute/Order/index.json?$select=id,customer,orderStatus
Select attribute with alias/Order/index.json?$select=id,customer/description as customerName,orderStatus/name as orderStatusName
Select attribute with aggregation/Order/index.json?$select=count(id) as totalCount&$filter=orderStatus/alternateName eq 'OrderProcessing'
 /Product/index.json?$select=max(price) as maxPrice&$filter=category eq 'Laptops'
 /Product/index.json?$select=min(price) as minPrice&$filter=category eq 'Laptops'

Data Sorting ($orderby or $order query options)

DescriptionExample
Ascending order/Product/index.json?$orderby=name
Descending order/Product/index.json?$orderby=category desc,name desc

Data Paging ($top, $skip and $inlinecount query options)

The $top query option allows developers to apply paging in the result-set by giving the max number of records for each page. The default value is 25. The $skip query option provides a way to skip a number of records. The default value is 0. The $inlinecount query option includes in the result-set the total number of records of the query expression provided:


 {
     "total": 94,
     "records": [ ... ]
 }
  

The default value is false.

DescriptionExample
Limit records/Product/index.json?$top=5
Skip records/Product/index.json?$top=5&$skip=5
Paged records/Product/index.json?$top=5&$skip=5&$inlinecount=true

Data Grouping ($groupby or $group query options)

The $groupby query option allows developers to group the result-set by one or more attributes

DescriptionExample
group/Product/index.json?$select=count(id) as totalCount,category&$groupby=category
group and sort/Product/index.json?$select=count(id) as totalCount,category&$groupby=category&$orderby=count(id) desc

Data Expanding ($expand)

The $expand query option forces response to include associated objects which are not marked as expandable by default.

DescriptionExample
expand/Order/index.json?$filter=orderStatus/alternateName eq 'OrderProcessing'&$expand=customer

The $expand option is optional for a DataField marked as expandable.

Constructor

new HttpDataController()

Properties:
Name Type Description
model DataModel Gets or sets the current data model.
Source:

Extends

  • HttpController

Methods

association(callback)

Returns an instance of HttpResult class which contains a collection of items based on the specified association. This association should be a one-to-many association or many-many association. A routing for this action may be:

 { "url":"/:controller/:parent/:model/index.json", "mime":"application/json", "action":"association" }
 

or


 { "url":"/:controller/:parent/:model/index.html", "mime":"text/html", "action":"association" }
 

 //get orders in JSON format
 /GET /Party/353/Order/index.json
 

This action supports common query options like $filter, $order, $top, $skip etc. The result will be a result-set with associated items:


    //JSON Results:
 {
        "total": 8,
        "skip": 0,
        "records": [
            {
            "id": 37,
            "customer": 353,
            "orderDate": "2015-05-05 01:19:34.000+03:00",
            "orderedItem": {
                "id": 407,
                "additionalType": "Product",
                "category": "PC Components",
                "price": 1625.49,
                "model": "HR5845",
                "releaseDate": "2015-09-20 03:35:33.000+03:00",
                "name": "Nvidia GeForce GTX 650 Ti Boost",
                "dateCreated": "2015-11-23 14:53:04.884+02:00",
                "dateModified": "2015-11-23 14:53:04.887+02:00"
            },
            "orderNumber": "OFV804",
            "orderStatus": {
                "id": 1,
                "name": "Delivered",
                "alternateName": "OrderDelivered",
                "description": "Representing the successful delivery of an order."
            },
            "paymentDue": "2015-05-25 01:19:34.000+03:00",
            "paymentMethod": {
                "id": 6,
                "name": "Direct Debit",
                "alternateName": "DirectDebit",
                "description": "Payment by direct debit"
            },
            "additionalType": "Order",
            "dateCreated": "2015-11-23 21:00:18.264+02:00",
            "dateModified": "2015-11-23 21:00:18.266+02:00"
            }
        ...]
   ...
}
Parameters:
Name Type Description
callback function A callback function where the first argument will contain the Error object if an error occured, or null otherwise.
Source:

edit(callback)

Handles data object edit (e.g. /user/1/edit.html, /user/1/edit.json etc)
Parameters:
Name Type Description
callback function
Source:

index(callback)

Parameters:
Name Type Description
callback function
Source:

new(callback)

Handles data object creation (e.g. /user/1/new.html, /user/1/new.json etc)
Parameters:
Name Type Description
callback function
Source:

remove(callback)

Handles data object deletion (e.g. /user/1/remove.html, /user/1/remove.json etc)
Parameters:
Name Type Description
callback function
Source:

show(callback)

Handles data object display (e.g. /user/1/show.html, /user/1/show.json etc)
Parameters:
Name Type Description
callback function
Source: