From a3c305133fa2b485b3a2c2c4b8da907b592ee91b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tom=20K=C3=A4sler?= <tom.kaesler@mni.thm.de>
Date: Mon, 8 Apr 2019 14:27:23 +0200
Subject: [PATCH] Add extension to room

move comment threshold to room extension settings for comment
fix service to expect room model on new room
add default constructor to room
---
 .../room-edit/room-edit.component.html        |  2 +-
 .../_dialogs/room-edit/room-edit.component.ts | 17 +++++++++++--
 .../room-creator-page.component.ts            | 19 ++++++++------
 .../room-create/room-create.component.ts      | 15 ++++++-----
 .../comment-list/comment-list.component.ts    | 12 +++++++--
 src/app/models/room.ts                        | 25 ++++++++++++++++++-
 src/app/services/http/room.service.ts         |  9 +++----
 7 files changed, 75 insertions(+), 24 deletions(-)

diff --git a/src/app/components/creator/_dialogs/room-edit/room-edit.component.html b/src/app/components/creator/_dialogs/room-edit/room-edit.component.html
index e0fc69811..6654079e0 100644
--- a/src/app/components/creator/_dialogs/room-edit/room-edit.component.html
+++ b/src/app/components/creator/_dialogs/room-edit/room-edit.component.html
@@ -19,7 +19,7 @@
     <div fxLayout="row">
       <mat-label>Threshold for visible comments:</mat-label>
       <span class="fill-remaining-space"></span>
-      <mat-label>{{editRoom.commentThreshold | number}}</mat-label>
+      <mat-label>{{commentThreshold | number}}</mat-label>
     </div>
       <mat-slider id="commentSlider" min="-50" max="50" step="1" value="0"
                   color="accent" [(ngModel)]="editRoom.commentThreshold" (input)="onSliderChange($event)"></mat-slider>
diff --git a/src/app/components/creator/_dialogs/room-edit/room-edit.component.ts b/src/app/components/creator/_dialogs/room-edit/room-edit.component.ts
index f99b317cf..2e57892c6 100644
--- a/src/app/components/creator/_dialogs/room-edit/room-edit.component.ts
+++ b/src/app/components/creator/_dialogs/room-edit/room-edit.component.ts
@@ -2,6 +2,7 @@ import { Component, Inject, OnInit } from '@angular/core';
 import { Room } from '../../../../models/room';
 import { RoomCreateComponent } from '../../../shared/_dialogs/room-create/room-create.component';
 import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material';
