diff options
Diffstat (limited to 'src/app/components/post/post.component.ts')
| -rw-r--r-- | src/app/components/post/post.component.ts | 71 |
1 files changed, 47 insertions, 24 deletions
diff --git a/src/app/components/post/post.component.ts b/src/app/components/post/post.component.ts index 168b6a3..68bf161 100644 --- a/src/app/components/post/post.component.ts +++ b/src/app/components/post/post.component.ts @@ -22,8 +22,7 @@ export class PostComponent implements OnInit { @Input() paramId: string; public loggedIn = false; - constructor(private _postService: PostService, private _ratingServe: RatingService, private _userService: UserService, private _router: Router, private _tokenService: TokenService) - { } + constructor(private _postService: PostService, private _ratingServe: RatingService, private _userService: UserService, private _router: Router, private _tokenService: TokenService) { } ngOnInit(): void { this.loggedIn = this._tokenService.getTokenFromSessionStorage() !== ''; @@ -66,45 +65,69 @@ export class PostComponent implements OnInit { return; } - 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.deleteUpVoteRating(Object.values(x)[0]); + } + else { + this.putUpVoteRating(); + } }, () => { 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; + + crateUpVoteRating(): void { + this._ratingServe.createRatingWithSessionStorageRequest(Guid.parse(this.paramId), true).subscribe( + () => { + this.votesNumber++; } + ); +} - this._ratingServe.putRatingWithSessionStorageRequest(Guid.parse(this.paramId), false).subscribe( + putUpVoteRating(): void { + this._ratingServe.putRatingWithSessionStorageRequest(Guid.parse(this.paramId), true).subscribe( () => { - this.votesNumber -= 2; + this.votesNumber += 2; }, () => { - this.crateDownVoteRating(); + this.crateUpVoteRating(); } - ); + ); } - crateDownVoteRating(): void { - this._ratingServe.createRatingWithSessionStorageRequest(Guid.parse(this.paramId), false).subscribe( + deleteUpVoteRating(ratingId: string): void { + this._ratingServe.deleteRatingFromSessionStorageRequest(Guid.parse(ratingId)).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--; + } + ); +} } |
