From 31d989bda956fc0e67fec0eb9464a2aab226b5c5 Mon Sep 17 00:00:00 2001
From: Lukas Kimpel <lukas.kimpel@mni.thm.de>
Date: Tue, 20 Mar 2018 11:14:19 +0100
Subject: [PATCH] Refactor to new User class Fix room quick joining Refactor to
 new api routes

---
 src/app/authentication.guard.ts            |  2 +-
 src/app/authentication.interceptor.ts      |  2 --
 src/app/login/login.component.ts           | 11 ++---------
 src/app/room-list/room-list.component.html |  2 +-
 src/app/room-list/room-list.component.ts   | 18 +++++++++++++-----
 src/app/room.service.ts                    | 16 ++++++++--------
 6 files changed, 25 insertions(+), 26 deletions(-)

diff --git a/src/app/authentication.guard.ts b/src/app/authentication.guard.ts
index 9009340b1..4e4f00407 100644
--- a/src/app/authentication.guard.ts
+++ b/src/app/authentication.guard.ts
@@ -25,7 +25,7 @@ export class AuthenticationGuard implements CanActivate {
     // Allow access when user is logged in AND
     // the route doesn't require a specific role OR
     // the user's role is one of the required roles
-    if (user && (!requiredRoles || requiredRoles.includes(user.userRole))) {
+    if (user && (!requiredRoles || requiredRoles.includes(user.role))) {
       return true;
     }
 
diff --git a/src/app/authentication.interceptor.ts b/src/app/authentication.interceptor.ts
index 98b085c4e..071cba6ae 100644
--- a/src/app/authentication.interceptor.ts
+++ b/src/app/authentication.interceptor.ts
@@ -8,8 +8,6 @@ import { Observable } from 'rxjs/Observable';
 
 const AUTH_HEADER_KEY = 'Arsnova-Auth-Token';
 
-const AUTH_HEADER_KEY = 'Arsnova-Auth-Token';
-
 @Injectable()
 export class AuthenticationInterceptor implements HttpInterceptor {
 
diff --git a/src/app/login/login.component.ts b/src/app/login/login.component.ts
index f4156f3e1..cc6bcb187 100644
--- a/src/app/login/login.component.ts
+++ b/src/app/login/login.component.ts
@@ -45,7 +45,7 @@ export class LoginComponent implements OnInit {
 
     if (!this.usernameFormControl.hasError('required') && !this.usernameFormControl.hasError('email') &&
       !this.passwordFormControl.hasError('required')) {
-      this.authenticationService.login(username, password).subscribe(loginSuccessful => this.checkLogin(loginSuccessful));
+      this.authenticationService.login(username, password, this.role).subscribe(loginSuccessful => this.checkLogin(loginSuccessful));
     } else {
       this.notificationService.show('Please fit the requirements shown above.');
     }
@@ -55,15 +55,8 @@ export class LoginComponent implements OnInit {
     this.authenticationService.guestLogin().subscribe(loginSuccessful => this.checkLogin(loginSuccessful));
   }
 
-  private checkLogin(loginSuccessful: ClientAuthentication) {
+  private checkLogin(loginSuccessful: boolean) {
     if (loginSuccessful) {
-      const user: User = new User(
-        loginSuccessful.userId,
-        loginSuccessful.loginId,
-        loginSuccessful.authProvider,
-        loginSuccessful.token,
-        this.role);
-      this.authenticationService.setUser(user);
       this.notificationService.show('Login successful!');
       if (this.role === UserRole.CREATOR) {
         this.router.navigate(['creator']);
diff --git a/src/app/room-list/room-list.component.html b/src/app/room-list/room-list.component.html
index 869db2914..2513764e9 100644
--- a/src/app/room-list/room-list.component.html
+++ b/src/app/room-list/room-list.component.html
@@ -4,7 +4,7 @@
   </div>
   <mat-expansion-panel *ngFor="let room of rooms">
     <mat-expansion-panel-header>
-      <button mat-button color="primary" routerLink="/{{ baseUrl }}/room/{{ room.id }}">
+      <button mat-button color="primary" routerLink="/{{ baseUrl }}/room/{{ room.shortId }}">
         <mat-icon>send</mat-icon>
       </button>
       <mat-panel-title>
diff --git a/src/app/room-list/room-list.component.ts b/src/app/room-list/room-list.component.ts
index 9e0dd6e4a..0181fad40 100644
--- a/src/app/room-list/room-list.component.ts
+++ b/src/app/room-list/room-list.component.ts
@@ -34,10 +34,18 @@ export class RoomListComponent implements OnInit {
   }
 
   getRooms(): void {
-    this.roomService.getRooms().subscribe(rooms => {
-      this.rooms = rooms;
-      this.closedRooms = this.rooms.filter(room => room.closed);
-      this.isLoading = false;
-    });
+    if (this.authenticationService.getRole() === UserRole.CREATOR) {
+      this.roomService.getCreatorRooms().subscribe(rooms => {
+        this.rooms = rooms;
+        this.closedRooms = this.rooms.filter(room => room.closed);
+        this.isLoading = false;
+      });
+    } else if (this.authenticationService.getRole() === UserRole.PARTICIPANT) {
+      this.roomService.getParticipantRooms().subscribe(rooms => {
+        this.rooms = rooms;
+        this.closedRooms = this.rooms.filter(room => room.closed);
+        this.isLoading = false;
+      });
+    }
   }
 }
diff --git a/src/app/room.service.ts b/src/app/room.service.ts
index 31582ab76..45a09f718 100644
--- a/src/app/room.service.ts
+++ b/src/app/room.service.ts
@@ -26,7 +26,7 @@ export class RoomService extends ErrorHandlingService {
   getCreatorRooms(): Observable<Room[]> {
     const url = this.apiBaseUrl + this.roomsUrl + this.findRoomsUrl;
     return this.http.post<Room[]>(url, {
-      properties: { ownerId: this.authService.getUser().userId },
+      properties: { ownerId: this.authService.getUser().id },
       externalFilters: {}
     }).pipe(
       tap(_ => ''),
@@ -38,9 +38,9 @@ export class RoomService extends ErrorHandlingService {
     const url = this.apiBaseUrl + this.roomsUrl + this.findRoomsUrl;
     return this.http.post<Room[]>(url, {
       properties: {},
-      externalFilters: { inHistoryOfUserId: this.authService.getUser().userId }
+      externalFilters: { inHistoryOfUserId: this.authService.getUser().id }
     }).pipe(
-      tap(_ => ''),
+      tap(() => ''),
       catchError(this.handleError('getRooms', []))
     );
   }
@@ -48,7 +48,7 @@ export class RoomService extends ErrorHandlingService {
   addRoom(room: Room): Observable<Room> {
     const connectionUrl = this.apiBaseUrl + this.roomsUrl + '/';
     return this.http.post<Room>(connectionUrl, {
-      ownerId: this.authService.getUser().userId,
+      ownerId: this.authService.getUser().id,
       abbreviation: room.abbreviation, name: room.name, closed: room.closed, description: room.description
     }, httpOptions);
   }
@@ -61,7 +61,7 @@ export class RoomService extends ErrorHandlingService {
   }
 
   addToHistory(roomId: string): void {
-    const connectionUrl = `${ this.apiBaseUrl }${ this.userUrl }/${this.authService.getUser().userId}/roomHistory`;
+    const connectionUrl = `${ this.apiBaseUrl }${ this.userUrl }/${this.authService.getUser().id}/roomHistory`;
     this.http.post(connectionUrl, { roomId: roomId, lastVisit: this.joinDate.getTime() }, httpOptions).subscribe(r => console.log(r));
   }
 
@@ -75,10 +75,10 @@ export class RoomService extends ErrorHandlingService {
   updateRoom(room: Room): Observable<Room> {
     const connectionUrl = `${this.apiBaseUrl}${this.roomsUrl}/~${room.shortId}`;
     return this.http.put(connectionUrl, {
-      ownerId: this.authService.getUser().userId,
+      ownerId: this.authService.getUser().id,
       abbreviation: room.abbreviation, name: room.name, description: room.description
     }, httpOptions).pipe(
-      tap(_ => ''),
+      tap(() => ''),
       catchError(this.handleError<any>('updateRoom'))
     );
   }
@@ -86,7 +86,7 @@ export class RoomService extends ErrorHandlingService {
   deleteRoom(room: Room): Observable<Room> {
     const connectionUrl = `${this.apiBaseUrl}${this.roomsUrl}/${room.id}`;
     return this.http.delete<Room>(connectionUrl, httpOptions).pipe(
-      tap(_ => ''),
+      tap(() => ''),
       catchError(this.handleError<Room>('deleteRoom'))
     );
   }
-- 
GitLab