Matteo Scandolo | fb46ae6 | 2017-08-08 09:10:50 -0700 | [diff] [blame] | 1 | |
| 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 Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 19 | import {IXosAppConfig} from '../../../index'; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 20 | export interface IXosResourceService { |
| 21 | getResource(url: string): ng.resource.IResourceClass<any>; |
| 22 | } |
| 23 | |
| 24 | export class ModelRest implements IXosResourceService { |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 25 | static $inject = ['$resource', 'AppConfig']; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 26 | |
| 27 | /** @ngInject */ |
| 28 | constructor( |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 29 | private $resource: ng.resource.IResourceService, |
| 30 | private AppConfig: IXosAppConfig |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 31 | ) { |
| 32 | |
| 33 | } |
| 34 | |
| 35 | public getResource(url: string): ng.resource.IResourceClass<ng.resource.IResource<any>> { |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 36 | const resource: angular.resource.IResourceClass<any> = this.$resource(`${this.AppConfig.apiEndpoint}${url}/:id/`, {id: '@id'}, { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 37 | 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 Scandolo | 0a8b02e | 2017-01-06 14:43:36 -0800 | [diff] [blame] | 46 | }); |
| 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 Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 57 | } |
| 58 | } |