diff --git a/src/app/services/http/comment.service.ts b/src/app/services/http/comment.service.ts index f46b26fbd3e89975f12bb53f63c14724f11446f6..2bd824827a0e1c5a5a571b991a4f7f2cca65a66e 100644 --- a/src/app/services/http/comment.service.ts +++ b/src/app/services/http/comment.service.ts @@ -1,57 +1,75 @@ import { Injectable } from '@angular/core'; import { HttpClient, HttpHeaders } from '@angular/common/http'; import { Observable } from 'rxjs/Observable'; -import { Comment } from '../../models/comment'; import { catchError, tap } from 'rxjs/operators'; +import { Comment } from '../../models/comment'; +import { AuthenticationService } from './authentication.service'; import { BaseHttpService } from './base-http.service'; const httpOptions = { - headers: new HttpHeaders({ 'Content-Type': 'application/json' }) + headers: new HttpHeaders({}) }; @Injectable() export class CommentService extends BaseHttpService { - private commentsUrl = 'api/comments'; + private apiBaseUrl = 'https://arsnova-staging.mni.thm.de/api'; + private commentsUrl = '/comment'; + private findCommentUrl = '/find'; - constructor( private http: HttpClient ) { + constructor( private http: HttpClient, + private authService: AuthenticationService ) { super(); } addComment(comment: Comment): Observable<Comment> { - return this.http.post<Comment>(this.commentsUrl, comment, httpOptions).pipe( + const url = this.apiBaseUrl + this.commentsUrl + '/'; + return this.http.post<Comment>(url, { + roomId: comment.roomId, subject: comment.subject, body: comment.body, read: false, creationTimestamp: this.getDate() + }, httpOptions).pipe( tap (_ => ''), catchError(this.handleError<Comment>('addComment')) ); } - deleteComment(comment: Comment): Observable<Comment> { - const url = `${this.commentsUrl}/${comment.id}`; - return this.http.delete<Comment>(url, httpOptions).pipe( - tap (_ => ''), - catchError(this.handleError<Comment>('deleteComment')) + getComment(id: string): Observable<Comment> { + const url = `${this.apiBaseUrl}${this.commentsUrl}/${id}` + '/'; + return this.http.get<Comment>(url).pipe( + catchError(this.handleError<Comment>(`getComment id=${id}`)) ); } getComments(roomId: string): Observable<Comment[]> { - const url = `${this.commentsUrl}/?roomId=${roomId}`; - return this.http.get<Comment[]>(url).pipe( - tap (_ => ''), - catchError(this.handleError<Comment[]>('getComments', [])) - ); - } - - searchComments(roomId: string, userId: string): Observable<Comment[]> { - const url = `${this.commentsUrl}/?roomId=${roomId}&userId=${userId}`; - return this.http.get<Comment[]>(url).pipe( + const url = this.apiBaseUrl + this.commentsUrl + this.findCommentUrl + '/'; + return this.http.post<Comment[]>( url, { + properties: { roomId: roomId }, + externalFilters: {} + }).pipe( tap (_ => ''), catchError(this.handleError<Comment[]>('getComments', [])) ); } updateComment(comment: Comment): Observable<any> { - return this.http.put(this.commentsUrl, comment, httpOptions).pipe( + const url = `${this.apiBaseUrl}${this.commentsUrl}/${comment.id} + '/'`; + return this.http.put(url, { + ownerId: this.authService.getUser().id, + roomId: comment.roomId, subject: comment.subject, body: comment.body, read: !comment.read, + }, httpOptions).pipe( tap(_ => ''), catchError(this.handleError<any>('updateComment')) ); } + + deleteComment(comment: Comment): Observable<Comment> { + const url = `${this.apiBaseUrl}${this.commentsUrl}/${comment.id}` + '/'; + return this.http.delete<Comment>(url, httpOptions).pipe( + tap (_ => ''), + catchError(this.handleError<Comment>('deleteComment')) + ); + } + + private getDate(): any { + const date = new Date(Date.now()); + return date.getTime(); + } }