import { Injectable, Inject } from "ng-forward"; import {RestangularWrapperService} from "./restangular_wrapper_service"; @Injectable() @Inject("Restangular", "$q") export class ArticleService extends RestangularWrapperService { constructor(Restangular: restangular.IService, $q: ng.IQService, $log: ng.ILogService) { super(Restangular, $q, $log); } getPath() { return "articles"; } getDataKeys() { return { singular: 'article', plural: 'articles' }; } create(profileId: number, article: noosfero.Article): ng.IPromise { return this.post(this.Restangular.one('profiles', profileId), article); } // // TODO create a handle ErrorFactory too and move handleSuccessFactory and handleErrorFactory // // to a base class (of course we will have to creates a base class too) // handleSuccessFactory(deferred: ng.IDeferred): (response: restangular.IResponse) => void { // let self = this; // let successFunction = (response: restangular.IResponse): void => { // this.$log.debug("Request successfull executed", self, response); // deferred.resolve(response.data); // }; // return successFunction; // } // // handleErrorFactory(deferred: ng.IDeferred): (response: restangular.IResponse) => void { // let self = this; // let successFunction = (response: restangular.IResponse): void => { // this.$log.error("Error executing request", self, response); // deferred.reject(response.data); // }; // return successFunction; // } // TODO -> change all Restangular services to this approach "Return promise to a specific type" // it makes easy consume the service getByProfile(profileId: number, params?: any): ng.IPromise { let deferred = this.$q.defer(); this.Restangular.one('profiles', profileId).customGET('articles', params) .then(this.getHandleSuccessFunction(deferred, 'articles')) .catch(this.getHandleErrorFunction(deferred)); return deferred.promise; } getChildren(articleId: number, params?: any): ng.IPromise { let deferred = this.$q.defer(); this.get(articleId).customGET('children', params) .then(this.getHandleSuccessFunction(deferred, 'articles').bind(this)) .catch(this.getHandleErrorFunction(deferred)); return deferred.promise; } }