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 | |
Matteo Scandolo | a165457 | 2017-11-02 12:45:37 +0100 | [diff] [blame^] | 18 | import * as _ from 'lodash'; |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 19 | import {IXosAppConfig} from '../../../index'; |
Matteo Scandolo | a165457 | 2017-11-02 12:45:37 +0100 | [diff] [blame^] | 20 | import {IXosFormHelpersService} from '../../core/form/form-helpers'; |
| 21 | |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 22 | export interface IXosResourceService { |
| 23 | getResource(url: string): ng.resource.IResourceClass<any>; |
| 24 | } |
| 25 | |
| 26 | export class ModelRest implements IXosResourceService { |
Matteo Scandolo | a165457 | 2017-11-02 12:45:37 +0100 | [diff] [blame^] | 27 | static $inject = ['$resource', 'AppConfig', 'XosFormHelpers']; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 28 | |
| 29 | /** @ngInject */ |
| 30 | constructor( |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 31 | private $resource: ng.resource.IResourceService, |
Matteo Scandolo | a165457 | 2017-11-02 12:45:37 +0100 | [diff] [blame^] | 32 | private AppConfig: IXosAppConfig, |
| 33 | private XosFormHelpers: IXosFormHelpersService |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 34 | ) { |
| 35 | |
| 36 | } |
| 37 | |
| 38 | public getResource(url: string): ng.resource.IResourceClass<ng.resource.IResource<any>> { |
Matteo Scandolo | a165457 | 2017-11-02 12:45:37 +0100 | [diff] [blame^] | 39 | const self = this; |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 40 | 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] | 41 | update: { method: 'PUT' }, |
| 42 | query: { |
| 43 | method: 'GET', |
| 44 | isArray: true, |
| 45 | transformResponse: (res) => { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 46 | return res.items ? res.items : res; |
| 47 | } |
| 48 | } |
Matteo Scandolo | 0a8b02e | 2017-01-06 14:43:36 -0800 | [diff] [blame] | 49 | }); |
| 50 | |
| 51 | resource.prototype.$save = function() { |
Matteo Scandolo | a165457 | 2017-11-02 12:45:37 +0100 | [diff] [blame^] | 52 | |
| 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 Scandolo | 0a8b02e | 2017-01-06 14:43:36 -0800 | [diff] [blame] | 60 | if (this.id) { |
| 61 | return this.$update(); |
| 62 | } else { |
| 63 | return resource.save(this).$promise; |
| 64 | } |
| 65 | }; |
| 66 | |
| 67 | return resource; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 68 | } |
| 69 | } |