blob: dcec5d41daa9ac07fc76d29d3620961eb88e38b6 [file] [log] [blame]
Matteo Scandolofb46ae62017-08-08 09:10:50 -07001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Matteo Scandoloa1654572017-11-02 12:45:37 +010018import * as _ from 'lodash';
Matteo Scandolo828d1e82017-01-17 14:49:38 -080019import {IXosAppConfig} from '../../../index';
Matteo Scandoloa1654572017-11-02 12:45:37 +010020import {IXosFormHelpersService} from '../../core/form/form-helpers';
21
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080022export interface IXosResourceService {
23 getResource(url: string): ng.resource.IResourceClass<any>;
24}
25
26export class ModelRest implements IXosResourceService {
Matteo Scandoloa1654572017-11-02 12:45:37 +010027 static $inject = ['$resource', 'AppConfig', 'XosFormHelpers'];
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080028
29 /** @ngInject */
30 constructor(
Matteo Scandolo828d1e82017-01-17 14:49:38 -080031 private $resource: ng.resource.IResourceService,
Matteo Scandoloa1654572017-11-02 12:45:37 +010032 private AppConfig: IXosAppConfig,
33 private XosFormHelpers: IXosFormHelpersService
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080034 ) {
35
36 }
37
38 public getResource(url: string): ng.resource.IResourceClass<ng.resource.IResource<any>> {
Matteo Scandoloa1654572017-11-02 12:45:37 +010039 const self = this;
Matteo Scandolo828d1e82017-01-17 14:49:38 -080040 const resource: angular.resource.IResourceClass<any> = this.$resource(`${this.AppConfig.apiEndpoint}${url}/:id/`, {id: '@id'}, {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080041 update: { method: 'PUT' },
42 query: {
43 method: 'GET',
44 isArray: true,
45 transformResponse: (res) => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080046 return res.items ? res.items : res;
47 }
48 }
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -080049 });
50
51 resource.prototype.$save = function() {
Matteo Scandoloa1654572017-11-02 12:45:37 +010052
53 // NOTE converting dates back to timestamp
54 _.forEach(Object.keys(this), (k: string) => {
55 if (self.XosFormHelpers._getFieldFormat(this[k]) === 'date') {
56 this[k] = new Date(this[k]).getTime();
57 }
58 });
59
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -080060 if (this.id) {
61 return this.$update();
62 } else {
63 return resource.save(this).$promise;
64 }
65 };
66
Matteo Scandoloe9cdf9a2017-11-21 10:41:28 -080067 // resource.prototype.$delete = function() {
68 //
69 // }
70
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -080071 return resource;
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080072 }
73}