Commit e20ec7003e65b24c3ee197622e7d172ab4865440

Authored by ABNER SILVA DE OLIVEIRA
1 parent 9e3a58f8
Exists in master and in 1 other branch dev-fixes

refactory of code which are calling article service

src/app/components/noosfero-blocks/recent-documents/recent-documents.component.spec.ts
... ... @@ -18,7 +18,7 @@ describe("Components", () => {
18 18 new Provider('ArticleService', {
19 19 useValue: {
20 20 getByProfile: (profileId: number, filters: any): any => {
21   - return Promise.resolve({ data: { articles: [{ name: "article1" }] } });
  21 + return Promise.resolve({ articles: [{ name: "article1" }] });
22 22 }
23 23 }
24 24 }),
... ...
src/app/components/noosfero-blocks/recent-documents/recent-documents.component.ts
... ... @@ -27,8 +27,8 @@ export class RecentDocumentsBlock {
27 27 // FIXME get all text articles
28 28 // FIXME make the getByProfile a generic method where we tell the type passing a class TinyMceArticle
29 29 // and the promise should be of type TinyMceArticle[], per example
30   - this.articleService.getByProfile(this.profile.id, { content_type: 'TinyMceArticle', per_page: limit }).then((response: any) => {
31   - this.documents = response.data.articles;
  30 + this.articleService.getByProfile(this.profile.id, { content_type: 'TinyMceArticle', per_page: limit }).then((result: noosfero.ArticlesResult) => {
  31 + this.documents = result.articles;
32 32 this.documentsLoaded = true;
33 33 });
34 34 }
... ...
src/app/content-viewer/content-viewer.component.ts
1   -import * as noosfero from "../models/interfaces";
2   -
3   -
4 1 import {ArticleView} from "../components/noosfero-articles/article/article_view";
5 2 import {Input, Component, StateConfig, Inject, provide} from "ng-forward";
6 3  
... ... @@ -34,8 +31,8 @@ export class ContentViewer {
34 31 this.profileService.getCurrentProfile().then((profile: noosfero.Profile) => {
35 32 this.profile = profile;
36 33 return this.articleService.getByProfile(this.profile.id, { path: this.$stateParams["page"] });
37   - }).then((response: restangular.IResponse) => {
38   - this.article = response.data.article;
  34 + }).then((result: noosfero.ArticleResult) => {
  35 + this.article = result.article;
39 36 });
40 37 }
41 38 }
... ...
src/lib/ng-noosfero-api/http/article.service.spec.ts
... ... @@ -64,8 +64,8 @@ describe("Services", () => {
64 64 it("should create an article in a profile", (done) => {
65 65 let profileId = 1;
66 66 let article: noosfero.Article = <any>{ id: null};
67   - $httpBackend.expectPOST(`/api/v1/profiles/${profileId}/articles`, { article: article }).respond(200, { id: 2 });
68   - articleService.create(profileId, article).then((result: noosfero.Article) => {
  67 + $httpBackend.expectPOST(`/api/v1/profiles/${profileId}/articles`, { article: article }).respond(200, {article: { id: 2 }});
  68 + articleService.create(profileId, article).then((article: noosfero.Article) => {
69 69 expect(article).toEqual({ id: 2 });
70 70 done();
71 71 });
... ...
src/lib/ng-noosfero-api/http/article.service.ts
... ... @@ -17,11 +17,11 @@ export class ArticleService extends RestangularWrapperService&lt;noosfero.Article&gt;
17 17 return {
18 18 singular: 'article',
19 19 plural: 'articles'
20   - }
  20 + };
21 21 }
22 22  
23 23 create(profileId: number, article: noosfero.Article): ng.IPromise<noosfero.Article> {
24   - return this.post(this.Restangular.one('profiles', profileId), article);
  24 + return this.post<noosfero.Article>(this.Restangular.one('profiles', profileId), article);
25 25 }
26 26  
27 27 // // TODO create a handle ErrorFactory too and move handleSuccessFactory and handleErrorFactory
... ...
src/lib/ng-noosfero-api/http/restangular_wrapper_service.ts
... ... @@ -13,16 +13,19 @@ export abstract class RestangularWrapperService&lt;T&gt; {
13 13 return this.Restangular.one(this.getPath(), id);
14 14 }
15 15  
16   - protected post(elementRoot: restangular.IElement, element?: any, path?: string, params?: any, headers?: any): ng.IPromise<T> {
  16 + protected post<T>(elementRoot: restangular.IElement, element?: any, path?: string, params?: any, headers?: any): ng.IPromise<T> {
17 17 let deferred = this.$q.defer<T>();
18 18  
  19 + let postData = <any>{};
  20 + postData[this.getDataKeys().singular] = element;
  21 +
19 22 this.customPOST(
20 23 elementRoot,
21   - element,
  24 + postData,
22 25 this.getPath(),
23 26 {}
24 27 )
25   - .then(this.getHandleSuccessFunction(deferred))
  28 + .then(this.getPostSuccessHandleFunction(deferred))
26 29 .catch(this.getHandleErrorFunction(deferred));
27 30  
28 31 return deferred.promise;
... ... @@ -67,6 +70,23 @@ export abstract class RestangularWrapperService&lt;T&gt; {
67 70 return successFunction;
68 71 }
69 72  
  73 + getPostSuccessHandleFunction<T>(deferred: ng.IDeferred<T>): (response: restangular.IResponse) => void {
  74 + let self = this;
  75 + let successFunction = (response: restangular.IResponse): void => {
  76 + if (self.$log) {
  77 + self.$log.debug("Post successfully executed", self, response);
  78 + }
  79 + let data = response.data;
  80 +
  81 + if ((<Object>data).hasOwnProperty(self.getDataKeys().singular)) {
  82 + deferred.resolve(data[self.getDataKeys().singular]);
  83 + } else {
  84 + deferred.resolve(data);
  85 + }
  86 + };
  87 + return successFunction;
  88 + }
  89 +
70 90 getHandleErrorFunction<T>(deferred: ng.IDeferred<T>): (response: restangular.IResponse) => void {
71 91 let self = this;
72 92 let successFunction = (response: restangular.IResponse): void => {
... ...