Skip to content

Commit

Permalink
Merge branch 'main' into 8-publish-advertisement
Browse files Browse the repository at this point in the history
  • Loading branch information
Troter2 authored Dec 14, 2024
2 parents 7ce0dcd + c53342e commit 5dcf9bc
Show file tree
Hide file tree
Showing 16 changed files with 595 additions and 49 deletions.
46 changes: 45 additions & 1 deletion src/app/apartment/apartment-list/apartment-list.component.html
Original file line number Diff line number Diff line change
@@ -1 +1,45 @@
<p>apartment-list works!</p>
<div class="apartment-list-container">
<h2> My apartments</h2>

<div *ngIf="apartments.length === 0" class="no-apartments">
<p>No hay apartamentos disponibles.</p>
</div>

<div *ngIf="apartments.length > 0" class="table-responsive">
<table class="table table-striped table-bordered">
<thead class="thead-dark">
<tr>
<th>ID</th>
<th>Name</th>
<th>Address</th>
<th>Floor</th>
<th>Postal Code</th>
<th>Registration date</th>
<th>Description</th>
<th>Note</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr *ngFor="let apartment of apartments">
<td>{{ apartment.getIdFromLinks() }}</td>
<td>{{ apartment.name }}</td>
<td>{{ apartment.address }}, {{ apartment.city }}, {{ apartment.country }}</td>
<td>{{ apartment.floor }}</td>
<td>{{ apartment.postalCode }}</td>
<td>{{ apartment.registrationDate | date }}</td>
<td>{{ apartment.description || 'N/A' }}</td>
<td>{{ apartment.note || 'N/A' }}</td>
<td>
<button class="deleteButton" (click)="deleteApartment(apartment.getIdFromLinks())"><span class = "deleteSpan">Delete</span></button>
</td>
<td>
<button class="updateButton" (click)="updateApartment(apartment.getIdFromLinks())"><span class = "updateSpan">Edit</span></button>
</td>
</tr>
</tbody>
</table>
<button class="createButton" (click)="createApartment()"><span class="createSpan">Create</span></button>
</div>
</div>
83 changes: 79 additions & 4 deletions src/app/apartment/apartment-list/apartment-list.component.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,87 @@
import { Component } from '@angular/core';
import { Component, OnInit } from '@angular/core';
import { Apartment } from '../apartment';
import { ApartmentService } from '../apartment.service';
import { Router } from '@angular/router';
import { CommonModule } from '@angular/common';
import { User } from '../../login-basic/user';
import { AuthenticationBasicService } from '../../login-basic/authentication-basic.service';
import { ErrorMessageService } from '../../error-handler/error-message.service';

@Component({
selector: 'app-apartment-list',
standalone: true,
imports: [],
imports: [CommonModule],
templateUrl: './apartment-list.component.html',
styleUrl: './apartment-list.component.css'
styleUrls: ['./apartment-list.component.css']
})
export class ApartmentListComponent {
export class ApartmentListComponent implements OnInit {

public apartments: Apartment[] = []; // Almacena la lista de apartamentos
public currentUser: User = new User();
public isShowed: boolean = false;

constructor(
private router: Router,
private apartmentService: ApartmentService,
private authenticationService: AuthenticationBasicService,
private errorMessageService: ErrorMessageService,
) {
}

ngOnInit(): void {
this.currentUser = this.authenticationService.getCurrentUser();
this.isShowed = this.isOwner();

if (!this.isShowed) {
this.onNotShowed();
return;
}

if (this.currentUser) {
// Llama al servicio para obtener los apartamentos del usuario actual
this.apartmentService.findByOwner(this.currentUser).subscribe({

next: (resourceCollection) => {

this.apartments = resourceCollection.resources || []; // Asegúrate de asignar un arreglo
},
error: (err) => {
console.error('Error fetching apartments:', err);
this.errorMessageService.showErrorMessage('Failed to load apartments');
},
});
} else {
console.log('User not authenticated');
}
}

private isOwner(): boolean {
return this.currentUser.getRoles().includes('owner');
}

private onNotShowed(): void {
this.errorMessageService.showErrorMessage('You are not an owner');
this.router.navigate(['/apartments']);
}

deleteApartment(apartmentId: string): void {
if (apartmentId) {
console.log('Apartment ID:', apartmentId);
this.router.navigate([`/apartment/${apartmentId}/delete`]);
} else {
console.error('Invalid apartment ID');
}
}

updateApartment(apartmentId: string): void {
if (apartmentId) {
console.log('Apartment ID:', apartmentId);
this.router.navigate([`/apartment/${apartmentId}/update`]);
} else {
console.error('Invalid apartment ID');
}
}
createApartment(): void {
this.router.navigate(['/apartment/create']);
}
}
7 changes: 7 additions & 0 deletions src/app/apartment/apartment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export class ApartmentService extends HateoasResourceOperation<Apartment> {
super(Apartment);
}

public findById(id: string): Observable<ResourceCollection<Apartment>> {
return this.searchCollection("findById", {params: {id: id}});
}

public findByOwner(owner: User): Observable<ResourceCollection<Apartment>> {
return this.searchCollection("findByOwner", {params: {owner: owner}});
}
Expand Down Expand Up @@ -63,4 +67,7 @@ export class ApartmentService extends HateoasResourceOperation<Apartment> {
});
});
}
getApartmentsByOwner(ownerId: string): Observable<ResourceCollection<Apartment>> {
return this.searchCollection('findByOwner_Id', { params: { ownerId } });
}
}
5 changes: 5 additions & 0 deletions src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ import { ApartmentCreateComponent } from './apartment/apartment-create/apartment
import { ApartmentUpdateComponent } from './apartment/apartment-update/apartment-update.component';
import { ApartmentDeleteComponent } from './apartment/apartment-delete/apartment-delete.component';
import { VisitStatusComponent } from './visit/visit-status/visit-status.component';
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';



