From 21a269630a5562edd1c88701555fca232f6d9354 Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Mon, 28 Mar 2016 10:52:02 -0300 Subject: [PATCH] Filter communities based on the owner in communities block --- src/app/layout/blocks/communities-block/communities-block.component.spec.ts | 2 +- src/app/layout/blocks/communities-block/communities-block.component.ts | 3 +-- src/lib/ng-noosfero-api/http/community.service.spec.ts | 32 ++++++++++++++++++++++++++++++-- src/lib/ng-noosfero-api/http/community.service.ts | 22 ++++++++++++++++++++-- src/lib/ng-noosfero-api/http/person.service.ts | 23 +++++++++++++++++++++++ 5 files changed, 75 insertions(+), 7 deletions(-) create mode 100644 src/lib/ng-noosfero-api/http/person.service.ts diff --git a/src/app/layout/blocks/communities-block/communities-block.component.spec.ts b/src/app/layout/blocks/communities-block/communities-block.component.spec.ts index f81581d..bc6eafd 100644 --- a/src/app/layout/blocks/communities-block/communities-block.component.spec.ts +++ b/src/app/layout/blocks/communities-block/communities-block.component.spec.ts @@ -19,7 +19,7 @@ describe("Components", () => { new Provider('$state', { useValue: state }), new Provider('CommunityService', { useValue: { - list: (profileId: number, params: any): any => { + getByOwner: (owner: any, params: any): any => { return Promise.resolve({ data: [{ identifier: "community1" }] }); } } diff --git a/src/app/layout/blocks/communities-block/communities-block.component.ts b/src/app/layout/blocks/communities-block/communities-block.component.ts index 9dfab2f..a59f3ad 100644 --- a/src/app/layout/blocks/communities-block/communities-block.component.ts +++ b/src/app/layout/blocks/communities-block/communities-block.component.ts @@ -17,8 +17,7 @@ export class CommunitiesBlockComponent { ngOnInit() { let limit: number = ((this.block && this.block.settings) ? this.block.settings.limit : null) || 5; - - this.communityService.list(null, { limit: limit }).then((result: noosfero.RestResult) => { + this.communityService.getByOwner(this.owner, { limit: limit }).then((result: noosfero.RestResult) => { this.profiles = result.data; }); } diff --git a/src/lib/ng-noosfero-api/http/community.service.spec.ts b/src/lib/ng-noosfero-api/http/community.service.spec.ts index 6406ce5..c35ada8 100644 --- a/src/lib/ng-noosfero-api/http/community.service.spec.ts +++ b/src/lib/ng-noosfero-api/http/community.service.spec.ts @@ -19,14 +19,42 @@ describe("Services", () => { describe("Succesfull requests", () => { - it("should list communities", (done) => { + it("should list environment communities", (done) => { $httpBackend.expectGET(`/api/v1/communities`).respond(200, { communities: [{ name: "community1" }] }); - communityService.list().then((result: noosfero.RestResult) => { + communityService.getByEnvironment().then((result: noosfero.RestResult) => { expect(result.data).toEqual([{ name: "community1" }]); done(); }); $httpBackend.flush(); }); + + it("should list person communities", (done) => { + $httpBackend.expectGET(`/api/v1/people/1/communities`).respond(200, { communities: [{ name: "community1" }] }); + let person = { id: 1 }; + communityService.getByPerson(person).then((result: noosfero.RestResult) => { + expect(result.data).toEqual([{ name: "community1" }]); + done(); + }); + $httpBackend.flush(); + }); + + it("should list owner communities when it is an environment", (done) => { + $httpBackend.expectGET(`/api/v1/communities`).respond(200, { communities: [{ name: "community1" }] }); + let owner = { id: 1 }; + communityService.getByOwner(owner).then((result: noosfero.RestResult) => { + done(); + }); + $httpBackend.flush(); + }); + + it("should list owner communities when it is an person", (done) => { + $httpBackend.expectGET(`/api/v1/people/1/communities`).respond(200, { communities: [{ name: "community1" }] }); + let owner = { id: 1, type: "Person" }; + communityService.getByOwner(owner).then((result: noosfero.RestResult) => { + done(); + }); + $httpBackend.flush(); + }); }); }); diff --git a/src/lib/ng-noosfero-api/http/community.service.ts b/src/lib/ng-noosfero-api/http/community.service.ts index 987adaa..925e9df 100644 --- a/src/lib/ng-noosfero-api/http/community.service.ts +++ b/src/lib/ng-noosfero-api/http/community.service.ts @@ -1,11 +1,12 @@ import { Injectable, Inject } from "ng-forward"; import {RestangularService} from "./restangular_service"; +import {PersonService} from "./person.service"; @Injectable() -@Inject("Restangular", "$q", "$log") +@Inject("Restangular", "$q", "$log", PersonService) export class CommunityService extends RestangularService { - constructor(Restangular: restangular.IService, $q: ng.IQService, $log: ng.ILogService) { + constructor(Restangular: restangular.IService, $q: ng.IQService, $log: ng.ILogService, protected personService: PersonService) { super(Restangular, $q, $log); } @@ -20,4 +21,21 @@ export class CommunityService extends RestangularService { }; } + getByOwner(owner: any, params?: any) { + // TODO see a better way to verify the owner type + if (owner.type == "Person") { + return this.getByPerson(owner, params); + } else { + return this.getByEnvironment(params); + } + } + + getByEnvironment(params?: any) { + return this.list(null, params); + } + + getByPerson(person: noosfero.Person, params?: any) { + let personElement = this.personService.getElement(person.id); + return this.list(personElement, params); + } } diff --git a/src/lib/ng-noosfero-api/http/person.service.ts b/src/lib/ng-noosfero-api/http/person.service.ts new file mode 100644 index 0000000..a7f9b3d --- /dev/null +++ b/src/lib/ng-noosfero-api/http/person.service.ts @@ -0,0 +1,23 @@ +import { Injectable, Inject } from "ng-forward"; +import {RestangularService} from "./restangular_service"; + +@Injectable() +@Inject("Restangular", "$q", "$log") +export class PersonService extends RestangularService { + + constructor(Restangular: restangular.IService, $q: ng.IQService, $log: ng.ILogService) { + super(Restangular, $q, $log); + } + + getResourcePath() { + return "people"; + } + + getDataKeys() { + return { + singular: 'person', + plural: 'people' + }; + } + +} -- libgit2 0.21.2