From 386b25c583c1a1632282beab91537ff54e1bce83 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Wed, 7 Apr 2021 21:13:28 +0300 Subject: Added profile picture service and removed put profile picture from user service --- src/app/app-constants.module.ts | 1 + src/app/services/profile-picture.service.ts | 32 +++++++++++++++++++++++++++++ src/app/services/user.service.ts | 17 --------------- 3 files changed, 33 insertions(+), 17 deletions(-) create mode 100644 src/app/services/profile-picture.service.ts (limited to 'src/app') diff --git a/src/app/app-constants.module.ts b/src/app/app-constants.module.ts index 39538e0..d1bd9f5 100644 --- a/src/app/app-constants.module.ts +++ b/src/app/app-constants.module.ts @@ -2,6 +2,7 @@ export class AppConstants { public static BASE_API_URL = 'http://localhost:5000/api'; public static API_USER_URL = AppConstants.BASE_API_URL + '/User'; + public static API_PROFILE_PICTURE_URL = AppConstants.BASE_API_URL + '/ProfilePicture'; public static API_USER_LOGIN_URL = AppConstants.API_USER_URL + '/login'; public static API_USER_REGISTER_URL = AppConstants.API_USER_URL + '/register'; diff --git a/src/app/services/profile-picture.service.ts b/src/app/services/profile-picture.service.ts new file mode 100644 index 0000000..8b9d0a3 --- /dev/null +++ b/src/app/services/profile-picture.service.ts @@ -0,0 +1,32 @@ +import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http'; +import { Injectable } from '@angular/core'; +import { Guid } from 'guid-typescript'; +import { Observable } from 'rxjs'; +import { AppConstants } from '../app-constants.module'; +import { TokenService } from './token.service'; + +@Injectable({ + providedIn: 'root' +}) +export class ProfilePictureService { + constructor(private _http: HttpClient, private _tokenService: TokenService) + { } + + putPictureWithSessionStorageRequest(newPicture: File): Observable { + const userId = this._tokenService.getUserIdFromSessionStorageToken(); + const token = this._tokenService.getTokenFromSessionStorage(); + + return this.putRatingRequest(userId, token, newPicture); + } + + putRatingRequest(userId: Guid, authToken: string, newPicture: File): Observable { + const options = { + params: new HttpParams().set('UserId', userId.toString()), + headers: new HttpHeaders().set('Authorization', 'Bearer ' + authToken) + }; + const form = new FormData(); + form.append('picture', newPicture); + + return this._http.put(AppConstants.API_PROFILE_PICTURE_URL, form, options); + } +} diff --git a/src/app/services/user.service.ts b/src/app/services/user.service.ts index 10c8c59..e910ada 100644 --- a/src/app/services/user.service.ts +++ b/src/app/services/user.service.ts @@ -45,13 +45,6 @@ export class UserService { return this.putUserRequest(userId, token, updateUserFormGroup, languages, technologies, userRoles, userFriends); } - putProfilePictureFromSessionStorageRequest(newPicture: File): Observable { - const userId = this._tokenService.getUserIdFromSessionStorageToken(); - const token = this._tokenService.getTokenFromSessionStorage(); - - return this.putProfilePictureRequest(userId, token, newPicture); - } - putBareUserFromSessionStorageRequest(user: User, password: string): Observable { const userId = this._tokenService.getUserIdFromSessionStorageToken(); const token = this._tokenService.getTokenFromSessionStorage(); @@ -150,16 +143,6 @@ export class UserService { return this._http.put(AppConstants.API_USER_URL, body, options); } - putProfilePictureRequest(userId: Guid, authToken: string, newPicture: File): Observable { - const form = new FormData(); - form.append('picture', newPicture); - const options = { - params: new HttpParams().set('UserId', userId.toString()), - headers: new HttpHeaders().set('Authorization', 'Bearer ' + authToken) - }; - return this._http.put(AppConstants.API_USER_URL + '/ProfilePicture', form, options); - } - deleteUserRequest(userId: Guid, authToken: string): Observable { const options = { params: new HttpParams().set('Id', userId.toString()), -- cgit v1.2.3 From 10a4896a17495548ed3ce6b333939203b698470b Mon Sep 17 00:00:00 2001 From: Syndamia Date: Wed, 7 Apr 2021 21:22:43 +0300 Subject: Updated profile settings page to use profile picture service --- src/app/components/profile-settings/profile-settings.component.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/app') diff --git a/src/app/components/profile-settings/profile-settings.component.ts b/src/app/components/profile-settings/profile-settings.component.ts index f329942..3d6e491 100644 --- a/src/app/components/profile-settings/profile-settings.component.ts +++ b/src/app/components/profile-settings/profile-settings.component.ts @@ -14,6 +14,7 @@ import { Technology } from 'src/models/technology.model'; import { TokenService } from 'src/app/services/token.service'; import { Title } from '@angular/platform-browser'; import { AppConstants } from 'src/app/app-constants.module'; +import { ProfilePictureService } from 'src/app/services/profile-picture.service'; @Component({ selector: 'app-profile-settings', @@ -40,7 +41,7 @@ export class ProfileSettingsComponent implements OnInit { public availableTechnologies: Technology[]; public showCurrentPassword = false; - constructor(private _titleService: Title, private _router: Router, private _userService: UserService, private _languageService: LanguageService, private _technologyService: TechnologyService, private _tokenService: TokenService, private _fb: FormBuilder, private _location: Location) { + constructor(private _titleService: Title, private _router: Router, private _userService: UserService, private _profilePictureService: ProfilePictureService, private _languageService: LanguageService, private _technologyService: TechnologyService, private _tokenService: TokenService, private _fb: FormBuilder, private _location: Location) { this._titleService.setTitle(this._title); } @@ -187,7 +188,7 @@ export class ProfileSettingsComponent implements OnInit { return; } - this._userService.putProfilePictureFromSessionStorageRequest(this.newProfilePicture).subscribe({ + this._profilePictureService.putPictureWithSessionStorageRequest(this.newProfilePicture).subscribe({ next: () => { this.reloadPage(); } -- cgit v1.2.3