blob: 6c72d99ca23e85cd44cde76c83b901ce06269a47 [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
18
Matteo Scandolo828d1e82017-01-17 14:49:38 -080019import {IXosAppConfig} from '../../../index';
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080020export interface IXosResourceService {
21 getResource(url: string): ng.resource.IResourceClass<any>;
22}
23
24export class ModelRest implements IXosResourceService {
Matteo Scandolo828d1e82017-01-17 14:49:38 -080025 static $inject = ['$resource', 'AppConfig'];
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080026
27 /** @ngInject */
28 constructor(
Matteo Scandolo828d1e82017-01-17 14:49:38 -080029 private $resource: ng.resource.IResourceService,
30 private AppConfig: IXosAppConfig
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080031 ) {
32
33 }
34
35 public getResource(url: string): ng.resource.IResourceClass<ng.resource.IResource<any>> {
Matteo Scandolo828d1e82017-01-17 14:49:38 -080036 const resource: angular.resource.IResourceClass<any> = this.$resource(`${this.AppConfig.apiEndpoint}${url}/:id/`, {id: '@id'}, {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080037 update: { method: 'PUT' },
38 query: {
39 method: 'GET',
40 isArray: true,
41 transformResponse: (res) => {
42 // FIXME chameleon return everything inside "items"
43 return res.items ? res.items : res;
44 }
45 }
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -080046 });
47
48 resource.prototype.$save = function() {
49 if (this.id) {
50 return this.$update();
51 } else {
52 return resource.save(this).$promise;
53 }
54 };
55
56 return resource;
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080057 }
58}