From 62183df4b5fd476e33c842974f8677bac951c1f9 Mon Sep 17 00:00:00 2001 From: quimsb03 Date: Sun, 15 Dec 2024 15:16:28 +0100 Subject: [PATCH 1/6] createt room-updatet structure --- src/app/app-routing.module.ts | 4 +- .../room-update/room-update.component.css | 0 .../room-update/room-update.component.html | 57 ++++++++ .../room-update/room-update.component.spec.ts | 23 ++++ .../room/room-update/room-update.component.ts | 124 ++++++++++++++++++ 5 files changed, 207 insertions(+), 1 deletion(-) create mode 100644 src/app/room/room-update/room-update.component.css create mode 100644 src/app/room/room-update/room-update.component.html create mode 100644 src/app/room/room-update/room-update.component.spec.ts create mode 100644 src/app/room/room-update/room-update.component.ts diff --git a/src/app/app-routing.module.ts b/src/app/app-routing.module.ts index a18bd7b..3e8a7fc 100644 --- a/src/app/app-routing.module.ts +++ b/src/app/app-routing.module.ts @@ -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 {RoomUpdateComponent} from './room/room-update/room-update.component'; const routes: Routes = [ @@ -40,7 +41,8 @@ const routes: Routes = [ { path: 'rooms', component: RoomListComponent}, { path: 'room/create', component: RoomCreateComponent}, { path: '404', component: NotFoundComponent}, - { path: '', redirectTo: 'about', pathMatch: 'full'} + { path: '', redirectTo: 'about', pathMatch: 'full'}, + { path: 'room/update/:id', component: RoomUpdateComponent }, ]; @NgModule({ diff --git a/src/app/room/room-update/room-update.component.css b/src/app/room/room-update/room-update.component.css new file mode 100644 index 0000000..e69de29 diff --git a/src/app/room/room-update/room-update.component.html b/src/app/room/room-update/room-update.component.html new file mode 100644 index 0000000..f189bf4 --- /dev/null +++ b/src/app/room/room-update/room-update.component.html @@ -0,0 +1,57 @@ +
+

Update Room

+ +
+
+ Loading... +
+
+ + + +
+
+ + +
+ Surface is required and must be at least 0. +
+
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ + + +
+
+ +
+ +
diff --git a/src/app/room/room-update/room-update.component.spec.ts b/src/app/room/room-update/room-update.component.spec.ts new file mode 100644 index 0000000..916eb9d --- /dev/null +++ b/src/app/room/room-update/room-update.component.spec.ts @@ -0,0 +1,23 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { RoomUpdateComponent } from './room-update.component'; + +describe('RoomUpdateComponent', () => { + let component: RoomUpdateComponent; + let fixture: ComponentFixture; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + imports: [RoomUpdateComponent] + }) + .compileComponents(); + + fixture = TestBed.createComponent(RoomUpdateComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/src/app/room/room-update/room-update.component.ts b/src/app/room/room-update/room-update.component.ts new file mode 100644 index 0000000..4fb6ba7 --- /dev/null +++ b/src/app/room/room-update/room-update.component.ts @@ -0,0 +1,124 @@ +import { Component, OnInit } from '@angular/core'; +import { + FormBuilder, + FormControl, + FormGroup, + FormsModule, + ReactiveFormsModule, + Validators, +} from '@angular/forms'; +import { ActivatedRoute, Router } from '@angular/router'; +import { RoomService } from '../room.service'; +import { Room } from '../room'; +import { AuthenticationBasicService } from '../../login-basic/authentication-basic.service'; +import { ErrorMessageService } from '../../error-handler/error-message.service'; +import { CommonModule } from '@angular/common'; + +@Component({ + selector: 'app-room-update', + standalone: true, + imports: [FormsModule, CommonModule, ReactiveFormsModule], + templateUrl: './room-update.component.html', + styleUrls: ['./room-update.component.css'] +}) +export class RoomUpdateComponent implements OnInit { + public room: Room = new Room(); + public roomForm: FormGroup = new FormGroup({}); + public errorFetchMsg: string = ''; + public roomId: string = ''; + public isLoading: boolean = false; + public isAuthorized: boolean = false; + + constructor( + private formBuilder: FormBuilder, + private roomService: RoomService, + private router: Router, + private activatedRoute: ActivatedRoute, + private errorMessageService: ErrorMessageService, + private authenticationService: AuthenticationBasicService, + ) {} + + ngOnInit(): void { + this.isLoading = true; + this.roomId = this.activatedRoute.snapshot.paramMap.get('id') || ''; + + // Configuración inicial del formulario + this.roomForm = this.formBuilder.group({ + surface: new FormControl('', { validators: [Validators.required, Validators.min(0)] }), + isOccupied: new FormControl(false), + hasWindow: new FormControl(false), + hasDesk: new FormControl(false), + hasBed: new FormControl(false), + apartName: new FormControl({ value: '', disabled: true }, { validators: [Validators.required] }), + }); + + // Cargar la habitación desde el servicio + this.roomService + .findById(this.roomId) + .subscribe( + (_room) => { + this.room = _room; + this.setUpForm(); + this.isAuthorized = this.isAuthorised(); + if (!this.isAuthorized) { + this.onUnauthorised(); + } + this.isLoading = false; + }, + (error) => { + this.errorFetchMsg = error.message; + this.isLoading = false; + } + ); + } + + setUpForm(): void { + this.roomForm.setValue({ + surface: this.room.surface || 0, + isOccupied: this.room.isOccupied || false, + hasWindow: this.room.hasWindow || false, + hasDesk: this.room.hasDesk || false, + hasBed: this.room.hasBed || false, + apartName: this.room.apart?.name || '', // Asigna el nombre del apartamento + }); + } + + private isAuthorised(): boolean { + const userRoles = this.authenticationService.getCurrentUser()?.getRoles() || []; + return userRoles.includes('admin') || userRoles.includes('owner'); + } + + onSubmit(): void { + if (!this.isAuthorized) { + this.onUnauthorised(); + return; + } + if (this.roomForm.invalid) { + this.errorMessageService.showErrorMessage('Invalid form'); + return; + } + + // Obtener los valores del formulario, excluyendo los campos deshabilitados + const updatedRoomData = this.roomForm.getRawValue(); + delete updatedRoomData.apartName; // Excluir el nombre del apartamento + + // Asignar los valores actualizados al objeto `room` + Object.assign(this.room, updatedRoomData); + + this.roomService.updateResource(this.room).subscribe( + () => this.router.navigate(['/rooms']), + () => { + this.errorMessageService.showErrorMessage('Failed to update room'); + } + ); + } + + onUnauthorised(): void { + this.errorMessageService.showErrorMessage('You are not authorized to update this room.'); + this.router.navigate(['/rooms']); + } + + onCancel(): void { + this.router.navigate(['/rooms']); + } +} From fc5935051ba310f787456d718c23340f7ec05fb5 Mon Sep 17 00:00:00 2001 From: quimsb03 Date: Sun, 15 Dec 2024 16:10:05 +0100 Subject: [PATCH 2/6] delete apartment form form --- src/app/room/room-update/room-update.component.html | 6 ------ src/app/room/room-update/room-update.component.ts | 3 --- 2 files changed, 9 deletions(-) diff --git a/src/app/room/room-update/room-update.component.html b/src/app/room/room-update/room-update.component.html index f189bf4..3033ae6 100644 --- a/src/app/room/room-update/room-update.component.html +++ b/src/app/room/room-update/room-update.component.html @@ -39,12 +39,6 @@

Update Room

- -
- - -
- diff --git a/src/app/room/room-update/room-update.component.ts b/src/app/room/room-update/room-update.component.ts index ff3f464..bf9afb6 100644 --- a/src/app/room/room-update/room-update.component.ts +++ b/src/app/room/room-update/room-update.component.ts @@ -30,7 +30,6 @@ export class RoomUpdateComponent implements OnInit { public roomId: string = ''; public isLoading: boolean = false; public isAuthorized: boolean = false; - public apartment: Apartment = new Apartment(); constructor( private formBuilder: FormBuilder, @@ -53,7 +52,6 @@ export class RoomUpdateComponent implements OnInit { hasWindow: new FormControl(false), hasDesk: new FormControl(false), hasBed: new FormControl(false), - apartName: new FormControl({ value: '', disabled: true }, { validators: [Validators.required] }), }); // Cargar la habitación desde el servicio @@ -83,7 +81,6 @@ export class RoomUpdateComponent implements OnInit { hasWindow: this.room.hasWindow || false, hasDesk: this.room.hasDesk || false, hasBed: this.room.hasBed || false, - apartName: this.room.apart || '', // Asigna el nombre del apartamento }); } From da3ed81789fc032ab45f9548caa68aa26a37147f Mon Sep 17 00:00:00 2001 From: quimsb03 Date: Sun, 15 Dec 2024 16:18:08 +0100 Subject: [PATCH 3/6] add edit button at room list --- src/app/room/room-list/room-list.component.html | 4 +++- src/app/room/room-list/room-list.component.ts | 8 ++++++++ src/app/room/room.ts | 1 - 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/app/room/room-list/room-list.component.html b/src/app/room/room-list/room-list.component.html index 00d51ee..6f8b71b 100644 --- a/src/app/room/room-list/room-list.component.html +++ b/src/app/room/room-list/room-list.component.html @@ -24,6 +24,8 @@

List of Rooms

{{ room.isOccupied }} {{ room.surface}} + + @@ -32,4 +34,4 @@

List of Rooms

- \ No newline at end of file + diff --git a/src/app/room/room-list/room-list.component.ts b/src/app/room/room-list/room-list.component.ts index 79bc9c2..df10255 100644 --- a/src/app/room/room-list/room-list.component.ts +++ b/src/app/room/room-list/room-list.component.ts @@ -116,4 +116,12 @@ export class RoomListComponent implements OnInit { console.error('Invalid room ID'); } } + updateRoom(roomId: string): void { + if (roomId) { + console.log('Room ID:', roomId); + this.router.navigate([`/room/update/${roomId}`]); + } else { + console.error('Invalid room ID'); + } + } } diff --git a/src/app/room/room.ts b/src/app/room/room.ts index c970b69..d08afff 100644 --- a/src/app/room/room.ts +++ b/src/app/room/room.ts @@ -18,7 +18,6 @@ export class Room extends Resource { Object.assign(this, values); } - getRoomIdFromLinks(): string { if (this._links?.self?.href) { return this._links.self.href.split('/').pop() || ''; // Último segmento From 6bd73deb6684674abebb0f8bbc7fd2e93282436e Mon Sep 17 00:00:00 2001 From: quimsb03 Date: Sun, 15 Dec 2024 16:21:32 +0100 Subject: [PATCH 4/6] delete unused import --- src/app/room/room-update/room-update.component.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/app/room/room-update/room-update.component.ts b/src/app/room/room-update/room-update.component.ts index bf9afb6..cefe507 100644 --- a/src/app/room/room-update/room-update.component.ts +++ b/src/app/room/room-update/room-update.component.ts @@ -13,8 +13,6 @@ import { Room } from '../room'; import { AuthenticationBasicService } from '../../login-basic/authentication-basic.service'; import { ErrorMessageService } from '../../error-handler/error-message.service'; import { CommonModule } from '@angular/common'; -import {ApartmentService} from '../../apartment/apartment.service'; -import {Apartment} from '../../apartment/apartment'; @Component({ selector: 'app-room-update', @@ -38,7 +36,6 @@ export class RoomUpdateComponent implements OnInit { private activatedRoute: ActivatedRoute, private errorMessageService: ErrorMessageService, private authenticationService: AuthenticationBasicService, - private apartmentServices: ApartmentService, ) {} ngOnInit(): void { From 2f3e25fe4f1af5e187009e2ba3e14328d0f1886e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roberto=20Garc=C3=ADa?= Date: Mon, 16 Dec 2024 09:10:50 +0100 Subject: [PATCH 5/6] Update app-routing.module.ts Remove unused import RoomRemoveComponent --- src/app/app-routing.module.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/app/app-routing.module.ts b/src/app/app-routing.module.ts index 7182bd4..5c31fe1 100644 --- a/src/app/app-routing.module.ts +++ b/src/app/app-routing.module.ts @@ -20,7 +20,6 @@ import { VisitCancelComponent } from './visit/visit-cancel/visit-cancel.componen import { RoomListComponent } from './room/room-list/room-list.component'; import { RoomCreateComponent } from './room/room-create/room-create.component'; import { RoomUpdateComponent } from './room/room-update/room-update.component'; -import { RoomDeleteComponent } from './room/room-delete/room-delete.component'; import { MyAdvertisementComponent } from './advertisement/my-advertisement-list/my-advertisement.component'; import { ImageCreateComponent } from './image/image-create/image.component'; From 24b9852dcb533e397ab2503f44c823d6d0a012fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roberto=20Garc=C3=ADa?= Date: Mon, 16 Dec 2024 11:07:25 +0100 Subject: [PATCH 6/6] Retrieve room apartment to keep it after update --- src/app/room/room-update/room-update.component.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/app/room/room-update/room-update.component.ts b/src/app/room/room-update/room-update.component.ts index cefe507..567d2c9 100644 --- a/src/app/room/room-update/room-update.component.ts +++ b/src/app/room/room-update/room-update.component.ts @@ -13,6 +13,7 @@ import { Room } from '../room'; import { AuthenticationBasicService } from '../../login-basic/authentication-basic.service'; import { ErrorMessageService } from '../../error-handler/error-message.service'; import { CommonModule } from '@angular/common'; +import { Apartment } from '../../apartment/apartment'; @Component({ selector: 'app-room-update', @@ -57,6 +58,8 @@ export class RoomUpdateComponent implements OnInit { .subscribe( (_room) => { this.room = _room; + this.room.getRelation('apart').subscribe( + (apartment) => this.room.apart = apartment); this.setUpForm(); this.isAuthorized = this.isAuthorised(); if (!this.isAuthorized) {