From 98d4f0e3a0fc8fd8d753617a2b681a18f8a60e2b Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Sat, 13 Mar 2021 12:02:56 +0200 Subject: adding downVote functionality --- src/app/components/post/post.component.ts | 75 +++++++++++++++---------------- 1 file changed, 35 insertions(+), 40 deletions(-) (limited to 'src') 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--; - } - ); -} } -- cgit v1.2.3