aboutsummaryrefslogtreecommitdiff
path: root/src/app/services/comment.service.ts
blob: 29707ab8f102fff66c8fb0e85cc8fd310b1fdc46 (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
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
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Guid } from 'guid-typescript';
import { Observable } from 'rxjs';
import { Comment } from 'src/models/comment.model';
import { AppConstants } from '../app-constants.module';
import { TokenService } from './token.service';

@Injectable({
  providedIn: 'root'
})
export class CommentService {
  constructor(private _http: HttpClient, private _tokenService: TokenService)
  { }

  getDefaultComment(): Comment {
    return new Comment(Guid.createEmpty(), Guid.createEmpty(), 'Gosho', 'Trapov', 'gosho_trapov', 'Your opinion on my idea?', new Date());
  }

  /* Requests from session storage */

  createCommentWithSessionStorageRequest(postId: Guid, commentMessage: string): Observable<object> {
    const userId = this._tokenService.getUserIdFromSessionStorageToken();
    const token = this._tokenService.getTokenFromSessionStorage();

    return this.createCommentRequest(userId, token, postId, commentMessage);
  }

  putCommentWithSessionStorageRequest(commentId: Guid, postId: Guid, newMessage: string): Observable<object> {
    const userId = this._tokenService.getUserIdFromSessionStorageToken();
    const token = this._tokenService.getTokenFromSessionStorage();

    return this.putCommentRequest(userId, token, commentId, postId, newMessage);
  }

  deleteCommentWithSessionStorage(commentId: Guid): Observable<object> {
    const token = this._tokenService.getTokenFromSessionStorage();

    return this.deleteCommentRequest(commentId, token);
  }

  /* Comment requests */

  createCommentRequest(userId: Guid, authToken: string, postId: Guid, commentMessage: string): Observable<object> {
    const body = {
      postId: postId.toString(),
      message: commentMessage
    };
    const options = {
      params: new HttpParams().set('UserId', userId.toString()),
      headers: new HttpHeaders().set('Authorization', 'Bearer ' + authToken)
    };
    return this._http.post(AppConstants.API_COMMENT_URL, body, options);
  }

  getCommentRequest(id: Guid): Observable<object> {
    const options = {
      params: new HttpParams().set('Id', id.toString())
    };
    return this._http.get(AppConstants.API_COMMENT_URL, options);
  }

  putCommentRequest(userId: Guid, authToken: string, commentId: Guid, postId: Guid, newMessage: string): Observable<object> {
    const body = {
      commentId: commentId.toString(),
      postId: postId.toString(),
      newMessage: newMessage
    };
    const options = {
      params: new HttpParams().set('UserId', userId.toString()),
      headers: new HttpHeaders().set('Authorization', 'Bearer ' + authToken)
    };
    return this._http.put(AppConstants.API_COMMENT_URL, body, options);
  }

  deleteCommentRequest(commentId: Guid, authToken: string): Observable<object> {
    const options = {
      params: new HttpParams().set('Id', commentId.toString()),
      headers: new HttpHeaders().set('Authorization', 'Bearer ' + authToken)
    };
    return this._http.delete(AppConstants.API_COMMENT_URL, options);
  }
}