diff --git a/src/app/components/noosfero-blocks/recent-documents/recent-documents.component.spec.ts b/src/app/components/noosfero-blocks/recent-documents/recent-documents.component.spec.ts index eadbead..055eb88 100644 --- a/src/app/components/noosfero-blocks/recent-documents/recent-documents.component.spec.ts +++ b/src/app/components/noosfero-blocks/recent-documents/recent-documents.component.spec.ts @@ -18,7 +18,7 @@ describe("Components", () => { new Provider('ArticleService', { useValue: { getByProfile: (profileId: number, filters: any): any => { - return Promise.resolve({ data: { articles: [{ name: "article1" }] } }); + return Promise.resolve({ articles: [{ name: "article1" }] }); } } }), diff --git a/src/app/components/noosfero-blocks/recent-documents/recent-documents.component.ts b/src/app/components/noosfero-blocks/recent-documents/recent-documents.component.ts index 3596a51..dc5ddf5 100644 --- a/src/app/components/noosfero-blocks/recent-documents/recent-documents.component.ts +++ b/src/app/components/noosfero-blocks/recent-documents/recent-documents.component.ts @@ -27,8 +27,8 @@ export class RecentDocumentsBlock { // FIXME get all text articles // FIXME make the getByProfile a generic method where we tell the type passing a class TinyMceArticle // and the promise should be of type TinyMceArticle[], per example - this.articleService.getByProfile(this.profile.id, { content_type: 'TinyMceArticle', per_page: limit }).then((response: any) => { - this.documents = response.data.articles; + this.articleService.getByProfile(this.profile.id, { content_type: 'TinyMceArticle', per_page: limit }).then((result: noosfero.ArticlesResult) => { + this.documents = result.articles; this.documentsLoaded = true; }); } diff --git a/src/app/content-viewer/content-viewer.component.ts b/src/app/content-viewer/content-viewer.component.ts index 4e855d9..528847e 100644 --- a/src/app/content-viewer/content-viewer.component.ts +++ b/src/app/content-viewer/content-viewer.component.ts @@ -1,6 +1,3 @@ -import * as noosfero from "../models/interfaces"; - - import {ArticleView} from "../components/noosfero-articles/article/article_view"; import {Input, Component, StateConfig, Inject, provide} from "ng-forward"; @@ -34,8 +31,8 @@ export class ContentViewer { this.profileService.getCurrentProfile().then((profile: noosfero.Profile) => { this.profile = profile; return this.articleService.getByProfile(this.profile.id, { path: this.$stateParams["page"] }); - }).then((response: restangular.IResponse) => { - this.article = response.data.article; + }).then((result: noosfero.ArticleResult) => { + this.article = result.article; }); } } diff --git a/src/lib/ng-noosfero-api/http/article.service.spec.ts b/src/lib/ng-noosfero-api/http/article.service.spec.ts index dd51475..91c0067 100644 --- a/src/lib/ng-noosfero-api/http/article.service.spec.ts +++ b/src/lib/ng-noosfero-api/http/article.service.spec.ts @@ -64,8 +64,8 @@ describe("Services", () => { it("should create an article in a profile", (done) => { let profileId = 1; let article: noosfero.Article = { id: null}; - $httpBackend.expectPOST(`/api/v1/profiles/${profileId}/articles`, { article: article }).respond(200, { id: 2 }); - articleService.create(profileId, article).then((result: noosfero.Article) => { + $httpBackend.expectPOST(`/api/v1/profiles/${profileId}/articles`, { article: article }).respond(200, {article: { id: 2 }}); + articleService.create(profileId, article).then((article: noosfero.Article) => { expect(article).toEqual({ id: 2 }); done(); }); diff --git a/src/lib/ng-noosfero-api/http/article.service.ts b/src/lib/ng-noosfero-api/http/article.service.ts index 0911b41..defdb75 100644 --- a/src/lib/ng-noosfero-api/http/article.service.ts +++ b/src/lib/ng-noosfero-api/http/article.service.ts @@ -17,11 +17,11 @@ export class ArticleService extends RestangularWrapperService return { singular: 'article', plural: 'articles' - } + }; } create(profileId: number, article: noosfero.Article): ng.IPromise { - return this.post(this.Restangular.one('profiles', profileId), article); + return this.post(this.Restangular.one('profiles', profileId), article); } // // TODO create a handle ErrorFactory too and move handleSuccessFactory and handleErrorFactory diff --git a/src/lib/ng-noosfero-api/http/restangular_wrapper_service.ts b/src/lib/ng-noosfero-api/http/restangular_wrapper_service.ts index 8866a50..a5c3b91 100644 --- a/src/lib/ng-noosfero-api/http/restangular_wrapper_service.ts +++ b/src/lib/ng-noosfero-api/http/restangular_wrapper_service.ts @@ -13,16 +13,19 @@ export abstract class RestangularWrapperService { return this.Restangular.one(this.getPath(), id); } - protected post(elementRoot: restangular.IElement, element?: any, path?: string, params?: any, headers?: any): ng.IPromise { + protected post(elementRoot: restangular.IElement, element?: any, path?: string, params?: any, headers?: any): ng.IPromise { let deferred = this.$q.defer(); + let postData = {}; + postData[this.getDataKeys().singular] = element; + this.customPOST( elementRoot, - element, + postData, this.getPath(), {} ) - .then(this.getHandleSuccessFunction(deferred)) + .then(this.getPostSuccessHandleFunction(deferred)) .catch(this.getHandleErrorFunction(deferred)); return deferred.promise; @@ -67,6 +70,23 @@ export abstract class RestangularWrapperService { return successFunction; } + getPostSuccessHandleFunction(deferred: ng.IDeferred): (response: restangular.IResponse) => void { + let self = this; + let successFunction = (response: restangular.IResponse): void => { + if (self.$log) { + self.$log.debug("Post successfully executed", self, response); + } + let data = response.data; + + if ((data).hasOwnProperty(self.getDataKeys().singular)) { + deferred.resolve(data[self.getDataKeys().singular]); + } else { + deferred.resolve(data); + } + }; + return successFunction; + } + getHandleErrorFunction(deferred: ng.IDeferred): (response: restangular.IResponse) => void { let self = this; let successFunction = (response: restangular.IResponse): void => { -- libgit2 0.21.2