-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from UdL-EPS-SoftArch/delete-rooms
Delete rooms
- Loading branch information
Showing
6 changed files
with
110 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<p>room-delete works!</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
||
import { RoomDeleteComponent } from './room-delete.component'; | ||
|
||
describe('RoomDeleteComponent', () => { | ||
let component: RoomDeleteComponent; | ||
let fixture: ComponentFixture<RoomDeleteComponent>; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
imports: [RoomDeleteComponent] | ||
}) | ||
.compileComponents(); | ||
|
||
fixture = TestBed.createComponent(RoomDeleteComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import {Component, OnInit} from '@angular/core'; | ||
import {User} from '../../login-basic/user'; | ||
import {ActivatedRoute, Router} from '@angular/router'; | ||
import {RoomService} from '../room.service'; | ||
import {ErrorMessageService} from '../../error-handler/error-message.service'; | ||
import {AuthenticationBasicService} from '../../login-basic/authentication-basic.service'; | ||
import {Room} from '../room'; | ||
|
||
@Component({ | ||
selector: 'app-room-delete', | ||
standalone: true, | ||
imports: [], | ||
templateUrl: './room-delete.component.html', | ||
styleUrl: './room-delete.component.css' | ||
}) | ||
export class RoomDeleteComponent implements OnInit { | ||
public roomId: string = ''; | ||
public user: User | null = null; | ||
public isAuthorized: boolean = false; | ||
|
||
constructor( | ||
private router: Router, | ||
private activatedRoute: ActivatedRoute, | ||
private roomService: RoomService, | ||
private errorMessageService: ErrorMessageService, | ||
private authenticationService: AuthenticationBasicService, | ||
) {} | ||
|
||
ngOnInit() { | ||
this.roomId = this.activatedRoute.snapshot.paramMap.get('id') || ''; | ||
|
||
try { | ||
this.user = this.authenticationService.getCurrentUser(); | ||
|
||
if (!this.user) { | ||
throw new Error('No user found') | ||
} | ||
|
||
this.isAuthorized = this.isAuthorised() | ||
|
||
if (this.isAuthorized) { | ||
this.removeRoom(); | ||
|
||
} else { | ||
this.onUnauthorised(); | ||
} | ||
} catch (error) { | ||
console.error('Error during user authentication:', error); | ||
this.errorMessageService.showErrorMessage('Authentication error. Please log in again.'); | ||
this.router.navigate(['/login']); | ||
} | ||
} | ||
|
||
private removeRoom() { | ||
this.roomService.getResource(this.roomId).subscribe( | ||
(room: Room) => { | ||
this.roomService.deleteResource(room).subscribe( | ||
() => { | ||
this.router.navigate(['/room']); | ||
}, | ||
() => { | ||
this.errorMessageService.showErrorMessage('Failed to delete room. Please try again.') | ||
} | ||
); | ||
}, | ||
() => { | ||
this.errorMessageService.showErrorMessage('Failed to load room for deletion.'); | ||
} | ||
); | ||
} | ||
|
||
private isAuthorised(): boolean { | ||
if (!this.user) { | ||
return false; | ||
} | ||
return this.user && (this.user.getRoles().includes('admin') || this.user.getRoles().includes('owner')); | ||
} | ||
|
||
onUnauthorised(): void { | ||
this.errorMessageService.showErrorMessage('You are not authorized to delete a room'); | ||
this.router.navigate(['/room']); | ||
} | ||
} |