const routes: Routes = [
Expand All @@ -34,7 +37,9 @@ const routes: Routes = [
{ path: 'apartment/:id/update', component: ApartmentUpdateComponent},
{ path: 'apartment/:id/delete', component: ApartmentDeleteComponent},
{ path: 'visit/:id/status', component: VisitStatusComponent},
{ path: 'visit/:id/cancel', component:VisitCancelComponent},
{ path: 'rooms', component: RoomListComponent},
{ path: 'room/create', component: RoomCreateComponent},
{ path: '404', component: NotFoundComponent},
{ path: '', redirectTo: 'about', pathMatch: 'full'}
];
Expand Down
63 changes: 32 additions & 31 deletions src/app/navbar/navbar.component.html
Original file line number Diff line number Diff line change
@@ -1,38 +1,39 @@
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand ms-3" [routerLink]="['/about']"><b>My</b>Apartments</a>
<button class="navbar-toggler" type="button" (click)="isCollapsed = !isCollapsed"
[attr.aria-expanded]="!isCollapsed" aria-controls="navbarContent" aria-label="Toggle navigation">
<a class="navbar-brand ms-3" [routerLink]="['/about']"><b>My</b>Apartments</a>
<button class="navbar-toggler" type="button" (click)="isCollapsed = !isCollapsed" [attr.aria-expanded]="!isCollapsed" aria-controls="navbarContent" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>

<div class="collapse navbar-collapse" id="navbarContent" [ngbCollapse]="isCollapsed">
<ul class="navbar-nav">
<li class="nav-item" [routerLinkActive]="'active'" [routerLinkActiveOptions]="{ exact: true }">
<a class="nav-link" [routerLink]="['/advertisements']">Advertisements</a>
</li>
<li class="nav-item" [routerLinkActive]="'active'" [routerLinkActiveOptions]="{ exact: true }">
<a class="nav-link" [routerLink]="['/about']">About</a>
</li>

<!--LIST APARTMENT-->
<li class="nav-item" *ngIf="isRole('user') || isRole('owner')" [routerLinkActive]="'active'" [routerLinkActiveOptions]="{ exact: true }">
<a class="nav-link" [routerLink]="['/apartments']">Apartments list</a>
</li>
<div class="collapse navbar-collapse" id="navbarContent" [ngbCollapse]="isCollapsed">
<ul class="navbar-nav">

<li class="nav-item" [routerLinkActive]="'active'" [routerLinkActiveOptions]="{ exact: true }">
<a class="nav-link" [routerLink]="['/about']">About</a>
</li>

<!--LIST ADVERTISEMENTS-->
<li class="nav-item" [routerLinkActive]="'active'" [routerLinkActiveOptions]="{ exact: true }">
<a class="nav-link" [routerLink]="['/advertisements']">Advertisements List</a>
</li>

<!--CREATE APARTMENT-->
<li class="nav-item" *ngIf="isRole('owner')" [routerLinkActive]="'active'" [routerLinkActiveOptions]="{ exact: true }">
<a class="nav-link" [routerLink]="['/apartment/create']">Create apartment</a>
</li>
<!--LIST APARTMENT-->
<li class="nav-item" *ngIf="isRole('owner')" [routerLinkActive]="'active'" [routerLinkActiveOptions]="{ exact: true }">
<a class="nav-link" [routerLink]="['/apartments']">Apartments list</a>
</li>

<li class="nav-item" *ngIf="isRole('user')" ngbDropdown>
<a class="nav-link dropdown-toggle" id="playerDropdown" role="button"
aria-haspopup="true" aria-expanded="false" ngbDropdownToggle>Users</a>
<div class="dropdown-menu" aria-labelledby="playDropdown" ngbDropdownMenu>
<a class="dropdown-item" [routerLinkActive]="'active'" [routerLinkActiveOptions]="{ exact: true }"
[routerLink]="['/users']"><span class="fas fa-user-circle"></span> List</a>
</div>
</li>
</ul>
<ul class="navbar-nav ms-auto me-3" app-login-navbar></ul>
</div>
</nav>
<!--CREATE APARTMENT-->
<li class="nav-item" *ngIf="isRole('owner')" [routerLinkActive]="'active'" [routerLinkActiveOptions]="{ exact: true }">
<a class="nav-link" [routerLink]="['/apartment/create']">Create apartment</a>
</li>

<li class="nav-item" *ngIf="isRole('user')" ngbDropdown>
<a class="nav-link dropdown-toggle" id="playerDropdown" role="button" aria-haspopup="true" aria-expanded="false" ngbDropdownToggle>Users</a>
<div class="dropdown-menu" aria-labelledby="playDropdown" ngbDropdownMenu>
<a class="dropdown-item" [routerLinkActive]="'active'" [routerLinkActiveOptions]="{ exact: true }" [routerLink]="['/users']"><span class="fas fa-user-circle"></span> List</a>
</div>
</li>
</ul>
<ul class="navbar-nav ms-auto me-3" app-login-navbar></ul>
</div>
</nav>
Empty file.
57 changes: 57 additions & 0 deletions src/app/room/room-create/room-create.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<div *ngIf="canCreateRoom" class="container mt-5 w-50 p-3">
<h1 class="mb-4">Create Apartment</h1>

<form (ngSubmit)="onSubmit()" #roomForm="ngForm">
<div class="mb-3">
<label for="name" class="form-label">Select apartment</label>
<select class="form-select" id="apartmentSelect" required [(ngModel)]="apartmentId" (ngModelChange)="onApartmentChange()">
<option value="" disabled>Select...</option>
<option *ngFor="let apartment_value of apartments" [value]="apartment_value.id">
{{ apartment_value.name }}
</option>
</select>
</div>
<div class="mb-3">
<label for="name" class="form-label">Is ocuppied ?</label>
<select class="form-control" id="trueFalseSelectOcupied" required [(ngModel)]="room.isOccupied">
<option value="" disabled>Select...</option>
<option [value]="true">True</option>
<option [value]="false">False</option>
</select>
</div>
<div class="mb-3">
<label for="name" class="form-label">Has bed ?</label>
<select class="form-control" id="trueFalseSelectHasBed" required [(ngModel)]="room.hasBed">
<option value="" disabled>Select...</option>
<option [value]="true">True</option>
<option [value]="false">False</option>
</select>
</div>
<div class="mb-3">
<label for="name" class="form-label">Has window ?</label>
<select class="form-control" id="trueFalseSelectHasWindow" required [(ngModel)]="room.hasWindow">
<option value="" disabled>Select...</option>
<option [value]="true">True</option>
<option [value]="false">False</option>
</select>
</div>
<div class="mb-3">
<label for="name" class="form-label">Has desk ?</label>
<select class="form-control" id="trueFalseSelectHasDesk" required [(ngModel)]="room.hasDesk">
<option value="" disabled>Select...</option>
<option [value]="true">True</option>
<option [value]="false">False</option>
</select>
</div>



<button type="submit" class="btn btn-primary mt-3">Submit</button>
</form>
</div>

<div *ngIf="!canCreateRoom" class="container mt-5 w-50 p-3">
<div class="alert alert-danger" role="alert">
You are not authorized to create a Room.
</div>
</div>
23 changes: 23 additions & 0 deletions src/app/room/room-create/room-create.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 { RoomCreateComponent } from './room-create.component';

describe('RoomCreateComponent', () => {
let component: RoomCreateComponent;
let fixture: ComponentFixture<RoomCreateComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [RoomCreateComponent]
})
.compileComponents();

fixture = TestBed.createComponent(RoomCreateComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
Loading

0 comments on commit 5dcf9bc

Please sign in to comment.