blob: 8bd2c1faa7240d986376201b526accc51634035d [file] [log] [blame]
Matteo Scandolof2c3ed62016-12-15 14:32:50 -08001import {AppConfig} from '../../config/app.config';
2
3export interface IXosResourceService {
4 getResource(url: string): ng.resource.IResourceClass<any>;
5}
6
7export class ModelRest implements IXosResourceService {
8 static $inject = ['$resource'];
Matteo Scandolof2c3ed62016-12-15 14:32:50 -08009
10 /** @ngInject */
11 constructor(
12 private $resource: ng.resource.IResourceService
13 ) {
14
15 }
16
17 public getResource(url: string): ng.resource.IResourceClass<ng.resource.IResource<any>> {
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -080018 const resource: angular.resource.IResourceClass<any> = this.$resource(`${AppConfig.apiEndpoint}${url}/:id/`, {id: '@id'}, {
19 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}