aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/rest
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/app/shared/rest')
-rw-r--r--client/src/app/shared/rest/index.ts1
-rw-r--r--client/src/app/shared/rest/rest-data-source.ts98
-rw-r--r--client/src/app/shared/rest/rest-extractor.service.ts66
-rw-r--r--client/src/app/shared/rest/rest-pagination.ts5
-rw-r--r--client/src/app/shared/rest/rest-table.ts27
-rw-r--r--client/src/app/shared/rest/rest.service.ts31
6 files changed, 116 insertions, 112 deletions
diff --git a/client/src/app/shared/rest/index.ts b/client/src/app/shared/rest/index.ts
index e0be155cf..3f1996130 100644
--- a/client/src/app/shared/rest/index.ts
+++ b/client/src/app/shared/rest/index.ts
@@ -2,3 +2,4 @@ export * from './rest-data-source'
2export * from './rest-extractor.service' 2export * from './rest-extractor.service'
3export * from './rest-pagination' 3export * from './rest-pagination'
4export * from './rest.service' 4export * from './rest.service'
5export * from './rest-table'
diff --git a/client/src/app/shared/rest/rest-data-source.ts b/client/src/app/shared/rest/rest-data-source.ts
index 5c205d280..57a2efb57 100644
--- a/client/src/app/shared/rest/rest-data-source.ts
+++ b/client/src/app/shared/rest/rest-data-source.ts
@@ -1,68 +1,32 @@
1import { Http, RequestOptionsArgs, URLSearchParams, Response } from '@angular/http' 1export class RestDataSource {
2 2 // protected addSortRequestOptions (requestOptions: RequestOptionsArgs) {
3import { ServerDataSource } from 'ng2-smart-table' 3 // const searchParams = requestOptions.params as URLSearchParams
4 4 //
5export class RestDataSource extends ServerDataSource { 5 // if (this.sortConf) {
6 private updateResponse: (input: any[]) => any[] 6 // this.sortConf.forEach((fieldConf) => {
7 7 // const sortPrefix = fieldConf.direction === 'desc' ? '-' : ''
8 constructor (http: Http, endpoint: string, updateResponse?: (input: any[]) => any[]) { 8 //
9 const options = { 9 // searchParams.set(this.conf.sortFieldKey, sortPrefix + fieldConf.field)
10 endPoint: endpoint, 10 // })
11 sortFieldKey: 'sort', 11 // }
12 dataKey: 'data' 12 //
13 } 13 // return requestOptions
14 super(http, options) 14 // }
15 15 //
16 if (updateResponse) { 16 // protected addPagerRequestOptions (requestOptions: RequestOptionsArgs) {
17 this.updateResponse = updateResponse 17 // const searchParams = requestOptions.params as URLSearchParams
18 } 18 //
19 } 19 // if (this.pagingConf && this.pagingConf['page'] && this.pagingConf['perPage']) {
20 20 // const perPage = this.pagingConf['perPage']
21 protected extractDataFromResponse (res: Response) { 21 // const page = this.pagingConf['page']
22 const json = res.json() 22 //
23 if (!json) return [] 23 // const start = (page - 1) * perPage
24 let data = json.data 24 // const count = perPage
25 25 //
26 if (this.updateResponse !== undefined) { 26 // searchParams.set('start', start.toString())
27 data = this.updateResponse(data) 27 // searchParams.set('count', count.toString())
28 } 28 // }
29 29 //
30 return data 30 // return requestOptions
31 } 31 // }
32
33 protected extractTotalFromResponse (res: Response) {
34 const rawData = res.json()
35 return rawData ? parseInt(rawData.total, 10) : 0
36 }
37
38 protected addSortRequestOptions (requestOptions: RequestOptionsArgs) {
39 const searchParams = requestOptions.params as URLSearchParams
40
41 if (this.sortConf) {
42 this.sortConf.forEach((fieldConf) => {
43 const sortPrefix = fieldConf.direction === 'desc' ? '-' : ''
44
45 searchParams.set(this.conf.sortFieldKey, sortPrefix + fieldConf.field)
46 })
47 }
48
49 return requestOptions
50 }
51
52 protected addPagerRequestOptions (requestOptions: RequestOptionsArgs) {
53 const searchParams = requestOptions.params as URLSearchParams
54
55 if (this.pagingConf && this.pagingConf['page'] && this.pagingConf['perPage']) {
56 const perPage = this.pagingConf['perPage']
57 const page = this.pagingConf['page']
58
59 const start = (page - 1) * perPage
60 const count = perPage
61
62 searchParams.set('start', start.toString())
63 searchParams.set('count', count.toString())
64 }
65
66 return requestOptions
67 }
68} 32}
diff --git a/client/src/app/shared/rest/rest-extractor.service.ts b/client/src/app/shared/rest/rest-extractor.service.ts
index f6a818ec8..32dad5c73 100644
--- a/client/src/app/shared/rest/rest-extractor.service.ts
+++ b/client/src/app/shared/rest/rest-extractor.service.ts
@@ -1,52 +1,58 @@
1import { Injectable } from '@angular/core' 1import { Injectable } from '@angular/core'
2import { Response } from '@angular/http'
3import { Observable } from 'rxjs/Observable' 2import { Observable } from 'rxjs/Observable'
3import { HttpErrorResponse } from '@angular/common/http'
4 4
5export interface ResultList { 5import { Utils } from '../utils'
6 data: any[] 6import { ResultList } from '../../../../../shared'
7 total: number
8}
9 7
10@Injectable() 8@Injectable()
11export class RestExtractor { 9export class RestExtractor {
12 10
13 extractDataBool (res: Response) { 11 extractDataBool () {
14 return true 12 return true
15 } 13 }
16 14
17 extractDataList (res: Response) { 15 applyToResultListData <T> (result: ResultList<T>, fun: Function, additionalArgs?: any[]): ResultList<T> {
18 const body = res.json() 16 const data: T[] = result.data
17 const newData: T[] = []
19 18
20 const ret: ResultList = { 19 data.forEach(d => newData.push(fun.call(this, d, additionalArgs)))
21 data: body.data,
22 total: body.total
23 }
24 20
25 return ret 21 return {
22 total: result.total,
23 data: newData
24 }
26 } 25 }
27 26
28 extractDataGet (res: Response) { 27 convertResultListDateToHuman <T> (result: ResultList<T>, fieldsToConvert: string[] = [ 'createdAt' ]): ResultList<T> {
29 return res.json() 28 return this.applyToResultListData(result, this.convertDateToHuman, [ fieldsToConvert ])
30 } 29 }
31 30
32 handleError (res: Response) { 31 convertDateToHuman (target: object, fieldsToConvert: string[]) {
33 let text = 'Server error: ' 32 const source = {}
34 text += res.text() 33 fieldsToConvert.forEach(field => {
35 let json = '' 34 source[field] = Utils.dateToHuman(target[field])
35 })
36 36
37 try { 37 return Object.assign(target, source)
38 json = res.json() 38 }
39 } catch (err) {
40 console.error('Cannot get JSON from response.')
41 }
42 39
43 const error = { 40 handleError (err: HttpErrorResponse) {
44 json, 41 let errorMessage
45 text 42
43 if (err.error instanceof Error) {
44 // A client-side or network error occurred. Handle it accordingly.
45 errorMessage = err.error.message
46 console.error('An error occurred:', errorMessage)
47 } else if (err.status !== undefined) {
48 // The backend returned an unsuccessful response code.
49 // The response body may contain clues as to what went wrong,
50 errorMessage = err.error
51 console.error(`Backend returned code ${err.status}, body was: ${errorMessage}`)
52 } else {
53 errorMessage = err
46 } 54 }
47 55
48 console.error(error) 56 return Observable.throw(errorMessage)
49
50 return Observable.throw(error)
51 } 57 }
52} 58}
diff --git a/client/src/app/shared/rest/rest-pagination.ts b/client/src/app/shared/rest/rest-pagination.ts
index 766e7a9e5..0faa59303 100644
--- a/client/src/app/shared/rest/rest-pagination.ts
+++ b/client/src/app/shared/rest/rest-pagination.ts
@@ -1,5 +1,4 @@
1export interface RestPagination { 1export interface RestPagination {
2 currentPage: number 2 start: number
3 itemsPerPage: number 3 count: number
4 totalItems: number
5} 4}
diff --git a/client/src/app/shared/rest/rest-table.ts b/client/src/app/shared/rest/rest-table.ts
new file mode 100644
index 000000000..db2cb5e14
--- /dev/null
+++ b/client/src/app/shared/rest/rest-table.ts
@@ -0,0 +1,27 @@
1import { LazyLoadEvent, SortMeta } from 'primeng/primeng'
2
3import { RestPagination } from './rest-pagination'
4
5export abstract class RestTable {
6 abstract totalRecords: number
7 abstract rowsPerPage: number
8 abstract sort: SortMeta
9 abstract pagination: RestPagination
10
11 protected abstract loadData (): void
12
13 loadLazy (event: LazyLoadEvent) {
14 this.sort = {
15 order: event.sortOrder,
16 field: event.sortField
17 }
18
19 this.pagination = {
20 start: event.first,
21 count: this.rowsPerPage
22 }
23
24 this.loadData()
25 }
26
27}
diff --git a/client/src/app/shared/rest/rest.service.ts b/client/src/app/shared/rest/rest.service.ts
index 43dc20b34..f7838ba06 100644
--- a/client/src/app/shared/rest/rest.service.ts
+++ b/client/src/app/shared/rest/rest.service.ts
@@ -1,27 +1,34 @@
1import { Injectable } from '@angular/core' 1import { Injectable } from '@angular/core'
2import { URLSearchParams } from '@angular/http' 2import { HttpParams } from '@angular/common/http'
3import { SortMeta } from 'primeng/primeng'
3 4
4import { RestPagination } from './rest-pagination' 5import { RestPagination } from './rest-pagination'
5 6
6@Injectable() 7@Injectable()
7export class RestService { 8export class RestService {
8 9
9 buildRestGetParams (pagination?: RestPagination, sort?: string) { 10 addRestGetParams (params: HttpParams, pagination?: RestPagination, sort?: SortMeta | string) {
10 const params = new URLSearchParams() 11 let newParams = params
11 12
12 if (pagination) { 13 if (pagination !== undefined) {
13 const start: number = (pagination.currentPage - 1) * pagination.itemsPerPage 14 newParams = newParams.set('start', pagination.start.toString())
14 const count: number = pagination.itemsPerPage 15 .set('count', pagination.count.toString())
15
16 params.set('start', start.toString())
17 params.set('count', count.toString())
18 } 16 }
19 17
20 if (sort) { 18 if (sort !== undefined) {
21 params.set('sort', sort) 19 let sortString = ''
20
21 if (typeof sort === 'string') {
22 sortString = sort
23 } else {
24 const sortPrefix = sort.order === 1 ? '' : '-'
25 sortString = sortPrefix + sort.field
26 }
27
28 newParams = newParams.set('sort', sortString)
22 } 29 }
23 30
24 return params 31 return newParams
25 } 32 }
26 33
27} 34}