Commit eef070ef21a687158d935868841b010ed2d1e2e5

Authored by Ábner Oliveira
1 parent be5b00c2
Exists in master and in 1 other branch dev-fixes

Cleaning the House Task - first commit

src/app/components/noosfero-blocks/recent-documents/recent-documents.component.ts
... ... @@ -23,8 +23,10 @@ export class RecentDocumentsBlock {
23 23 this.profile = this.owner;
24 24 this.documents = [];
25 25  
26   - var limit = (this.block && this.block.settings) ? this.block.settings.limit : null || 5;
27   - //FIXME get all text articles
  26 + let limit = (this.block && this.block.settings) ? this.block.settings.limit : null || 5;
  27 + // FIXME get all text articles
  28 + // FIXME make the getByProfile a generic method where we tell the type passing a class TinyMceArticle
  29 + // and the promise should be of type TinyMceArticle[], per example
28 30 this.articleService.getByProfile(this.profile.id, { content_type: 'TinyMceArticle', per_page: limit }).then((response: any) => {
29 31 this.documents = response.data.articles;
30 32 this.documentsLoaded = true;
... ...
src/lib/ng-noosfero-api/http/article.service.ts
... ... @@ -2,10 +2,12 @@ import { Injectable, Inject } from "ng-forward";
2 2 import {Article} from "../../../app/models/interfaces";
3 3  
4 4 @Injectable()
5   -@Inject("Restangular")
  5 +@Inject("Restangular", "$q")
  6 +
  7 +
6 8 export class ArticleService {
7 9  
8   - constructor(private Restangular: any) { }
  10 + constructor(private Restangular: restangular.IService, private $q: ng.IQService, private $log: ng.ILogService) { }
9 11  
10 12 create(profileId: number, article: Article) {
11 13 return this.Restangular.one('profiles', profileId).customPOST(
... ... @@ -16,8 +18,23 @@ export class ArticleService {
16 18 );
17 19 }
18 20  
19   - getByProfile(profileId: number, params?: any) {
20   - return this.Restangular.one('profiles', profileId).customGET('articles', params);
  21 + // TODO create a handle ErrorFactory too and move handleSuccessFactory and handleErrorFactory
  22 + // to a base class (of course we will have to creates a base class too)
  23 + handleSuccessFactory<T>(deferred: ng.IDeferred<T>): (response: restangular.IResponse) => void {
  24 + let self = this;
  25 + let successFunction = (response: restangular.IResponse): void => {
  26 + this.$log.debug("Request successfull executed", this, response);
  27 + deferred.resolve(response.data);
  28 + };
  29 + return successFunction;
  30 + }
  31 +
  32 + // TODO -> change all Restangular services to this approach "Return promise to a specific type"
  33 + // it makes easy consume the service
  34 + getByProfile<T>(profileId: number, params?: any): ng.IPromise<T> {
  35 + let deferred = this.$q.defer<T>();
  36 + this.Restangular.one('profiles', profileId).customGET('articles', params).then(this.handleSuccessFactory(deferred));
  37 + return deferred.promise;
21 38 }
22 39  
23 40 getChildren(articleId: number, params?: any) {
... ...
src/lib/ng-noosfero-api/interfaces/article.ts 0 → 100644
... ... @@ -0,0 +1,6 @@
  1 +
  2 +namespace noosfero {
  3 + interface Article {
  4 + id: number;
  5 + }
  6 +}
0 7 \ No newline at end of file
... ...
src/lib/ng-noosfero-api/interfaces/event.ts 0 → 100644
... ... @@ -0,0 +1,5 @@
  1 +namespace noosfero {
  2 + interface Event extends Article {
  3 + id: number;
  4 + }
  5 +}
... ...
src/lib/ng-noosfero-api/interfaces/person.ts 0 → 100644
... ... @@ -0,0 +1,5 @@
  1 +namespace noosfero {
  2 + export interface Person extends Profile {
  3 + id: number;
  4 + }
  5 +}
0 6 \ No newline at end of file
... ...
src/lib/ng-noosfero-api/interfaces/profile.ts 0 → 100644
... ... @@ -0,0 +1,8 @@
  1 +
  2 +namespace noosfero {
  3 + export interface Profile {
  4 + id: number;
  5 + identifier: string;
  6 + type: string;
  7 + }
  8 +}
0 9 \ No newline at end of file
... ...