-
Notifications
You must be signed in to change notification settings - Fork 2
/
comment.service.ts
60 lines (50 loc) · 1.56 KB
/
comment.service.ts
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
import { Injectable } from '@angular/core';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { catchError, map, tap } from 'rxjs/operators';
interface User {
username: string;
profileImage: string;
}
interface Comment {
id: number;
user: User;
text: string;
projectId: number;
}
interface CommentApiResponse {
content: Comment[];
pageNumber: number;
pageSize: number;
totalElements: number;
totalPages: number;
first: boolean;
last: boolean;
}
@Injectable({
providedIn: 'root'
})
export class CommentService {
constructor(private http: HttpClient) {}
getComments(projectId: number, page: number, pageSize: number = 10): Observable<Comment[]> {
return this.http.get<CommentApiResponse>(`/api/comments/projects/${projectId}/?page=${page}`).pipe(
map(response => response.content)
, // Mapear solo la parte del contenido
catchError(this.handleError) // Manejar errores potenciales
);
}
private handleError(error: HttpErrorResponse) {
console.error('An error occurred:', error);
return throwError(() => new Error('Something bad happened; please try again later.'));
}
deleteComment(commentId: number): Observable<any> {
return this.http.delete(`/api/comments/${commentId}/`).pipe(
catchError(this.handleError)
);
}
addComment(id : number, commentText: string) {
const url = `/api/comments/${id}/`;
const body = commentText ;
// Enviar la solicitud POST al servidor
return this.http.post(url, body); }
}