article.service.ts
1.73 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
import { Injectable, Inject } from "ng-forward";
import {Article} from "../../../app/models/interfaces";
@Injectable()
@Inject("Restangular", "$q")
export class ArticleService {
constructor(private Restangular: restangular.IService, private $q: ng.IQService, private $log: ng.ILogService) { }
create(profileId: number, article: Article) {
return this.Restangular.one('profiles', profileId).customPOST(
{ article: article },
'articles',
{},
{ 'Content-Type': 'application/json' }
);
}
// 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<T>(deferred: ng.IDeferred<T>): (response: restangular.IResponse) => void {
let self = this;
let successFunction = (response: restangular.IResponse): void => {
this.$log.debug("Request successfull executed", this, response);
deferred.resolve(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<T>(profileId: number, params?: any): ng.IPromise<T> {
let deferred = this.$q.defer<T>();
this.Restangular.one('profiles', profileId).customGET('articles', params).then(this.handleSuccessFactory(deferred));
return deferred.promise;
}
getChildren(articleId: number, params?: any) {
return this.get(articleId).customGET('children', params);
}
private get(articleId: number) {
return this.Restangular.one('articles', articleId);
}
}