diff options
| author | Danail Dimitrov <danaildimitrov321@gmail.com> | 2021-03-13 12:02:56 +0200 |
|---|---|---|
| committer | Danail Dimitrov <danaildimitrov321@gmail.com> | 2021-03-13 12:02:56 +0200 |
| commit | 98d4f0e3a0fc8fd8d753617a2b681a18f8a60e2b (patch) | |
| tree | 99c14a82c0a08c7f93d98c919130f60fdd5a27f8 /src/app/components | |
| parent | 2ea7a39c3f8eaf90ec28f8fd6fc465f215a0d99b (diff) | |
| download | DevHive-Angular-98d4f0e3a0fc8fd8d753617a2b681a18f8a60e2b.tar DevHive-Angular-98d4f0e3a0fc8fd8d753617a2b681a18f8a60e2b.tar.gz DevHive-Angular-98d4f0e3a0fc8fd8d753617a2b681a18f8a60e2b.zip | |
adding downVote functionality
Diffstat (limited to 'src/app/components')
| -rw-r--r-- | src/app/components/post/post.component.ts | 75 |
1 files changed, 35 insertions, 40 deletions
diff --git a/src/app/components/post/post.component.ts b/src/app/components/post/post.component.ts index 68bf161..1ec9232 100644 --- a/src/app/components/post/post.component.ts +++ b/src/app/components/post/post.component.ts @@ -68,66 +68,61 @@ export class PostComponent implements OnInit { this._ratingServe.getRatingByUserAndPostWithSessionStorageRequest(Guid.parse(this.paramId)).subscribe( (x: object) => { if (Object.values(x)[3]) { - this.deleteUpVoteRating(Object.values(x)[0]); + this.deleteRating(Object.values(x)[0], true); } else { - this.putUpVoteRating(); + this.putRating(true); } }, () => { - this.crateUpVoteRating(); + this.crateRating(true); } ); } - - crateUpVoteRating(): void { - this._ratingServe.createRatingWithSessionStorageRequest(Guid.parse(this.paramId), true).subscribe( - () => { - this.votesNumber++; + + downVotePost(): void { + if (!this.loggedIn) { + this._router.navigate(['/login']); + return; } - ); -} - putUpVoteRating(): void { - this._ratingServe.putRatingWithSessionStorageRequest(Guid.parse(this.paramId), true).subscribe( - () => { - this.votesNumber += 2; + this._ratingServe.getRatingByUserAndPostWithSessionStorageRequest(Guid.parse(this.paramId)).subscribe( + (x: object) => { + if (!Object.values(x)[3]) { + this.deleteRating(Object.values(x)[0], false); + } + else { + this.putRating(false); + } }, () => { - this.crateUpVoteRating(); + this.crateRating(false); } ); } - deleteUpVoteRating(ratingId: string): void { - this._ratingServe.deleteRatingFromSessionStorageRequest(Guid.parse(ratingId)).subscribe( + crateRating(isLike: boolean): void { + this._ratingServe.createRatingWithSessionStorageRequest(Guid.parse(this.paramId), isLike).subscribe( + () => { + this.votesNumber += -1 + Number(isLike) * 2; + } + ); +} + + putRating(isLike: boolean): void { + this._ratingServe.putRatingWithSessionStorageRequest(Guid.parse(this.paramId), isLike).subscribe( () => { - this.votesNumber--; + // when false -2 + 0 wjen true -2 + 4 + this.votesNumber += -2 + Number(isLike) * 4; } ); } - downVotePost(): void { - if(!this.loggedIn) { - this._router.navigate(['/login']); - return; -} - -this._ratingServe.putRatingWithSessionStorageRequest(Guid.parse(this.paramId), false).subscribe( - () => { - this.votesNumber -= 2; - }, - () => { - this.crateDownVoteRating(); - } -); + deleteRating(ratingId: string, isLike: boolean): void { + this._ratingServe.deleteRatingFromSessionStorageRequest(Guid.parse(ratingId)).subscribe( + () => { + this.votesNumber += 1 - Number(isLike) * 2; + } + ); } - - crateDownVoteRating(): void { - this._ratingServe.createRatingWithSessionStorageRequest(Guid.parse(this.paramId), false).subscribe( - () => { - this.votesNumber--; - } - ); -} } |
