1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import * as FormData from 'form-data';
import { Guid } from 'guid-typescript';
import { Observable } from 'rxjs';
import { Post } from 'src/models/post';
import { AppConstants } from '../app-constants.module';
import { TokenService } from './token.service';
@Injectable({
providedIn: 'root'
})
export class RatingService {
constructor(private _http: HttpClient, private _tokenService: TokenService)
{ }
createRatingWithSessionStorageRequest(postId: Guid, isLike: boolean): Observable<object> {
const userId = this._tokenService.getUserIdFromSessionStorageToken();
const token = this._tokenService.getTokenFromSessionStorage();
return this.createRatingRequest(userId, token, postId, isLike);
}
putRatingWithSessionStorageRequest(postId: Guid, isLike: boolean): Observable<object> {
const userId = this._tokenService.getUserIdFromSessionStorageToken();
const token = this._tokenService.getTokenFromSessionStorage();
return this.putRatingRequest(userId, token, postId, isLike);
}
getRatingByUserAndPostWithSessionStorageRequest(postId: Guid): Observable<object> {
const userId = this._tokenService.getUserIdFromSessionStorageToken();
const token = this._tokenService.getTokenFromSessionStorage();
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()),
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<object> {
const options = {
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.put(AppConstants.API_RATING_URL, body, options);
}
getRatingByUserAndPostRequest(userId: Guid, authToken: string, postId: Guid): Observable<object> {
const options = {
params: new HttpParams().set('UserId', userId.toString()).set('PostId', postId.toString()),
headers: new HttpHeaders().set('Authorization', 'Bearer ' + authToken)
};
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);
}
}
|