From e6bb091b74ef2dee9130a87555e8029bed4ed09e Mon Sep 17 00:00:00 2001 From: Leandro Nunes dos Santos Date: Thu, 4 Aug 2016 14:00:00 -0300 Subject: [PATCH] use profile service instead of people service to upload image --- src/app/profile/image/image.component.spec.ts | 6 +++--- src/app/profile/image/image.component.ts | 13 +++++-------- src/app/profile/image/profile-image-editor.component.spec.ts | 10 +++++----- src/app/profile/image/profile-image-editor.component.ts | 10 +++++----- src/app/shared/services/permission.service.ts | 2 +- src/lib/ng-noosfero-api/http/article.service.ts | 8 ++++---- src/lib/ng-noosfero-api/http/profile.service.ts | 50 +++++++++++++++++++++++++++++++++++++++----------- 7 files changed, 62 insertions(+), 37 deletions(-) diff --git a/src/app/profile/image/image.component.spec.ts b/src/app/profile/image/image.component.spec.ts index a648878..48d657b 100644 --- a/src/app/profile/image/image.component.spec.ts +++ b/src/app/profile/image/image.component.spec.ts @@ -7,7 +7,7 @@ import { ComponentTestHelper, createClass } from '../../../spec/component-test-helper'; import { TestComponentBuilder, ComponentFixture } from 'ng-forward/cjs/testing/test-component-builder'; import { Pipe, Input, provide, Component } from 'ng-forward'; -import { PersonService } from "../../../lib/ng-noosfero-api/http/person.service"; +import { ProfileService } from "../../../lib/ng-noosfero-api/http/profile.service"; import * as helpers from "../../../spec/helpers"; @@ -25,14 +25,14 @@ describe("Components", () => { beforeEach((done) => { let scope = helpers.mocks.scopeWithEvents; - let personService = jasmine.createSpyObj("personService", ["upload"]); + let profileService = jasmine.createSpyObj("profileService", ["upload"]); let properties = { profile: { custom_footer: "footer" } }; let cls = createClass({ template: htmlTemplate, directives: [ProfileImageComponent], properties: properties, providers: [ - helpers.createProviderToValue("PersonService", personService), + helpers.createProviderToValue("ProfileService", profileService), helpers.createProviderToValue("$uibModal", helpers.mocks.$modal), helpers.createProviderToValue("$scope", scope) ] diff --git a/src/app/profile/image/image.component.ts b/src/app/profile/image/image.component.ts index 9404e7a..cb2fbb1 100644 --- a/src/app/profile/image/image.component.ts +++ b/src/app/profile/image/image.component.ts @@ -1,5 +1,5 @@ import { Inject, Input, Component, provide } from "ng-forward"; -import { PersonService } from "../../../lib/ng-noosfero-api/http/person.service"; +import { ProfileService } from "../../../lib/ng-noosfero-api/http/profile.service"; import { PermissionService } from "../../shared/services/permission.service"; import { ProfileImageEditorComponent } from "./profile-image-editor.component"; @@ -12,11 +12,10 @@ import { ProfileImageEditorComponent } from "./profile-image-editor.component"; @Component({ selector: "noosfero-profile-image", templateUrl: 'app/profile/image/image.html', - providers: [provide('personService', { useClass: PersonService })] + providers: [provide('profileService', { useClass: ProfileService })] }) -@Inject(PersonService, PermissionService, "$uibModal", "$scope") +@Inject(ProfileService, PermissionService, "$uibModal", "$scope") export class ProfileImageComponent { - /** * @ngdoc property * @name profile @@ -42,8 +41,7 @@ export class ProfileImageComponent { croppedDataUrl: any; modalInstance: any; - constructor(private personService: PersonService, private permissionService: PermissionService, private $uibModal: ng.ui.bootstrap.IModalService, private $scope: ng.IScope) { - console.log('wwwwwwwwwwwwwwwwwwwwwwww', this.editable); + constructor(private profileService: ProfileService, private permissionService: PermissionService, private $uibModal: ng.ui.bootstrap.IModalService, private $scope: ng.IScope) { } fileSelected(file: any, errFiles: any) { @@ -59,7 +57,7 @@ export class ProfileImageComponent { resolve: { picFile: this.picFile, profile: this.profile, - personService: this.personService + profileService: this.profileService } }); } @@ -79,7 +77,6 @@ export class ProfileImageComponent { return this.editable && this.permissionService.isAllowed(this.profile, 'allow_edit'); } - /** * @ngdoc method * @name ngOnInit diff --git a/src/app/profile/image/profile-image-editor.component.spec.ts b/src/app/profile/image/profile-image-editor.component.spec.ts index fe5e79c..a5b7ac4 100644 --- a/src/app/profile/image/profile-image-editor.component.spec.ts +++ b/src/app/profile/image/profile-image-editor.component.spec.ts @@ -18,7 +18,7 @@ describe("Components", () => { let modalInstance = jasmine.createSpyObj("$uibModalInstance", ["close"]); let picFile = { type: "png" }; let $q: ng.IQService; - let personServiceMock: any; + let profileServiceMock: any; let $rootScope: ng.IRootScopeService; beforeEach(inject((_$q_: ng.IQService, _$rootScope_: ng.IRootScopeService) => { @@ -26,7 +26,7 @@ describe("Components", () => { $rootScope = _$rootScope_; })); - let comp = new ProfileImageEditorComponent(picFile, this.profile, personServiceMock, modalInstance); + let comp = new ProfileImageEditorComponent(picFile, this.profile, profileServiceMock, modalInstance); it("get data", done => { @@ -46,10 +46,10 @@ describe("Components", () => { it("upload image", done => { let imageName = "image1"; - personServiceMock = jasmine.createSpyObj("personServiceMock", ["uploadImage"]); + profileServiceMock = jasmine.createSpyObj("profileServiceMock", ["uploadImage"]); let deferredUploadImage = $q.defer(); - personServiceMock.uploadImage = jasmine.createSpy('uploadImage').and.returnValue(deferredUploadImage.promise); - comp.personService = personServiceMock; + profileServiceMock.uploadImage = jasmine.createSpy('uploadImage').and.returnValue(deferredUploadImage.promise); + comp.profileService = profileServiceMock; comp.uploadImage(testDataUrl, imageName); deferredUploadImage.resolve(); $rootScope.$apply(); diff --git a/src/app/profile/image/profile-image-editor.component.ts b/src/app/profile/image/profile-image-editor.component.ts index 7fd4053..7d6d922 100644 --- a/src/app/profile/image/profile-image-editor.component.ts +++ b/src/app/profile/image/profile-image-editor.component.ts @@ -1,19 +1,19 @@ import { StateConfig, Component, Input, Output, Inject, provide } from 'ng-forward'; import { TranslateProfile } from "../../shared/pipes/translate-profile.filter"; -import { PersonService } from "../../../lib/ng-noosfero-api/http/person.service"; +import { ProfileService } from "../../../lib/ng-noosfero-api/http/profile.service"; export class ProfileImageEditorComponent { croppedDataUrl: string; - static $inject = ["picFile", "profile", "personService", "$uibModalInstance"]; + static $inject = ["picFile", "profile", "profileService", "$uibModalInstance"]; - constructor(public picFile: any, public profile: noosfero.Profile, public personService: PersonService, + constructor(public picFile: any, public profile: noosfero.Profile, public profileService: ProfileService, public modalInstance: ng.ui.bootstrap.IModalServiceInstance) { } - + uploadImage(dataUrl: any, name: any) { let base64ImageJson = this.getBase64ImageJson(dataUrl, name); - this.personService.uploadImage(this.profile, base64ImageJson).then((result: any) => { + this.profileService.uploadImage(this.profile, base64ImageJson).then((result: any) => { this.modalInstance.close(name); }); } diff --git a/src/app/shared/services/permission.service.ts b/src/app/shared/services/permission.service.ts index 596fe53..e71a8c7 100644 --- a/src/app/shared/services/permission.service.ts +++ b/src/app/shared/services/permission.service.ts @@ -5,7 +5,7 @@ type WithPermissions = noosfero.Profile | noosfero.Comment | noosfero.Article; @Injectable() export class PermissionService { isAllowed(target: WithPermissions, permission: string) { - return target.permissions.indexOf(permission) >= 0; + return (target.permissions || []).indexOf(permission) >= 0; } } diff --git a/src/lib/ng-noosfero-api/http/article.service.ts b/src/lib/ng-noosfero-api/http/article.service.ts index 362dac5..fb94a77 100644 --- a/src/lib/ng-noosfero-api/http/article.service.ts +++ b/src/lib/ng-noosfero-api/http/article.service.ts @@ -58,7 +58,7 @@ export class ArticleService extends RestangularService { } createInProfile(profile: noosfero.Profile, article: noosfero.Article): ng.IPromise> { - let profileElement = this.profileService.get(profile.id); + let profileElement = this.profileService.getProfileElement(profile.id); (profileElement).id = profile.id; let headers = { 'Content-Type': 'application/json' @@ -84,13 +84,13 @@ export class ArticleService extends RestangularService { } getByProfile(profile: noosfero.Profile, params?: any): ng.IPromise> { - let profileElement = this.profileService.get(profile.id); + let profileElement = this.profileService.getProfileElement(profile.id); return this.list(profileElement, params); } getArticleByProfileAndPath(profile: noosfero.Profile, path: string): ng.IPromise> { let deferred = this.$q.defer>(); - let profileElement = this.profileService.get(profile.id); + let profileElement = this.profileService.getProfileElement(profile.id); let restRequest: ng.IPromise; @@ -108,7 +108,7 @@ export class ArticleService extends RestangularService { } getOneByProfile(profile: noosfero.Profile, params?: any): ng.IPromise> { - let profileElement = this.profileService.get(profile.id); + let profileElement = this.profileService.getProfileElement(profile.id); return this.getSub(profileElement, params); } diff --git a/src/lib/ng-noosfero-api/http/profile.service.ts b/src/lib/ng-noosfero-api/http/profile.service.ts index 0b463db..c68dcbd 100644 --- a/src/lib/ng-noosfero-api/http/profile.service.ts +++ b/src/lib/ng-noosfero-api/http/profile.service.ts @@ -1,15 +1,29 @@ import { Injectable, Inject } from "ng-forward"; +import { RestangularService } from "./restangular_service"; + @Injectable() @Inject("Restangular", "$q") -export class ProfileService { +export class ProfileService extends RestangularService { private _currentProfilePromise: ng.IDeferred; - constructor(private restangular: restangular.IService, private $q: ng.IQService) { + constructor(private restangular: restangular.IService, $q: ng.IQService, $log: ng.ILogService) { + super(restangular, $q, $log); this.resetCurrentProfile(); } + getResourcePath() { + return "profiles"; + } + + getDataKeys() { + return { + singular: 'profile', + plural: 'profiles' + }; + } + resetCurrentProfile() { this._currentProfilePromise = this.$q.defer(); } @@ -31,7 +45,7 @@ export class ProfileService { } getHomePage(profileId: number, params?: any) { - return this.get(profileId).customGET("home_page", params); + return this.getProfileElement(profileId).customGET("home_page", params); } getByIdentifier(identifier: string): ng.IPromise { @@ -45,28 +59,28 @@ export class ProfileService { } getProfileMembers(profileId: number, params?: any): restangular.IPromise { - return this.get(profileId).customGET("members", params); + return this.getProfileElement(profileId).customGET("members", params); } getBoxes(profileId: number): restangular.IPromise { - return this.get(profileId).customGET('boxes'); + return this.getProfileElement(profileId).customGET('boxes'); } getActivities(profileId: number, params?: any): restangular.IPromise { - return this.get(profileId).customGET("activities", params); + return this.getProfileElement(profileId).customGET("activities", params); } - get(profileId: number): restangular.IElement { + getProfileElement(profileId: number): restangular.IElement { return this.restangular.one('profiles', profileId); } update(profile: noosfero.Profile) { let headers = { 'Content-Type': 'application/json' }; - return this.get(profile.id).customPOST({ profile: profile }, null, null, headers); + return this.getProfileElement(profile.id).customPOST({ profile: profile }, null, null, headers); } getMembers(profile: noosfero.Profile, params?: any) { - let p = this.get(profile.id); + let p = this.getProfileElement(profile.id); return p.customGET('members', params); } @@ -83,10 +97,24 @@ export class ProfileService { } addMember(person: noosfero.Person, profile: noosfero.Profile) { - return this.get(profile.id).customPOST({}, "members", null, null); + return this.getProfileElement(profile.id).customPOST({}, "members", null, null); } removeMember(person: noosfero.Person, profile: noosfero.Profile) { - return this.get(profile.id).customDELETE("members", null, null); + return this.getProfileElement(profile.id).customDELETE("members", null, null); + } + + uploadImage(profile: noosfero.Profile, base64ImageJson: any) { + let headers = { 'Content-Type': 'application/json' }; + let deferred = this.$q.defer>(); + // TODO dynamically copy the selected attributes to update + let attributesToUpdate: any = { + profile: { image_builder: base64ImageJson } + }; + let restRequest: ng.IPromise> = + this.getProfileElement(profile.id).customPOST(attributesToUpdate, null, null, headers); + restRequest.then(this.getHandleSuccessFunction(deferred)) + .catch(this.getHandleErrorFunction(deferred)); + return deferred.promise; } } -- libgit2 0.21.2