blob: 12590afba353be70be51c428fa92ef3da1d68449 [file] [log] [blame]
Matteo Scandolo828d1e82017-01-17 14:49:38 -08001import {IXosAppConfig} from '../../../index';
Matteo Scandolof2c3ed62016-12-15 14:32:50 -08002export interface IXosResourceService {
3 getResource(url: string): ng.resource.IResourceClass<any>;
4}
5
6export class ModelRest implements IXosResourceService {
Matteo Scandolo828d1e82017-01-17 14:49:38 -08007 static $inject = ['$resource', 'AppConfig'];
Matteo Scandolof2c3ed62016-12-15 14:32:50 -08008
9 /** @ngInject */
10 constructor(
Matteo Scandolo828d1e82017-01-17 14:49:38 -080011 private $resource: ng.resource.IResourceService,
12 private AppConfig: IXosAppConfig
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080013 ) {
14
15 }
16
17 public getResource(url: string): ng.resource.IResourceClass<ng.resource.IResource<any>> {
Matteo Scandolo828d1e82017-01-17 14:49:38 -080018 const resource: angular.resource.IResourceClass<any> = this.$resource(`${this.AppConfig.apiEndpoint}${url}/:id/`, {id: '@id'}, {
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -080019 update: { method: 'PUT' }
20 });
21
22 resource.prototype.$save = function() {
23 if (this.id) {
24 return this.$update();
25 } else {
26 return resource.save(this).$promise;
27 }
28 };
29
30 return resource;
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080031 }
32}