aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/app/components/post/post.component.ts71
-rw-r--r--src/app/services/rating.service.ts18
2 files changed, 64 insertions, 25 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--;
+ }
+ );
+}
}
diff --git a/src/app/services/rating.service.ts b/src/app/services/rating.service.ts
index f3e3265..58ff1f4 100644
--- a/src/app/services/rating.service.ts
+++ b/src/app/services/rating.service.ts
@@ -36,6 +36,13 @@ export class RatingService {
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()),
@@ -67,6 +74,15 @@ export class RatingService {
headers: new HttpHeaders().set('Authorization', 'Bearer ' + authToken)
};
- return this._http.get(AppConstants.API_RATING_URL + 'GetByUserAndPost', options);
+ 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);
}
}