import { Injectable, Inject } from "ng-forward"; import {RestangularService} from "./restangular_service"; import {ProfileService} from "./profile.service"; @Injectable() @Inject("Restangular", "$q", "$log", ProfileService) export class ArticleService extends RestangularService { constructor(Restangular: restangular.IService, $q: ng.IQService, $log: ng.ILogService, protected profileService: ProfileService) { super(Restangular, $q, $log); } getResourcePath() { return "articles"; } getDataKeys() { return { singular: 'article', plural: 'articles' }; } updateArticle(article: noosfero.Article) { let headers = { 'Content-Type': 'application/json' }; let deferred = this.$q.defer>(); let attributesToUpdate: any = { article: { name: article.name, body: article.body, published: article.published } }; let restRequest: ng.IPromise> = this.getElement(article.id).customPOST(attributesToUpdate, null, null, headers); restRequest.then(this.getHandleSuccessFunction(deferred)) .catch(this.getHandleErrorFunction(deferred)); return deferred.promise; } createInProfile(profile: noosfero.Profile, article: noosfero.Article): ng.IPromise> { let profileElement = this.profileService.get(profile.id); (profileElement).id = profile.id; let headers = { 'Content-Type': 'application/json' }; return this.create(article, profileElement, null, headers); } createInParent(parentId: number, article: noosfero.Article): ng.IPromise> { let headers = { 'Content-Type': 'application/json' }; let parent = this.getElement(parentId); return this.create(article, parent, null, headers, true, "children"); } getAsCollectionChildrenOf(rootElement: noosfero.Environment | noosfero.Article | noosfero.Profile, path: string, queryParams?: any, headers?: any): restangular.ICollectionPromise { return rootElement.getList(path, queryParams, headers); } getAsElementChildrenOf(rootElement: noosfero.Environment | noosfero.Article | noosfero.Profile, path: string, id: number, queryParams?: any, headers?: any) { return rootElement.one(path, id).get(queryParams, headers); } getByProfile(profile: noosfero.Profile, params?: any): ng.IPromise> { let profileElement = this.profileService.get(profile.id); return this.list(profileElement, params); } getArticleByProfileAndPath(profile: noosfero.Profile, path: string): ng.IPromise> { let deferred = this.$q.defer>(); let profileElement = this.profileService.get(profile.id); let restRequest: ng.IPromise; let params = { path: path }; restRequest = profileElement.customGET(this.getResourcePath(), params); restRequest .then(this.getHandleSuccessFunction(deferred)) .catch(this.getHandleErrorFunction(deferred)); return deferred.promise; } getOneByProfile(profile: noosfero.Profile, params?: any): ng.IPromise> { let profileElement = this.profileService.get(profile.id); return this.getSub(profileElement, params); } getChildren(article: noosfero.Article, params?: any): ng.IPromise> { let articleElement = this.getElement(article.id); articleElement.id = article.id; return this.listSubElements(articleElement, "children", params); } }