From 25e36ed8f190ba4a19fda3771c739c49e5c15970 Mon Sep 17 00:00:00 2001 From: Hagen <hagen.dressler@mni.thm.de> Date: Thu, 22 Mar 2018 03:24:33 +0100 Subject: [PATCH] Edit service comment (implement api connection) --- src/app/services/http/comment.service.ts | 60 +++++++++++++++--------- 1 file changed, 39 insertions(+), 21 deletions(-) diff --git a/src/app/services/http/comment.service.ts b/src/app/services/http/comment.service.ts index f46b26fbd..2bd824827 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(); + } } -- GitLab