Skip to content

Commit

Permalink
Merge pull request #52 from UdL-EPS-SoftArch/delete-rooms
Browse files Browse the repository at this point in the history
Delete rooms
  • Loading branch information
samplecode17 authored Dec 15, 2024
2 parents b4e767f + 1ee6538 commit 9111f5f
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class ApartmentDeleteComponent implements OnInit {
}

onUnauthorised(): void {
this.errorMessageService.showErrorMessage('You are not authorized to create an apartment');
this.errorMessageService.showErrorMessage('You are not authorized to delete an apartment');
this.router.navigate(['/apartments']);
}
}
2 changes: 2 additions & 0 deletions src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { VisitStatusComponent } from './visit/visit-status/visit-status.componen
import { VisitCancelComponent } from './visit/visit-cancel/visit-cancel.component';
import { RoomListComponent } from './room/room-list/room-list.component';
import { RoomCreateComponent } from './room/room-create/room-create.component';
import {RoomDeleteComponent} from './room/room-delete/room-delete.component';


const routes: Routes = [
Expand All @@ -39,6 +40,7 @@ const routes: Routes = [
{ path: 'visit/:id/cancel', component:VisitCancelComponent},
{ path: 'rooms', component: RoomListComponent},
{ path: 'room/create', component: RoomCreateComponent},
{ path: 'room/:id/delete', component: RoomDeleteComponent},
{ path: '404', component: NotFoundComponent},
{ path: '', redirectTo: 'about', pathMatch: 'full'}
];
Expand Down
Empty file.
1 change: 1 addition & 0 deletions src/app/room/room-delete/room-delete.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>room-delete works!</p>
23 changes: 23 additions & 0 deletions src/app/room/room-delete/room-delete.component.spec.ts
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();
});
});
83 changes: 83 additions & 0 deletions src/app/room/room-delete/room-delete.component.ts
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']);
}
}

0 comments on commit 9111f5f

Please sign in to comment.