diff options
| author | Danail Dimitrov <danaildimitrov321@gmail.com> | 2021-03-14 21:48:04 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-03-14 21:48:04 +0200 |
| commit | 8bd52ec0e9a58405376b0a9ac08ce8e35bad8599 (patch) | |
| tree | 2b836291af4bc6801d7d809c828eb3e7d58a027a /src/app/services | |
| parent | 988c09b9233fd356b056b62593494772a2061993 (diff) | |
| parent | e4fc300d2fe82084c67c50258c76b60248850863 (diff) | |
| download | DevHive-Angular-8bd52ec0e9a58405376b0a9ac08ce8e35bad8599.tar DevHive-Angular-8bd52ec0e9a58405376b0a9ac08ce8e35bad8599.tar.gz DevHive-Angular-8bd52ec0e9a58405376b0a9ac08ce8e35bad8599.zip | |
Merge pull request #2 from Team-Kaleidoscope/rating_system
Rating system
Diffstat (limited to 'src/app/services')
| -rw-r--r-- | src/app/services/post.service.ts | 2 | ||||
| -rw-r--r-- | src/app/services/rating.service.ts | 88 |
2 files changed, 89 insertions, 1 deletions
diff --git a/src/app/services/post.service.ts b/src/app/services/post.service.ts index eb613a6..c8bd892 100644 --- a/src/app/services/post.service.ts +++ b/src/app/services/post.service.ts @@ -15,7 +15,7 @@ export class PostService { { } getDefaultPost(): Post { - return new Post(Guid.createEmpty(), 'Gosho', 'Trapov', 'gosho_trapov', 'Your opinion on my idea?', new Date(), [], []); + return new Post(Guid.createEmpty(), 'Gosho', 'Trapov', 'gosho_trapov', 'Your opinion on my idea?', new Date(), [], [], 0); } /* Requests from session storage */ diff --git a/src/app/services/rating.service.ts b/src/app/services/rating.service.ts new file mode 100644 index 0000000..58ff1f4 --- /dev/null +++ b/src/app/services/rating.service.ts @@ -0,0 +1,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); + } +} |
