Commit 428991c16be5d67b371bf02478290535d7653979
1 parent
f6a236c8
Exists in
master
and in
1 other branch
refactory to add a customized method on article service to get article of a prof…
…ile given a specific path
Showing
5 changed files
with
51 additions
and
5 deletions
Show diff stats
src/app/content-viewer/content-viewer.component.spec.ts
@@ -69,7 +69,7 @@ describe('Content Viewer Component', () => { | @@ -69,7 +69,7 @@ describe('Content Viewer Component', () => { | ||
69 | return helpers.mocks.promiseResultTemplate(profile); | 69 | return helpers.mocks.promiseResultTemplate(profile); |
70 | }; | 70 | }; |
71 | 71 | ||
72 | - helpers.mocks.articleService.getOneByProfile = (id: number, params: any) => { | 72 | + helpers.mocks.articleService.getArticleByProfileAndPath = (profile: noosfero.Profile, path: string) => { |
73 | return helpers.mocks.promiseResultTemplate({ | 73 | return helpers.mocks.promiseResultTemplate({ |
74 | data: article | 74 | data: article |
75 | }); | 75 | }); |
src/app/content-viewer/content-viewer.component.ts
@@ -30,8 +30,8 @@ export class ContentViewer { | @@ -30,8 +30,8 @@ export class ContentViewer { | ||
30 | activate() { | 30 | activate() { |
31 | this.profileService.getCurrentProfile().then((profile: noosfero.Profile) => { | 31 | this.profileService.getCurrentProfile().then((profile: noosfero.Profile) => { |
32 | this.profile = profile; | 32 | this.profile = profile; |
33 | - return this.articleService.getOneByProfile(<any>this.profile, { path: this.$stateParams["page"] }); | ||
34 | - }).then((result: noosfero.RestResult<noosfero.Article>) => { | 33 | + return this.articleService.getArticleByProfileAndPath(this.profile, this.$stateParams["page"]); |
34 | + }).then((result: noosfero.RestResult<any>) => { | ||
35 | this.article = <noosfero.Article>result.data; | 35 | this.article = <noosfero.Article>result.data; |
36 | }); | 36 | }); |
37 | } | 37 | } |
src/lib/ng-noosfero-api/http/article.service.ts
@@ -44,9 +44,28 @@ export class ArticleService extends RestangularService<noosfero.Article> { | @@ -44,9 +44,28 @@ export class ArticleService extends RestangularService<noosfero.Article> { | ||
44 | return this.list(profileElement, params); | 44 | return this.list(profileElement, params); |
45 | } | 45 | } |
46 | 46 | ||
47 | + getArticleByProfileAndPath(profile: noosfero.Profile, path: string): ng.IPromise<noosfero.RestResult<noosfero.Article>> { | ||
48 | + let deferred = this.$q.defer<noosfero.RestResult<noosfero.Article>>(); | ||
49 | + let profileElement = this.profileService.get(<number>profile.id); | ||
50 | + | ||
51 | + let restRequest: ng.IPromise<any>; | ||
52 | + | ||
53 | + let params = { path: path }; | ||
54 | + | ||
55 | + restRequest = profileElement.customGET(this.getResourcePath(), params); | ||
56 | + | ||
57 | + | ||
58 | + restRequest | ||
59 | + .then(this.getHandleSuccessFunction(deferred)) | ||
60 | + .catch(this.getHandleErrorFunction(deferred)); | ||
61 | + | ||
62 | + | ||
63 | + return deferred.promise; | ||
64 | + } | ||
65 | + | ||
47 | getOneByProfile<T>(profile: noosfero.Profile, params?: any): ng.IPromise<noosfero.RestResult<noosfero.Article>> { | 66 | getOneByProfile<T>(profile: noosfero.Profile, params?: any): ng.IPromise<noosfero.RestResult<noosfero.Article>> { |
48 | let profileElement = this.profileService.get(<number>profile.id); | 67 | let profileElement = this.profileService.get(<number>profile.id); |
49 | - return this.get(profile.id, params); | 68 | + return this.getSub(profileElement, params);; |
50 | } | 69 | } |
51 | 70 | ||
52 | getChildren<T>(article: noosfero.Article, params?: any): ng.IPromise<noosfero.RestResult<noosfero.Article[]>> { | 71 | getChildren<T>(article: noosfero.Article, params?: any): ng.IPromise<noosfero.RestResult<noosfero.Article[]>> { |
src/lib/ng-noosfero-api/http/restangular_service.ts
@@ -125,6 +125,33 @@ export abstract class RestangularService<T extends noosfero.RestModel> { | @@ -125,6 +125,33 @@ export abstract class RestangularService<T extends noosfero.RestModel> { | ||
125 | 125 | ||
126 | return deferred.promise; | 126 | return deferred.promise; |
127 | } | 127 | } |
128 | + | ||
129 | + /** | ||
130 | + * Do a HTTP GET call to the resource collection represented | ||
131 | + * | ||
132 | + * @protected | ||
133 | + * @param {number} id (description) | ||
134 | + * @returns {ng.IPromise<T>} Returns a Promise to the Generic Type | ||
135 | + */ | ||
136 | + public getSub(rootElement?: restangular.IElement, queryParams?: any, headers?: any): ng.IPromise<noosfero.RestResult<T>> { | ||
137 | + let deferred = this.$q.defer<noosfero.RestResult<T>>(); | ||
138 | + | ||
139 | + let restRequest: ng.IPromise<any>; | ||
140 | + | ||
141 | + if (rootElement) { | ||
142 | + restRequest = rootElement.customGET(this.getResourcePath(), queryParams, headers); | ||
143 | + } else { | ||
144 | + restRequest = this.baseResource.customGET("", queryParams, headers); | ||
145 | + } | ||
146 | + | ||
147 | + | ||
148 | + restRequest | ||
149 | + .then(this.getHandleSuccessFunction(deferred)) | ||
150 | + .catch(this.getHandleErrorFunction(deferred)); | ||
151 | + | ||
152 | + | ||
153 | + return deferred.promise; | ||
154 | + } | ||
128 | 155 | ||
129 | public listSubElements<C>(obj: T, subElement: string, queryParams?: any, headers?: any): ng.IPromise<noosfero.RestResult<C>> { | 156 | public listSubElements<C>(obj: T, subElement: string, queryParams?: any, headers?: any): ng.IPromise<noosfero.RestResult<C>> { |
130 | let deferred = this.$q.defer<noosfero.RestResult<C>>(); | 157 | let deferred = this.$q.defer<noosfero.RestResult<C>>(); |
src/spec/mocks.ts
@@ -48,7 +48,7 @@ export var mocks = { | @@ -48,7 +48,7 @@ export var mocks = { | ||
48 | } | 48 | } |
49 | }; | 49 | }; |
50 | }, | 50 | }, |
51 | - getOneByProfile: (profileId: number, params?: any) => { | 51 | + getArticleByProfileAndPath: (profile: noosfero.Profile, path: string) => { |
52 | return { | 52 | return { |
53 | then: (func?: Function) => { | 53 | then: (func?: Function) => { |
54 | if (func) func({ | 54 | if (func) func({ |