diff options
| author | Kamen Mladenov <kamen.d.mladenov@protonmail.com> | 2021-04-09 19:55:59 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-09 19:55:59 +0300 |
| commit | f849e37ccdd6fd48f83119a3b3b65cdd8b765dc3 (patch) | |
| tree | 83b88a773bb7dc053bb3aced35bce302264ec925 /src/app/services/rating.service.ts | |
| parent | bcd88af53b1a920d728ec98b45daa9ac2e2c0917 (diff) | |
| parent | c13889759d70687de6833c505221c203f65fedb8 (diff) | |
| download | DevHive-Angular-main.tar DevHive-Angular-main.tar.gz DevHive-Angular-main.zip | |
Second Stage: Complete
Diffstat (limited to 'src/app/services/rating.service.ts')
| -rw-r--r-- | src/app/services/rating.service.ts | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/src/app/services/rating.service.ts b/src/app/services/rating.service.ts new file mode 100644 index 0000000..be35a81 --- /dev/null +++ b/src/app/services/rating.service.ts @@ -0,0 +1,86 @@ +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 RatingService { + constructor(private _http: HttpClient, private _tokenService: TokenService) + { } + + createRatingWithSessionStorageRequest(postId: Guid, isLike: boolean): Observable<object> { + const userId = this._tokenService.getUserIdFromSessionStorageToken(); + const token = this._tokenService.getTokenFromSessionStorage(); + + return this.createRatingRequest(userId, token, postId, isLike); + } + + putRatingWithSessionStorageRequest(postId: Guid, isLike: boolean): Observable<object> { + const userId = this._tokenService.getUserIdFromSessionStorageToken(); + const token = this._tokenService.getTokenFromSessionStorage(); + + return this.putRatingRequest(userId, token, postId, isLike); + } + + getRatingByUserAndPostWithSessionStorageRequest(postId: Guid): Observable<object> { + const userId = this._tokenService.getUserIdFromSessionStorageToken(); + const token = this._tokenService.getTokenFromSessionStorage(); + + return this.getRatingByUserAndPostRequest(userId, token, postId); + } + + deleteRatingFromSessionStorageRequest(ratingId: Guid): Observable<object> { + const userId = this._tokenService.getUserIdFromSessionStorageToken(); + const token = this._tokenService.getTokenFromSessionStorage(); + + return this.deleteRatingRequest(userId, token, ratingId); + } + + createRatingRequest(userId: Guid, authToken: string, postId: Guid, isLike: boolean): Observable<object> { + const options = { + params: new HttpParams().set('UserId', userId.toString()), + headers: new HttpHeaders().set('Authorization', 'Bearer ' + authToken) + }; + const body = { + postId: postId.toString(), + isLike: isLike + }; + + return this._http.post(AppConstants.API_RATING_URL, body, options); + } + + putRatingRequest(userId: Guid, authToken: string, postId: Guid, isLike: boolean): Observable<object> { + const options = { + params: new HttpParams().set('UserId', userId.toString()).set('PostId', postId.toString()), + headers: new HttpHeaders().set('Authorization', 'Bearer ' + authToken) + }; + const body = { + isLike: isLike + }; + + return this._http.put(AppConstants.API_RATING_URL, body, options); + } + + getRatingByUserAndPostRequest(userId: Guid, authToken: string, postId: Guid): Observable<object> { + const options = { + params: new HttpParams().set('UserId', userId.toString()).set('PostId', postId.toString()), + headers: new HttpHeaders().set('Authorization', 'Bearer ' + authToken) + }; + + return this._http.get(AppConstants.API_RATING_URL + '/GetByUserAndPost', options); + } + + deleteRatingRequest(userId: Guid, authToken: string, ratingId: Guid): Observable<object> { + const options = { + params: new HttpParams().set('UserId', userId.toString()).set('RatingId', ratingId.toString()), + headers: new HttpHeaders().set('Authorization', 'Bearer ' + authToken) + }; + + return this._http.delete(AppConstants.API_RATING_URL, options); + } +} |
