Commit 21a269630a5562edd1c88701555fca232f6d9354

Authored by Victor Costa
1 parent b3bfce0b

Filter communities based on the owner in communities block

src/app/layout/blocks/communities-block/communities-block.component.spec.ts
... ... @@ -19,7 +19,7 @@ describe("Components", () => {
19 19 new Provider('$state', { useValue: state }),
20 20 new Provider('CommunityService', {
21 21 useValue: {
22   - list: (profileId: number, params: any): any => {
  22 + getByOwner: (owner: any, params: any): any => {
23 23 return Promise.resolve({ data: [{ identifier: "community1" }] });
24 24 }
25 25 }
... ...
src/app/layout/blocks/communities-block/communities-block.component.ts
... ... @@ -17,8 +17,7 @@ export class CommunitiesBlockComponent {
17 17  
18 18 ngOnInit() {
19 19 let limit: number = ((this.block && this.block.settings) ? this.block.settings.limit : null) || 5;
20   -
21   - this.communityService.list(null, { limit: limit }).then((result: noosfero.RestResult<noosfero.Community[]>) => {
  20 + this.communityService.getByOwner(this.owner, { limit: limit }).then((result: noosfero.RestResult<noosfero.Community[]>) => {
22 21 this.profiles = result.data;
23 22 });
24 23 }
... ...
src/lib/ng-noosfero-api/http/community.service.spec.ts
... ... @@ -19,14 +19,42 @@ describe(&quot;Services&quot;, () =&gt; {
19 19  
20 20 describe("Succesfull requests", () => {
21 21  
22   - it("should list communities", (done) => {
  22 + it("should list environment communities", (done) => {
23 23 $httpBackend.expectGET(`/api/v1/communities`).respond(200, { communities: [{ name: "community1" }] });
24   - communityService.list().then((result: noosfero.RestResult<noosfero.Community[]>) => {
  24 + communityService.getByEnvironment().then((result: noosfero.RestResult<noosfero.Community[]>) => {
25 25 expect(result.data).toEqual([{ name: "community1" }]);
26 26 done();
27 27 });
28 28 $httpBackend.flush();
29 29 });
  30 +
  31 + it("should list person communities", (done) => {
  32 + $httpBackend.expectGET(`/api/v1/people/1/communities`).respond(200, { communities: [{ name: "community1" }] });
  33 + let person = <any>{ id: 1 };
  34 + communityService.getByPerson(person).then((result: noosfero.RestResult<noosfero.Community[]>) => {
  35 + expect(result.data).toEqual([{ name: "community1" }]);
  36 + done();
  37 + });
  38 + $httpBackend.flush();
  39 + });
  40 +
  41 + it("should list owner communities when it is an environment", (done) => {
  42 + $httpBackend.expectGET(`/api/v1/communities`).respond(200, { communities: [{ name: "community1" }] });
  43 + let owner = <any>{ id: 1 };
  44 + communityService.getByOwner(owner).then((result: noosfero.RestResult<noosfero.Community[]>) => {
  45 + done();
  46 + });
  47 + $httpBackend.flush();
  48 + });
  49 +
  50 + it("should list owner communities when it is an person", (done) => {
  51 + $httpBackend.expectGET(`/api/v1/people/1/communities`).respond(200, { communities: [{ name: "community1" }] });
  52 + let owner = <any>{ id: 1, type: "Person" };
  53 + communityService.getByOwner(owner).then((result: noosfero.RestResult<noosfero.Community[]>) => {
  54 + done();
  55 + });
  56 + $httpBackend.flush();
  57 + });
30 58 });
31 59  
32 60 });
... ...
src/lib/ng-noosfero-api/http/community.service.ts
1 1 import { Injectable, Inject } from "ng-forward";
2 2 import {RestangularService} from "./restangular_service";
  3 +import {PersonService} from "./person.service";
3 4  
4 5 @Injectable()
5   -@Inject("Restangular", "$q", "$log")
  6 +@Inject("Restangular", "$q", "$log", PersonService)
6 7 export class CommunityService extends RestangularService<noosfero.Community> {
7 8  
8   - constructor(Restangular: restangular.IService, $q: ng.IQService, $log: ng.ILogService) {
  9 + constructor(Restangular: restangular.IService, $q: ng.IQService, $log: ng.ILogService, protected personService: PersonService) {
9 10 super(Restangular, $q, $log);
10 11 }
11 12  
... ... @@ -20,4 +21,21 @@ export class CommunityService extends RestangularService&lt;noosfero.Community&gt; {
20 21 };
21 22 }
22 23  
  24 + getByOwner(owner: any, params?: any) {
  25 + // TODO see a better way to verify the owner type
  26 + if (owner.type == "Person") {
  27 + return this.getByPerson(owner, params);
  28 + } else {
  29 + return this.getByEnvironment(params);
  30 + }
  31 + }
  32 +
  33 + getByEnvironment(params?: any) {
  34 + return this.list(null, params);
  35 + }
  36 +
  37 + getByPerson(person: noosfero.Person, params?: any) {
  38 + let personElement = this.personService.getElement(person.id);
  39 + return this.list(personElement, params);
  40 + }
23 41 }
... ...
src/lib/ng-noosfero-api/http/person.service.ts 0 → 100644
... ... @@ -0,0 +1,23 @@
  1 +import { Injectable, Inject } from "ng-forward";
  2 +import {RestangularService} from "./restangular_service";
  3 +
  4 +@Injectable()
  5 +@Inject("Restangular", "$q", "$log")
  6 +export class PersonService extends RestangularService<noosfero.Person> {
  7 +
  8 + constructor(Restangular: restangular.IService, $q: ng.IQService, $log: ng.ILogService) {
  9 + super(Restangular, $q, $log);
  10 + }
  11 +
  12 + getResourcePath() {
  13 + return "people";
  14 + }
  15 +
  16 + getDataKeys() {
  17 + return {
  18 + singular: 'person',
  19 + plural: 'people'
  20 + };
  21 + }
  22 +
  23 +}
... ...