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
|
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.model';
import { AppConstants } from '../app-constants.module';
import { TokenService } from './token.service';
@Injectable({
providedIn: 'root'
})
export class PostService {
constructor(private _http: HttpClient, private _tokenService: TokenService)
{ }
getDefaultPost(): Post {
return new Post(Guid.createEmpty(), 'Gosho', 'Trapov', 'gosho_trapov', 'Your opinion on my idea?', new Date(), [], [], 0);
}
/* Requests from session storage */
createPostWithSessionStorageRequest(postMessage: string, files: File[]): Observable<object> {
const userId = this._tokenService.getUserIdFromSessionStorageToken();
const token = this._tokenService.getTokenFromSessionStorage();
return this.createPostRequest(userId, token, postMessage, files);
}
putPostWithSessionStorageRequest(postId: Guid, newMessage: string, posts: File[]): Observable<object> {
const userId = this._tokenService.getUserIdFromSessionStorageToken();
const token = this._tokenService.getTokenFromSessionStorage();
return this.putPostRequest(userId, token, postId, newMessage, posts);
}
deletePostWithSessionStorage(postId: Guid): Observable<object> {
const token = this._tokenService.getTokenFromSessionStorage();
return this.deletePostRequest(postId, token);
}
/* Post requests */
createPostRequest(userId: Guid, authToken: string, postMessage: string, files: File[]): Observable<object> {
const form = new FormData();
form.append('message', postMessage);
for (const file of files) {
form.append('files', file, file.name);
}
const options = {
params: new HttpParams().set('UserId', userId.toString()),
headers: new HttpHeaders().set('Authorization', 'Bearer ' + authToken)
};
return this._http.post(AppConstants.API_POST_URL, form, options);
}
getPostRequest(id: Guid): Observable<object> {
const options = {
params: new HttpParams().set('Id', id.toString())
};
return this._http.get(AppConstants.API_POST_URL, options);
}
putPostRequest(userId: Guid, authToken: string, postId: Guid, newMessage: string, files: File[]): Observable<object> {
const form = new FormData();
form.append('postId', postId);
form.append('newMessage', newMessage);
for (const file of files) {
form.append('files', file, file.name);
}
const options = {
params: new HttpParams().set('UserId', userId.toString()),
headers: new HttpHeaders().set('Authorization', 'Bearer ' + authToken)
};
return this._http.put(AppConstants.API_POST_URL, form, options);
}
deletePostRequest(postId: Guid, authToken: string): Observable<object> {
const options = {
params: new HttpParams().set('PostId', postId.toString()),
headers: new HttpHeaders().set('Authorization', 'Bearer ' + authToken)
};
return this._http.delete(AppConstants.API_POST_URL, options);
}
}
|