aboutsummaryrefslogtreecommitdiff
path: root/src/app/services/rating.service.ts
blob: 5a86b67a5063f83e76cf447a504af74a20ff1c7f (plain) (blame)
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
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);
  }

  createRatingRequest(userId: Guid, authToken: string, postId: Guid, isLike: boolean): Observable<object>  {
    const body = {
      postId: postId.toString(),
      isLike: isLike
    };
    const options = {
      params: new HttpParams().set('UserId', userId.toString()),
      headers: new HttpHeaders().set('Authorization', 'Bearer ' + authToken)     
    };

    return this._http.post(AppConstants.API_RATING_URL, body, options);
  }
}