+import { TSMap } from 'typescript-map';
 
 @Component({
   selector: 'app-room-edit',
@@ -10,6 +11,7 @@ import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material';
 })
 export class RoomEditComponent implements OnInit {
   editRoom: Room;
+  commentThreshold: number;
 
   constructor(public dialogRef: MatDialogRef<RoomCreateComponent>,
               @Inject(MAT_DIALOG_DATA) public data: any) {
@@ -20,10 +22,21 @@ export class RoomEditComponent implements OnInit {
   }
 
   ngOnInit() {
-
+    // ToDo: implement a more robust way
+    // ToDo: have a default comment threshold defined in config files
+    if (this.editRoom.extensions != null) {
+      const commentExtension = this.editRoom.extensions.get('comments');
+      if ((commentExtension != null) && commentExtension.get('threshold') != null) {
+        this.commentThreshold = commentExtension.get('threshold');
+      } else {
+        this.commentThreshold = -10;
+      }
+    } else {
+      this.commentThreshold = -10;
+    }
   }
 
   onSliderChange(event: any) {
-    this.editRoom.commentThreshold = event.value;
+    this.commentThreshold = event.value;
   }
 }
diff --git a/src/app/components/creator/room-creator-page/room-creator-page.component.ts b/src/app/components/creator/room-creator-page/room-creator-page.component.ts
index 895ec931f..a0c050507 100644
--- a/src/app/components/creator/room-creator-page/room-creator-page.component.ts
+++ b/src/app/components/creator/room-creator-page/room-creator-page.component.ts
@@ -10,6 +10,7 @@ import { RoomDeleteComponent } from '../_dialogs/room-delete/room-delete.compone
 import { RoomEditComponent } from '../_dialogs/room-edit/room-edit.component';
 import { TranslateService } from '@ngx-translate/core';
 import { LanguageService } from '../../../services/util/language.service';
+import { TSMap } from 'typescript-map';
 
 @Component({
   selector: 'app-room-creator-page',
@@ -20,6 +21,8 @@ export class RoomCreatorPageComponent extends RoomPageComponent implements OnIni
   room: Room;
   updRoom: Room;
   themeClass = localStorage.getItem('classNameOfTheme');
+  commentThreshold: number;
+  updCommentThreshold: number;
 
   constructor(protected roomService: RoomService,
               protected notification: NotificationService,
@@ -45,14 +48,19 @@ export class RoomCreatorPageComponent extends RoomPageComponent implements OnIni
   updateRoom(): void {
     if ((this.updRoom.name === this.room.name) &&
       (this.updRoom.description === this.room.description) &&
-      (this.updRoom.commentThreshold === this.room.commentThreshold)
+      (this.commentThreshold === this.updCommentThreshold)
     ) {
       this.notification.show('There were no changes');
       return;
     } else {
       this.room.name = this.updRoom.name;
       this.room.description = this.updRoom.description;
-      this.room.commentThreshold = this.updRoom.commentThreshold;
+      if (this.room.extensions === null) {
+        this.room.extensions = new TSMap();
+      }
+      const commentExtension: TSMap<string, any> = new TSMap();
+      commentExtension.set('threshold', this.updCommentThreshold);
+      this.room.extensions.set('comments', commentExtension);
       this.roomService.updateRoom(this.room)
         .subscribe(() => {
           this.notification.show('Changes are made');
@@ -84,15 +92,12 @@ export class RoomCreatorPageComponent extends RoomPageComponent implements OnIni
   }
 
   showEditDialog(): void {
-    this.updRoom = new Room();
-    this.updRoom.name = this.room.name;
-    this.updRoom.shortId = this.room.shortId;
-    this.updRoom.description = this.room.description;
-    this.updRoom.commentThreshold = this.room.commentThreshold;
+    this.updRoom = this.room;
     const dialogRef = this.dialog.open(RoomEditComponent, {
       width: '400px'
     });
     dialogRef.componentInstance.editRoom = this.updRoom;
+    dialogRef.componentInstance.commentThreshold = this.updCommentThreshold;
     dialogRef.afterClosed()
       .subscribe(result => {
         if (result === 'abort') {
diff --git a/src/app/components/shared/_dialogs/room-create/room-create.component.ts b/src/app/components/shared/_dialogs/room-create/room-create.component.ts
index d0ad04a12..2c277d032 100644
--- a/src/app/components/shared/_dialogs/room-create/room-create.component.ts
+++ b/src/app/components/shared/_dialogs/room-create/room-create.component.ts
@@ -6,6 +6,7 @@ import { NotificationService } from '../../../../services/util/notification.serv
 import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material';
 import { ContentService } from '../../../../services/http/content.service';
 import { TranslateService } from '@ngx-translate/core';
+import { TSMap } from 'typescript-map';
 
 @Component({
   selector: 'app-room-create',
@@ -46,12 +47,14 @@ export class RoomCreateComponent implements OnInit {
       this.emptyInputs = true;
       return;
     }
-    this.roomService.addRoom({
-      name: longRoomName,
-      abbreviation: '00000000',
-      description: description,
-      commentThreshold: commentThreshold
-    } as Room).subscribe(room => {
+    const newRoom = new Room();
+    newRoom.name = longRoomName;
+    newRoom.abbreviation = '00000000';
+    newRoom.description = description;
+    const commentExtension: TSMap<string, any> = new TSMap();
+    commentExtension.set('commentThreshold', commentThreshold);
+    newRoom.extensions.set('comments', commentExtension);
+    this.roomService.addRoom(newRoom).subscribe(room => {
       this.room = room;
       let msg1: string;
       let msg2: string;
diff --git a/src/app/components/shared/comment-list/comment-list.component.ts b/src/app/components/shared/comment-list/comment-list.component.ts
index fc55fb67a..46965aa3b 100644
--- a/src/app/components/shared/comment-list/comment-list.component.ts
+++ b/src/app/components/shared/comment-list/comment-list.component.ts
@@ -73,10 +73,18 @@ export class CommentListComponent implements OnInit {
   }
 
   getCommentsCreator(): Comment[] {
+    // ToDo: get a default comment threshold from config settings file
+    let commentThreshold = -10;
+    if (
+      (this.room.extensions.get('comments') != null) &&
+      (this.room.extensions.get('comments').get('commentThreshold'))
+    ) {
+      commentThreshold = this.room.extensions.get('comments').get('commentThreshold');
+    }
     if (this.hideCommentsList) {
-      return this.filteredComments.filter( x => x.score >= this.room.commentThreshold );
+      return this.filteredComments.filter( x => x.score >= commentThreshold );
     } else {
-      return  this.comments.filter( x => x.score >= this.room.commentThreshold );
+      return  this.comments.filter( x => x.score >= commentThreshold );
     }
   }
 
diff --git a/src/app/models/room.ts b/src/app/models/room.ts
index a63578334..1ce435e6e 100644
--- a/src/app/models/room.ts
+++ b/src/app/models/room.ts
@@ -1,13 +1,36 @@
 import { ContentGroup } from './content-group';
+import { TSMap } from 'typescript-map';
 
 export class Room {
   id: string;
   revision: string;
+  ownerId: string;
   shortId: string;
   abbreviation: string;
   name: string;
   description: string;
   closed: boolean;
-  commentThreshold: number;
   contentGroups: ContentGroup[];
+  extensions: TSMap<string, TSMap<string, any>>;
+
+  constructor(
+    ownerId: string = '',
+    shortId: string = '',
+    abbreviation: string = '',
+    name: string = '',
+    description: string = '',
+    closed: boolean = false,
+    contentGroups: ContentGroup[] = [],
+    extensions: TSMap<string, TSMap<string, any>> = new TSMap()
+  ) {
+    this.id = '',
+    this.ownerId = ownerId;
+    this.shortId = shortId;
+    this.abbreviation = abbreviation;
+    this.name = name,
+    this.description = description;
+    this.closed = closed;
+    this.contentGroups = contentGroups;
+    this.extensions = extensions;
+  }
 }
diff --git a/src/app/services/http/room.service.ts b/src/app/services/http/room.service.ts
index b1e9903cd..544f7848d 100644
--- a/src/app/services/http/room.service.ts
+++ b/src/app/services/http/room.service.ts
@@ -48,12 +48,11 @@ export class RoomService extends BaseHttpService {
   }
 
   addRoom(room: Room): Observable<Room> {
+    delete room.id;
+    delete room.revision;
     const connectionUrl = this.apiUrl.base + this.apiUrl.rooms + '/';
-    return this.http.post<Room>(connectionUrl, {
-      ownerId: this.authService.getUser().id,
-      abbreviation: room.abbreviation, name: room.name, closed: room.closed, description: room.description,
-      commentThreshold: room.commentThreshold
-    }, httpOptions);
+    room.ownerId = this.authService.getUser().id;
+    return this.http.post<Room>(connectionUrl, room, httpOptions);
   }
 
   getRoom(id: string): Observable<Room> {
-- 
GitLab