article.service.ts
5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { Injectable, Inject, EventEmitter } from "ng-forward";
import {RestangularService} from "./restangular_service";
import {ProfileService} from "./profile.service";
import {NoosferoRootScope} from "./../../../app/shared/models/interfaces";
@Injectable()
@Inject("Restangular", "$q", "$log", ProfileService)
export class ArticleService extends RestangularService<noosfero.Article> {
private articleRemoved: EventEmitter<noosfero.Article> = new EventEmitter<noosfero.Article>();
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'
};
}
removeArticle(article: noosfero.Article) {
let restRequest: ng.IPromise<noosfero.RestResult<noosfero.Article>> = this.remove(article);
let deferred = this.$q.defer<noosfero.RestResult<noosfero.Article>>();
restRequest.then((result: any) => {
this.notifyArticleRemovedListeners(article);
}).catch(this.getHandleErrorFunction(deferred));
return deferred.promise;
}
/**
* Notify listeners that this article has been removed
*/
private notifyArticleRemovedListeners(article: noosfero.Article) {
this.articleRemoved.next(article);
}
/**
* subscribes to the ArticleRemoved event emitter
*/
subscribeToArticleRemoved(fn: Function) {
this.articleRemoved.subscribe(fn);
}
updateArticle(article: noosfero.Article) {
let headers = {
'Content-Type': 'application/json'
};
let deferred = this.$q.defer<noosfero.RestResult<noosfero.Article>>();
// TODO dynamically copy the selected attributes to update
let attributesToUpdate: any = {
article: {
name: article.name, body: article.body, published: article.published,
start_date: article['start_date'], end_date: article['end_date']
}
};
let restRequest: ng.IPromise<noosfero.RestResult<noosfero.Article>> = 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<noosfero.RestResult<noosfero.Article>> {
let profileElement = this.profileService.get(<number>profile.id);
(<any>profileElement).id = profile.id;
let headers = {
'Content-Type': 'application/json'
};
return this.create(article, <noosfero.RestModel>profileElement, null, headers);
}
createInParent(parentId: number, article: noosfero.Article): ng.IPromise<noosfero.RestResult<noosfero.Article>> {
let headers = {
'Content-Type': 'application/json'
};
let parent = this.getElement(parentId);
return this.create(article, parent, null, headers, true, "children");
}
getAsCollectionChildrenOf<C>(rootElement: noosfero.Environment | noosfero.Article | noosfero.Profile, path: string, queryParams?: any, headers?: any): restangular.ICollectionPromise<C> {
return rootElement.getList<C>(path, queryParams, headers);
}
getAsElementChildrenOf<C>(rootElement: noosfero.Environment | noosfero.Article | noosfero.Profile, path: string, id: number, queryParams?: any, headers?: any) {
return rootElement.one(path, id).get<C>(queryParams, headers);
}
getByProfile<T>(profile: noosfero.Profile, params?: any): ng.IPromise<noosfero.RestResult<noosfero.Article[]>> {
let profileElement = this.profileService.get(<number>profile.id);
return this.list(profileElement, params);
}
getArticleByProfileAndPath(profile: noosfero.Profile, path: string): ng.IPromise<noosfero.RestResult<noosfero.Article>> {
let deferred = this.$q.defer<noosfero.RestResult<noosfero.Article>>();
let profileElement = this.profileService.get(<number>profile.id);
let restRequest: ng.IPromise<any>;
let params = { path: path };
restRequest = profileElement.customGET(this.getResourcePath(), params);
restRequest
.then(this.getHandleSuccessFunction(deferred))
.catch(this.getHandleErrorFunction(deferred));
return deferred.promise;
}
getOneByProfile<T>(profile: noosfero.Profile, params?: any): ng.IPromise<noosfero.RestResult<noosfero.Article>> {
let profileElement = this.profileService.get(<number>profile.id);
return this.getSub(profileElement, params);
}
getChildren<T>(article: noosfero.Article, params?: any): ng.IPromise<noosfero.RestResult<noosfero.Article[]>> {
let articleElement = this.getElement(article.id);
articleElement.id = article.id;
return this.listSubElements(<noosfero.Article>articleElement, "children", params);
}
}