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 f15e208..3596a51 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 @@ -23,8 +23,10 @@ export class RecentDocumentsBlock { this.profile = this.owner; this.documents = []; - var limit = (this.block && this.block.settings) ? this.block.settings.limit : null || 5; - //FIXME get all text articles + let limit = (this.block && this.block.settings) ? this.block.settings.limit : null || 5; + // 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.documentsLoaded = true; diff --git a/src/lib/ng-noosfero-api/http/article.service.ts b/src/lib/ng-noosfero-api/http/article.service.ts index 8b78098..992431e 100644 --- a/src/lib/ng-noosfero-api/http/article.service.ts +++ b/src/lib/ng-noosfero-api/http/article.service.ts @@ -2,10 +2,12 @@ import { Injectable, Inject } from "ng-forward"; import {Article} from "../../../app/models/interfaces"; @Injectable() -@Inject("Restangular") +@Inject("Restangular", "$q") + + export class ArticleService { - constructor(private Restangular: any) { } + 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( @@ -16,8 +18,23 @@ export class ArticleService { ); } - getByProfile(profileId: number, params?: any) { - return this.Restangular.one('profiles', profileId).customGET('articles', params); + // 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(deferred: ng.IDeferred): (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(profileId: number, params?: any): ng.IPromise { + let deferred = this.$q.defer(); + this.Restangular.one('profiles', profileId).customGET('articles', params).then(this.handleSuccessFactory(deferred)); + return deferred.promise; } getChildren(articleId: number, params?: any) { diff --git a/src/lib/ng-noosfero-api/interfaces/article.ts b/src/lib/ng-noosfero-api/interfaces/article.ts new file mode 100644 index 0000000..651d9da --- /dev/null +++ b/src/lib/ng-noosfero-api/interfaces/article.ts @@ -0,0 +1,6 @@ + +namespace noosfero { + interface Article { + id: number; + } +} \ No newline at end of file diff --git a/src/lib/ng-noosfero-api/interfaces/event.ts b/src/lib/ng-noosfero-api/interfaces/event.ts new file mode 100644 index 0000000..434caff --- /dev/null +++ b/src/lib/ng-noosfero-api/interfaces/event.ts @@ -0,0 +1,5 @@ +namespace noosfero { + interface Event extends Article { + id: number; + } +} diff --git a/src/lib/ng-noosfero-api/interfaces/person.ts b/src/lib/ng-noosfero-api/interfaces/person.ts new file mode 100644 index 0000000..049cf2a --- /dev/null +++ b/src/lib/ng-noosfero-api/interfaces/person.ts @@ -0,0 +1,5 @@ +namespace noosfero { + export interface Person extends Profile { + id: number; + } +} \ No newline at end of file diff --git a/src/lib/ng-noosfero-api/interfaces/profile.ts b/src/lib/ng-noosfero-api/interfaces/profile.ts new file mode 100644 index 0000000..f0fa1de --- /dev/null +++ b/src/lib/ng-noosfero-api/interfaces/profile.ts @@ -0,0 +1,8 @@ + +namespace noosfero { + export interface Profile { + id: number; + identifier: string; + type: string; + } +} \ No newline at end of file -- libgit2 0.21.2