Commit e6bb091b74ef2dee9130a87555e8029bed4ed09e

Authored by Leandro Santos
1 parent b471f0f7

use profile service instead of people service to upload image

src/app/profile/image/image.component.spec.ts
... ... @@ -7,7 +7,7 @@
7 7 import { ComponentTestHelper, createClass } from '../../../spec/component-test-helper';
8 8 import { TestComponentBuilder, ComponentFixture } from 'ng-forward/cjs/testing/test-component-builder';
9 9 import { Pipe, Input, provide, Component } from 'ng-forward';
10   -import { PersonService } from "../../../lib/ng-noosfero-api/http/person.service";
  10 +import { ProfileService } from "../../../lib/ng-noosfero-api/http/profile.service";
11 11  
12 12 import * as helpers from "../../../spec/helpers";
13 13  
... ... @@ -25,14 +25,14 @@ describe("Components", () => {
25 25  
26 26 beforeEach((done) => {
27 27 let scope = helpers.mocks.scopeWithEvents;
28   - let personService = jasmine.createSpyObj("personService", ["upload"]);
  28 + let profileService = jasmine.createSpyObj("profileService", ["upload"]);
29 29 let properties = { profile: { custom_footer: "footer" } };
30 30 let cls = createClass({
31 31 template: htmlTemplate,
32 32 directives: [ProfileImageComponent],
33 33 properties: properties,
34 34 providers: [
35   - helpers.createProviderToValue("PersonService", personService),
  35 + helpers.createProviderToValue("ProfileService", profileService),
36 36 helpers.createProviderToValue("$uibModal", helpers.mocks.$modal),
37 37 helpers.createProviderToValue("$scope", scope)
38 38 ]
... ...
src/app/profile/image/image.component.ts
1 1 import { Inject, Input, Component, provide } from "ng-forward";
2   -import { PersonService } from "../../../lib/ng-noosfero-api/http/person.service";
  2 +import { ProfileService } from "../../../lib/ng-noosfero-api/http/profile.service";
3 3 import { PermissionService } from "../../shared/services/permission.service";
4 4 import { ProfileImageEditorComponent } from "./profile-image-editor.component";
5 5  
... ... @@ -12,11 +12,10 @@ import { ProfileImageEditorComponent } from "./profile-image-editor.component";
12 12 @Component({
13 13 selector: "noosfero-profile-image",
14 14 templateUrl: 'app/profile/image/image.html',
15   - providers: [provide('personService', { useClass: PersonService })]
  15 + providers: [provide('profileService', { useClass: ProfileService })]
16 16 })
17   -@Inject(PersonService, PermissionService, "$uibModal", "$scope")
  17 +@Inject(ProfileService, PermissionService, "$uibModal", "$scope")
18 18 export class ProfileImageComponent {
19   -
20 19 /**
21 20 * @ngdoc property
22 21 * @name profile
... ... @@ -42,8 +41,7 @@ export class ProfileImageComponent {
42 41 croppedDataUrl: any;
43 42 modalInstance: any;
44 43  
45   - constructor(private personService: PersonService, private permissionService: PermissionService, private $uibModal: ng.ui.bootstrap.IModalService, private $scope: ng.IScope) {
46   - console.log('wwwwwwwwwwwwwwwwwwwwwwww', this.editable);
  44 + constructor(private profileService: ProfileService, private permissionService: PermissionService, private $uibModal: ng.ui.bootstrap.IModalService, private $scope: ng.IScope) {
47 45 }
48 46  
49 47 fileSelected(file: any, errFiles: any) {
... ... @@ -59,7 +57,7 @@ export class ProfileImageComponent {
59 57 resolve: {
60 58 picFile: this.picFile,
61 59 profile: this.profile,
62   - personService: this.personService
  60 + profileService: this.profileService
63 61 }
64 62 });
65 63 }
... ... @@ -79,7 +77,6 @@ export class ProfileImageComponent {
79 77 return this.editable && this.permissionService.isAllowed(this.profile, 'allow_edit');
80 78 }
81 79  
82   -
83 80 /**
84 81 * @ngdoc method
85 82 * @name ngOnInit
... ...
src/app/profile/image/profile-image-editor.component.spec.ts
... ... @@ -18,7 +18,7 @@ describe("Components", () => {
18 18 let modalInstance = jasmine.createSpyObj("$uibModalInstance", ["close"]);
19 19 let picFile = { type: "png" };
20 20 let $q: ng.IQService;
21   - let personServiceMock: any;
  21 + let profileServiceMock: any;
22 22 let $rootScope: ng.IRootScopeService;
23 23  
24 24 beforeEach(inject((_$q_: ng.IQService, _$rootScope_: ng.IRootScopeService) => {
... ... @@ -26,7 +26,7 @@ describe("Components", () => {
26 26 $rootScope = _$rootScope_;
27 27 }));
28 28  
29   - let comp = new ProfileImageEditorComponent(picFile, this.profile, personServiceMock, modalInstance);
  29 + let comp = new ProfileImageEditorComponent(picFile, this.profile, profileServiceMock, modalInstance);
30 30  
31 31 it("get data", done => {
32 32  
... ... @@ -46,10 +46,10 @@ describe("Components", () => {
46 46  
47 47 it("upload image", done => {
48 48 let imageName = "image1";
49   - personServiceMock = jasmine.createSpyObj("personServiceMock", ["uploadImage"]);
  49 + profileServiceMock = jasmine.createSpyObj("profileServiceMock", ["uploadImage"]);
50 50 let deferredUploadImage = $q.defer();
51   - personServiceMock.uploadImage = jasmine.createSpy('uploadImage').and.returnValue(deferredUploadImage.promise);
52   - comp.personService = personServiceMock;
  51 + profileServiceMock.uploadImage = jasmine.createSpy('uploadImage').and.returnValue(deferredUploadImage.promise);
  52 + comp.profileService = profileServiceMock;
53 53 comp.uploadImage(testDataUrl, imageName);
54 54 deferredUploadImage.resolve();
55 55 $rootScope.$apply();
... ...
src/app/profile/image/profile-image-editor.component.ts
1 1 import { StateConfig, Component, Input, Output, Inject, provide } from 'ng-forward';
2 2 import { TranslateProfile } from "../../shared/pipes/translate-profile.filter";
3   -import { PersonService } from "../../../lib/ng-noosfero-api/http/person.service";
  3 +import { ProfileService } from "../../../lib/ng-noosfero-api/http/profile.service";
4 4  
5 5 export class ProfileImageEditorComponent {
6 6  
7 7 croppedDataUrl: string;
8   - static $inject = ["picFile", "profile", "personService", "$uibModalInstance"];
  8 + static $inject = ["picFile", "profile", "profileService", "$uibModalInstance"];
9 9  
10   - constructor(public picFile: any, public profile: noosfero.Profile, public personService: PersonService,
  10 + constructor(public picFile: any, public profile: noosfero.Profile, public profileService: ProfileService,
11 11 public modalInstance: ng.ui.bootstrap.IModalServiceInstance) {
12 12 }
13   -
  13 +
14 14 uploadImage(dataUrl: any, name: any) {
15 15 let base64ImageJson = this.getBase64ImageJson(dataUrl, name);
16   - this.personService.uploadImage(this.profile, base64ImageJson).then((result: any) => {
  16 + this.profileService.uploadImage(this.profile, base64ImageJson).then((result: any) => {
17 17 this.modalInstance.close(name);
18 18 });
19 19 }
... ...
src/app/shared/services/permission.service.ts
... ... @@ -5,7 +5,7 @@ type WithPermissions = noosfero.Profile | noosfero.Comment | noosfero.Article;
5 5 @Injectable()
6 6 export class PermissionService {
7 7 isAllowed(target: WithPermissions, permission: string) {
8   - return target.permissions.indexOf(permission) >= 0;
  8 + return (target.permissions || []).indexOf(permission) >= 0;
9 9 }
10 10  
11 11 }
... ...
src/lib/ng-noosfero-api/http/article.service.ts
... ... @@ -58,7 +58,7 @@ export class ArticleService extends RestangularService<noosfero.Article> {
58 58 }
59 59  
60 60 createInProfile(profile: noosfero.Profile, article: noosfero.Article): ng.IPromise<noosfero.RestResult<noosfero.Article>> {
61   - let profileElement = this.profileService.get(<number>profile.id);
  61 + let profileElement = this.profileService.getProfileElement(<number>profile.id);
62 62 (<any>profileElement).id = profile.id;
63 63 let headers = {
64 64 'Content-Type': 'application/json'
... ... @@ -84,13 +84,13 @@ export class ArticleService extends RestangularService&lt;noosfero.Article&gt; {
84 84 }
85 85  
86 86 getByProfile<T>(profile: noosfero.Profile, params?: any): ng.IPromise<noosfero.RestResult<noosfero.Article[]>> {
87   - let profileElement = this.profileService.get(<number>profile.id);
  87 + let profileElement = this.profileService.getProfileElement(<number>profile.id);
88 88 return this.list(profileElement, params);
89 89 }
90 90  
91 91 getArticleByProfileAndPath(profile: noosfero.Profile, path: string): ng.IPromise<noosfero.RestResult<noosfero.Article>> {
92 92 let deferred = this.$q.defer<noosfero.RestResult<noosfero.Article>>();
93   - let profileElement = this.profileService.get(<number>profile.id);
  93 + let profileElement = this.profileService.getProfileElement(<number>profile.id);
94 94  
95 95 let restRequest: ng.IPromise<any>;
96 96  
... ... @@ -108,7 +108,7 @@ export class ArticleService extends RestangularService&lt;noosfero.Article&gt; {
108 108 }
109 109  
110 110 getOneByProfile<T>(profile: noosfero.Profile, params?: any): ng.IPromise<noosfero.RestResult<noosfero.Article>> {
111   - let profileElement = this.profileService.get(<number>profile.id);
  111 + let profileElement = this.profileService.getProfileElement(<number>profile.id);
112 112 return this.getSub(profileElement, params);
113 113 }
114 114  
... ...
src/lib/ng-noosfero-api/http/profile.service.ts
1 1 import { Injectable, Inject } from "ng-forward";
  2 +import { RestangularService } from "./restangular_service";
  3 +
2 4  
3 5 @Injectable()
4 6 @Inject("Restangular", "$q")
5   -export class ProfileService {
  7 +export class ProfileService extends RestangularService<noosfero.Profile> {
6 8  
7 9 private _currentProfilePromise: ng.IDeferred<noosfero.Profile>;
8 10  
9   - constructor(private restangular: restangular.IService, private $q: ng.IQService) {
  11 + constructor(private restangular: restangular.IService, $q: ng.IQService, $log: ng.ILogService) {
  12 + super(restangular, $q, $log);
10 13 this.resetCurrentProfile();
11 14 }
12 15  
  16 + getResourcePath() {
  17 + return "profiles";
  18 + }
  19 +
  20 + getDataKeys() {
  21 + return {
  22 + singular: 'profile',
  23 + plural: 'profiles'
  24 + };
  25 + }
  26 +
13 27 resetCurrentProfile() {
14 28 this._currentProfilePromise = this.$q.defer();
15 29 }
... ... @@ -31,7 +45,7 @@ export class ProfileService {
31 45 }
32 46  
33 47 getHomePage(profileId: number, params?: any) {
34   - return this.get(profileId).customGET("home_page", params);
  48 + return this.getProfileElement(profileId).customGET("home_page", params);
35 49 }
36 50  
37 51 getByIdentifier(identifier: string): ng.IPromise<noosfero.Profile> {
... ... @@ -45,28 +59,28 @@ export class ProfileService {
45 59 }
46 60  
47 61 getProfileMembers(profileId: number, params?: any): restangular.IPromise<any> {
48   - return this.get(profileId).customGET("members", params);
  62 + return this.getProfileElement(profileId).customGET("members", params);
49 63 }
50 64  
51 65 getBoxes(profileId: number): restangular.IPromise<any> {
52   - return this.get(profileId).customGET('boxes');
  66 + return this.getProfileElement(profileId).customGET('boxes');
53 67 }
54 68  
55 69 getActivities(profileId: number, params?: any): restangular.IPromise<any> {
56   - return this.get(profileId).customGET("activities", params);
  70 + return this.getProfileElement(profileId).customGET("activities", params);
57 71 }
58 72  
59   - get(profileId: number): restangular.IElement {
  73 + getProfileElement(profileId: number): restangular.IElement {
60 74 return this.restangular.one('profiles', profileId);
61 75 }
62 76  
63 77 update(profile: noosfero.Profile) {
64 78 let headers = { 'Content-Type': 'application/json' };
65   - return this.get(profile.id).customPOST({ profile: profile }, null, null, headers);
  79 + return this.getProfileElement(profile.id).customPOST({ profile: profile }, null, null, headers);
66 80 }
67 81  
68 82 getMembers(profile: noosfero.Profile, params?: any) {
69   - let p = this.get(profile.id);
  83 + let p = this.getProfileElement(profile.id);
70 84 return p.customGET('members', params);
71 85 }
72 86  
... ... @@ -83,10 +97,24 @@ export class ProfileService {
83 97 }
84 98  
85 99 addMember(person: noosfero.Person, profile: noosfero.Profile) {
86   - return this.get(profile.id).customPOST({}, "members", null, null);
  100 + return this.getProfileElement(profile.id).customPOST({}, "members", null, null);
87 101 }
88 102  
89 103 removeMember(person: noosfero.Person, profile: noosfero.Profile) {
90   - return this.get(profile.id).customDELETE("members", null, null);
  104 + return this.getProfileElement(profile.id).customDELETE("members", null, null);
  105 + }
  106 +
  107 + uploadImage(profile: noosfero.Profile, base64ImageJson: any) {
  108 + let headers = { 'Content-Type': 'application/json' };
  109 + let deferred = this.$q.defer<noosfero.RestResult<noosfero.Profile>>();
  110 + // TODO dynamically copy the selected attributes to update
  111 + let attributesToUpdate: any = {
  112 + profile: { image_builder: base64ImageJson }
  113 + };
  114 + let restRequest: ng.IPromise<noosfero.RestResult<any>> =
  115 + this.getProfileElement(profile.id).customPOST(attributesToUpdate, null, null, headers);
  116 + restRequest.then(this.getHandleSuccessFunction(deferred))
  117 + .catch(this.getHandleErrorFunction(deferred));
  118 + return deferred.promise;
91 119 }
92 120 }
... ...