From afeba5e832d87bc659d72e7d945a78821bd7e7b8 Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Fri, 12 Mar 2021 21:16:34 +0200 Subject: adding downVote functionality --- src/app/components/post/post.component.html | 2 +- src/app/components/post/post.component.ts | 35 +++++++++++++++++++++++++++++ src/app/services/rating.service.ts | 27 ++++++++++++++++++---- 3 files changed, 59 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/app/components/post/post.component.html b/src/app/components/post/post.component.html index 4584591..830fa75 100644 --- a/src/app/components/post/post.component.html +++ b/src/app/components/post/post.component.html @@ -36,7 +36,7 @@ {{ votesNumber }} - diff --git a/src/app/components/post/post.component.ts b/src/app/components/post/post.component.ts index 0f48337..168b6a3 100644 --- a/src/app/components/post/post.component.ts +++ b/src/app/components/post/post.component.ts @@ -66,10 +66,45 @@ export class PostComponent implements OnInit { return; } + this._ratingServe.putRatingWithSessionStorageRequest(Guid.parse(this.paramId), true).subscribe( + () => { + this.votesNumber += 2; + }, + () => { + this.crateUpVoteRating(); + } + ); + } + + crateUpVoteRating(): void { this._ratingServe.createRatingWithSessionStorageRequest(Guid.parse(this.paramId), true).subscribe( () => { this.votesNumber++; } ); } + + downVotePost(): void { + if (!this.loggedIn) { + this._router.navigate(['/login']); + return; + } + + this._ratingServe.putRatingWithSessionStorageRequest(Guid.parse(this.paramId), false).subscribe( + () => { + this.votesNumber -= 2; + }, + () => { + this.crateDownVoteRating(); + } + ); + } + + crateDownVoteRating(): void { + this._ratingServe.createRatingWithSessionStorageRequest(Guid.parse(this.paramId), false).subscribe( + () => { + this.votesNumber--; + } + ); + } } diff --git a/src/app/services/rating.service.ts b/src/app/services/rating.service.ts index 5a86b67..9403575 100644 --- a/src/app/services/rating.service.ts +++ b/src/app/services/rating.service.ts @@ -22,16 +22,35 @@ export class RatingService { return this.createRatingRequest(userId, token, postId, isLike); } + putRatingWithSessionStorageRequest(postId: Guid, isLike: boolean): Observable { + const userId = this._tokenService.getUserIdFromSessionStorageToken(); + const token = this._tokenService.getTokenFromSessionStorage(); + + return this.putRatingRequest(userId, token, postId, isLike); + } + createRatingRequest(userId: Guid, authToken: string, postId: Guid, isLike: boolean): Observable { + 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 { const options = { - params: new HttpParams().set('UserId', userId.toString()), - headers: new HttpHeaders().set('Authorization', 'Bearer ' + authToken) + 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.post(AppConstants.API_RATING_URL, body, options); + return this._http.put(AppConstants.API_RATING_URL, body, options); } } -- cgit v1.2.3