Skip to content
Snippets Groups Projects
comment.service.ts 2.39 KiB
Newer Older
Hagen Dreßler's avatar
Hagen Dreßler committed
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
Thisari Muthuwahandi's avatar
Thisari Muthuwahandi committed
import { Observable, BehaviorSubject, Subject } from 'rxjs';
import { Comment } from '../../models/comment';
import { catchError, tap } from 'rxjs/operators';
import { BaseHttpService } from './base-http.service';
const httpOptions = {
  headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};
Hagen Dreßler's avatar
Hagen Dreßler committed

@Injectable()
export class CommentService extends BaseHttpService {
  private apiUrl = {
    base: '/api',
    comment: '/comment',
    find: '/find'
  };
Thisari Muthuwahandi's avatar
Thisari Muthuwahandi committed
  exportButton = new Subject<boolean>();

Thisari Muthuwahandi's avatar
zg  
Thisari Muthuwahandi committed
  constructor(private http: HttpClient) {
  getComment(commentId: string): Observable<Comment> {
Thisari Muthuwahandi's avatar
zg  
Thisari Muthuwahandi committed
    const connectionUrl = `${this.apiUrl.base}${this.apiUrl.comment}/~${commentId}`;
Lukas Mauß's avatar
Lukas Mauß committed
    return this.http.get<Comment>(connectionUrl, httpOptions).pipe(
Thisari Muthuwahandi's avatar
zg  
Thisari Muthuwahandi committed
      tap(_ => ''),
Lukas Mauß's avatar
Lukas Mauß committed
      catchError(this.handleError<Comment>('addComment'))
    );
  }
  addComment(comment: Comment): Observable<Comment> {
Lukas Mauß's avatar
Lukas Mauß committed
    const connectionUrl = this.apiUrl.base + this.apiUrl.comment + '/';
    return this.http.post<Comment>(connectionUrl,
      { roomId: comment.roomId, body: comment.body,
        read: comment.read, creationTimestamp: comment.creationTimestamp
      }, httpOptions).pipe(
Thisari Muthuwahandi's avatar
zg  
Thisari Muthuwahandi committed
        tap(_ => ''),
        catchError(this.handleError<Comment>('addComment'))
      );
  deleteComment(commentId: string): Observable<Comment> {
Thisari Muthuwahandi's avatar
zg  
Thisari Muthuwahandi committed
    const connectionUrl = `${this.apiUrl.base + this.apiUrl.comment}/${commentId}`;
Lukas Mauß's avatar
Lukas Mauß committed
    return this.http.delete<Comment>(connectionUrl, httpOptions).pipe(
Thisari Muthuwahandi's avatar
zg  
Thisari Muthuwahandi committed
      tap(_ => ''),
      catchError(this.handleError<Comment>('deleteComment'))
    );
  getComments(roomId: string): Observable<Comment[]> {
Lukas Mauß's avatar
Lukas Mauß committed
    const connectionUrl = this.apiUrl.base + this.apiUrl.comment + this.apiUrl.find;
    return this.http.post<Comment[]>(connectionUrl, {
Lukas Mauß's avatar
Lukas Mauß committed
      properties: { roomId: roomId },
      externalFilters: {}
    }, httpOptions).pipe(
Thisari Muthuwahandi's avatar
zg  
Thisari Muthuwahandi committed
      tap(_ => ''),
      catchError(this.handleError<Comment[]>('getComments', []))
    );

  updateComment(comment: Comment): Observable<any> {
Lukas Mauß's avatar
Lukas Mauß committed
    const connectionUrl = this.apiUrl.base + this.apiUrl.comment + '/' + comment.id;
Lukas Mauß's avatar
Lukas Mauß committed
    return this.http.put(connectionUrl, comment, httpOptions).pipe(
Hagen Dreßler's avatar
Hagen Dreßler committed
      catchError(this.handleError<any>('updateComment'))
Thisari Muthuwahandi's avatar
Thisari Muthuwahandi committed

  setState(state: boolean) {
    this.exportButton.next(state);
  }
Hagen Dreßler's avatar
Hagen Dreßler committed
}