1 import { catchError, map } from 'rxjs/operators'
2 import { HttpClient } from '@angular/common/http'
3 import { Injectable } from '@angular/core'
4 import { UserCreate, UserUpdateMe, UserVideoQuota } from '../../../../../shared'
5 import { environment } from '../../../environments/environment'
6 import { RestExtractor } from '../rest'
7 import { Avatar } from '../../../../../shared/models/avatars/avatar.model'
10 export class UserService {
11 static BASE_USERS_URL = environment.apiUrl + '/api/v1/users/'
14 private authHttp: HttpClient,
15 private restExtractor: RestExtractor
19 changePassword (newPassword: string) {
20 const url = UserService.BASE_USERS_URL + 'me'
21 const body: UserUpdateMe = {
25 return this.authHttp.put(url, body)
27 map(this.restExtractor.extractDataBool),
28 catchError(err => this.restExtractor.handleError(err))
32 updateMyProfile (profile: UserUpdateMe) {
33 const url = UserService.BASE_USERS_URL + 'me'
35 return this.authHttp.put(url, profile)
37 map(this.restExtractor.extractDataBool),
38 catchError(err => this.restExtractor.handleError(err))
42 changeAvatar (avatarForm: FormData) {
43 const url = UserService.BASE_USERS_URL + 'me/avatar/pick'
45 return this.authHttp.post<{ avatar: Avatar }>(url, avatarForm)
46 .pipe(catchError(err => this.restExtractor.handleError(err)))
49 signup (userCreate: UserCreate) {
50 return this.authHttp.post(UserService.BASE_USERS_URL + 'register', userCreate)
52 map(this.restExtractor.extractDataBool),
53 catchError(err => this.restExtractor.handleError(err))
57 getMyVideoQuotaUsed () {
58 const url = UserService.BASE_USERS_URL + '/me/video-quota-used'
60 return this.authHttp.get<UserVideoQuota>(url)
61 .pipe(catchError(err => this.restExtractor.handleError(err)))
64 askResetPassword (email: string) {
65 const url = UserService.BASE_USERS_URL + '/ask-reset-password'
67 return this.authHttp.post(url, { email })
69 map(this.restExtractor.extractDataBool),
70 catchError(err => this.restExtractor.handleError(err))
74 resetPassword (userId: number, verificationString: string, password: string) {
75 const url = `${UserService.BASE_USERS_URL}/${userId}/reset-password`
81 return this.authHttp.post(url, body)
83 map(this.restExtractor.extractDataBool),
84 catchError(res => this.restExtractor.handleError(res))