add booking

This commit is contained in:
Roland Schneider
2025-11-20 22:55:31 +01:00
parent 085605f85c
commit 02cad3dbcd
27 changed files with 1330 additions and 41 deletions

View File

@@ -0,0 +1,74 @@
<!-- dvbooking-cli/src/templates/angular/details.component.html.tpl -->
<!-- Generated by the CLI -->
<div class="p-4 md:p-8">
<ng-container *ngIf="booking$ | async as booking; else loading">
<div class="card bg-base-100 shadow-xl max-w-2xl mx-auto">
<div class="card-body">
<h2 class="card-title text-3xl">Booking Details</h2>
<div class="overflow-x-auto mt-4">
<table class="table w-full">
<tbody>
<tr>
<th>id</th>
<td>{{ booking.id }}</td>
</tr>
<tr>
<th>event_id</th>
<td>{{ booking.event_id }}</td>
</tr>
<tr>
<th>occurrence_start_time</th>
<td>{{ booking.occurrence_start_time }}</td>
</tr>
<tr>
<th>user_id</th>
<td>{{ booking.user_id }}</td>
</tr>
<tr>
<th>notes</th>
<td>{{ booking.notes }}</td>
</tr>
<tr>
<th>reserved_seats_count</th>
<td>{{ booking.reserved_seats_count }}</td>
</tr>
<tr>
<th>created_at</th>
<td>{{ booking.created_at }}</td>
</tr>
<tr>
<th>updated_at</th>
<td>{{ booking.updated_at }}</td>
</tr>
<tr>
<th>canceled_at</th>
<td>{{ booking.canceled_at }}</td>
</tr>
<tr>
<th>canceled_reason</th>
<td>{{ booking.canceled_reason }}</td>
</tr>
<tr>
<th>canceled_by_user_id</th>
<td>{{ booking.canceled_by_user_id }}</td>
</tr>
</tbody>
</table>
</div>
<div class="card-actions justify-end mt-6">
<a routerLink="/bookings" class="btn btn-secondary">Back to List</a>
<a routerLink="/bookings/{{ booking.id }}/edit" class="btn btn-primary">Edit</a>
</div>
</div>
</div>
</ng-container>
<ng-template #loading>
<div class="text-center p-8">
<span class="loading loading-spinner loading-lg"></span>
</div>
</ng-template>
</div>

View File

@@ -0,0 +1,34 @@
// dvbooking-cli/src/templates/angular/details.component.ts.tpl
// Generated by the CLI
import { Component, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ActivatedRoute, RouterModule } from '@angular/router';
import { Observable } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { Booking } from '../../models/booking.model';
import { BookingService } from '../../services/booking.service';
@Component({
selector: 'app-booking-details',
templateUrl: './booking-details.component.html',
standalone: true,
imports: [CommonModule, RouterModule],
})
export class BookingDetailsComponent implements OnInit {
booking$!: Observable<Booking>;
constructor(
private route: ActivatedRoute,
private bookingService: BookingService
) {}
ngOnInit(): void {
this.booking$ = this.route.params.pipe(
switchMap(params => {
const id = params['id'];
return this.bookingService.findOne(id);
})
);
}
}

View File

@@ -0,0 +1,15 @@
<!-- dvbooking-cli/src/templates/angular/filter.component.html.tpl -->
<!-- Generated by the CLI -->
<form [formGroup]="filterForm" class="p-4 bg-base-200 rounded-lg shadow">
<div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-end">
<div class="form-control"><label class="label"><span class="label-text">notes</span></label>
<input type="text" formControlName="notes" class="input input-bordered w-full" placeholder="Filter by notes" /></div>
<div class="form-control"><label class="label"><span class="label-text">canceled_reason</span></label>
<input type="text" formControlName="canceled_reason" class="input input-bordered w-full" placeholder="Filter by canceled_reason" /></div>
<div class="form-control">
<button type="button" (click)="reset()" class="btn btn-secondary">Reset</button>
</div>
</div>
</form>

View File

@@ -0,0 +1,38 @@
// dvbooking-cli/src/templates/angular/filter.component.ts.tpl
// Generated by the CLI
import { Component, EventEmitter, Output } from '@angular/core';
import { FormBuilder, FormGroup, ReactiveFormsModule } from '@angular/forms';
import { debounceTime, distinctUntilChanged } from 'rxjs/operators';
@Component({
selector: 'app-booking-filter',
templateUrl: './booking-filter.component.html',
standalone: true,
imports: [ReactiveFormsModule]
})
export class BookingFilterComponent {
@Output() filterChanged = new EventEmitter<any>();
filterForm: FormGroup;
constructor(private fb: FormBuilder) {
this.filterForm = this.fb.group({
notes: [''],
canceled_reason: ['']
});
this.filterForm.valueChanges.pipe(
debounceTime(300),
distinctUntilChanged()
).subscribe(values => {
const cleanFilter = Object.fromEntries(
Object.entries(values).filter(([_, v]) => v != null && v !== '')
);
this.filterChanged.emit(cleanFilter);
});
}
reset() {
this.filterForm.reset();
}
}

View File

@@ -0,0 +1,52 @@
<!-- dvbooking-cli/src/templates/angular/form.component.html.tpl -->
<!-- Generated by the CLI -->
<div class="p-4 md:p-8">
<div class="card bg-base-100 shadow-xl max-w-2xl mx-auto">
<div class="card-body">
<h2 class="card-title text-3xl">
{{ isEditMode ? 'Edit' : 'Create' }} Booking
</h2>
<form [formGroup]="form" (ngSubmit)="onSubmit()" class="space-y-4 mt-4">
<div class="form-control"><label class="label"><span class="label-text">event_id</span></label>
<input type="number" formControlName="event_id" class="input input-bordered w-full" /></div>
<div class="form-control"><label class="label"><span class="label-text">occurrence_start_time</span></label>
<input type="text" formControlName="occurrence_start_time" class="input input-bordered w-full" /></div>
<div class="form-control"><label class="label"><span class="label-text">user_id</span></label>
<input type="number" formControlName="user_id" class="input input-bordered w-full" /></div>
<div class="form-control"><label class="label"><span class="label-text">notes</span></label>
<input type="text" formControlName="notes" class="input input-bordered w-full" /></div>
<div class="form-control"><label class="label"><span class="label-text">reserved_seats_count</span></label>
<input type="number" formControlName="reserved_seats_count" class="input input-bordered w-full" /></div>
<div class="form-control"><label class="label"><span class="label-text">created_at</span></label>
<input type="text" formControlName="created_at" class="input input-bordered w-full" /></div>
<div class="form-control"><label class="label"><span class="label-text">updated_at</span></label>
<input type="text" formControlName="updated_at" class="input input-bordered w-full" /></div>
<div class="form-control"><label class="label"><span class="label-text">canceled_at</span></label>
<input type="text" formControlName="canceled_at" class="input input-bordered w-full" /></div>
<div class="form-control"><label class="label"><span class="label-text">canceled_reason</span></label>
<input type="text" formControlName="canceled_reason" class="input input-bordered w-full" /></div>
<div class="form-control"><label class="label"><span class="label-text">canceled_by_user_id</span></label>
<input type="number" formControlName="canceled_by_user_id" class="input input-bordered w-full" /></div>
<div class="card-actions justify-end mt-6">
<a routerLink="/bookings" class="btn btn-ghost">Cancel</a>
<button type="submit" class="btn btn-primary" [disabled]="form.invalid">
{{ isEditMode ? 'Update' : 'Create' }}
</button>
</div>
</form>
</div>
</div>
</div>

View File

@@ -0,0 +1,94 @@
// dvbooking-cli/src/templates/angular/form.component.ts.tpl
// Generated by the CLI
import { Component, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormBuilder, FormGroup, Validators, ReactiveFormsModule } from '@angular/forms';
import { ActivatedRoute, Router, RouterModule } from '@angular/router';
import { Observable, of } from 'rxjs';
import { switchMap, tap } from 'rxjs/operators';
import { Booking } from '../../models/booking.model';
import { BookingService } from '../../services/booking.service';
@Component({
selector: 'app-booking-form',
templateUrl: './booking-form.component.html',
standalone: true,
imports: [CommonModule, ReactiveFormsModule, RouterModule],
})
export class BookingFormComponent implements OnInit {
form: FormGroup;
isEditMode = false;
id: number | null = null;
private numericFields = ["event_id","user_id","reserved_seats_count","canceled_by_user_id"];
constructor(
private fb: FormBuilder,
private route: ActivatedRoute,
private router: Router,
private bookingService: BookingService
) {
this.form = this.fb.group({
event_id: [null],
occurrence_start_time: [null],
user_id: [null],
notes: [null],
reserved_seats_count: [null],
created_at: [null],
updated_at: [null],
canceled_at: [null],
canceled_reason: [null],
canceled_by_user_id: [null]
});
}
ngOnInit(): void {
this.route.params.pipe(
tap(params => {
if (params['id']) {
this.isEditMode = true;
this.id = +params['id'];
}
}),
switchMap(() => {
if (this.isEditMode && this.id) {
return this.bookingService.findOne(this.id);
}
return of(null);
})
).subscribe(booking => {
if (booking) {
this.form.patchValue(booking);
}
});
}
onSubmit(): void {
if (this.form.invalid) {
this.form.markAllAsTouched();
return;
}
const payload = { ...this.form.value };
for (const field of this.numericFields) {
if (payload[field] != null && payload[field] !== '') {
payload[field] = parseFloat(payload[field]);
}
}
let action$: Observable<Booking>;
if (this.isEditMode && this.id) {
action$ = this.bookingService.update(this.id, payload);
} else {
action$ = this.bookingService.create(payload);
}
action$.subscribe({
next: () => this.router.navigate(['/bookings']),
error: (err) => console.error('Failed to save booking', err)
});
}
}

View File

@@ -0,0 +1,78 @@
<!-- dvbooking-cli/src/templates/angular/list.component.html.tpl -->
<!-- Generated by the CLI -->
<div class="p-4 md:p-8 space-y-6">
<div class="flex justify-between items-center">
<h1 class="text-3xl font-bold">Bookings</h1>
<a routerLink="/bookings/new" class="btn btn-primary">Create New</a>
</div>
<app-booking-filter (filterChanged)="onFilterChanged($event)"></app-booking-filter>
<ng-container *ngIf="paginatedResponse$ | async as response; else loading">
<div class="overflow-x-auto bg-base-100 rounded-lg shadow">
<table class="table w-full">
<thead>
<tr>
<th>id</th>
<th>event_id</th>
<th>occurrence_start_time</th>
<th>user_id</th>
<th>notes</th>
<th>reserved_seats_count</th>
<th>created_at</th>
<th>updated_at</th>
<th>canceled_at</th>
<th>canceled_reason</th>
<th>canceled_by_user_id</th>
<th class="text-right">Actions</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let item of response.data" class="hover">
<td>{{ item.id }}</td>
<td>{{ item.event_id }}</td>
<td>{{ item.occurrence_start_time }}</td>
<td>{{ item.user_id }}</td>
<td>{{ item.notes }}</td>
<td>{{ item.reserved_seats_count }}</td>
<td>{{ item.created_at }}</td>
<td>{{ item.updated_at }}</td>
<td>{{ item.canceled_at }}</td>
<td>{{ item.canceled_reason }}</td>
<td>{{ item.canceled_by_user_id }}</td>
<td class="text-right space-x-2">
<a [routerLink]="['/bookings', item.id]" class="btn btn-sm btn-ghost">View</a>
<a [routerLink]="['/bookings', item.id, 'edit']" class="btn btn-sm btn-ghost">Edit</a>
<button (click)="deleteItem(item.id)" class="btn btn-sm btn-error btn-ghost">Delete</button>
</td>
</tr>
<tr *ngIf="response.data.length === 0">
<td colspan="12" class="text-center">No bookings found.</td>
</tr>
</tbody>
</table>
</div>
<!-- Pagination Controls -->
<div *ngIf="response.meta.totalPages > 1" class="flex justify-center mt-4">
<div class="join">
<button
class="join-item btn"
(click)="changePage(response.meta.currentPage - 1)"
[disabled]="response.meta.currentPage === 1">«</button>
<button class="join-item btn">Page {{ response.meta.currentPage }} of {{ response.meta.totalPages }}</button>
<button
class="join-item btn"
(click)="changePage(response.meta.currentPage + 1)"
[disabled]="response.meta.currentPage === response.meta.totalPages">»</button>
</div>
</div>
</ng-container>
<ng-template #loading>
<div class="text-center p-8">
<span class="loading loading-spinner loading-lg"></span>
</div>
</ng-template>
</div>

View File

@@ -0,0 +1,70 @@
// dvbooking-cli/src/templates/angular/list.component.ts.tpl
// Generated by the CLI
import { Component, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';
import { BehaviorSubject, Observable, combineLatest } from 'rxjs';
import { switchMap, startWith } from 'rxjs/operators';
import { Booking } from '../../models/booking.model';
import { BookingService } from '../../services/booking.service';
import { BookingFilterComponent } from '../booking-filter/booking-filter.component';
import { PaginatedResponse } from '../../../../../types';
@Component({
selector: 'app-booking-list',
templateUrl: './booking-list.component.html',
standalone: true,
imports: [CommonModule,RouterModule, BookingFilterComponent],
})
export class BookingListComponent implements OnInit {
private refresh$ = new BehaviorSubject<void>(undefined);
private filter$ = new BehaviorSubject<any>({});
private page$ = new BehaviorSubject<number>(1);
paginatedResponse$!: Observable<PaginatedResponse<Booking>>;
constructor(private bookingService: BookingService) { }
ngOnInit(): void {
this.paginatedResponse$ = combineLatest([
this.refresh$,
this.filter$.pipe(startWith({})),
this.page$.pipe(startWith(1))
]).pipe(
switchMap(([_, filter, page]) => {
const query = { ...filter, page, limit: 10 };
return this.bookingService.find(query);
})
);
}
onFilterChanged(filter: any): void {
this.page$.next(1);
this.filter$.next(filter);
}
changePage(newPage: number): void {
if (newPage > 0) {
this.page$.next(newPage);
}
}
deleteItem(id: number): void {
if (confirm('Are you sure you want to delete this item?')) {
this.bookingService.remove(id).subscribe({
next: () => {
console.log(`Item with ID ${id} deleted successfully.`);
this.refresh$.next();
},
// --- THIS IS THE FIX ---
// Explicitly type 'err' to satisfy strict TypeScript rules.
error: (err: any) => {
console.error(`Error deleting item with ID ${id}:`, err);
}
});
}
}
}

View File

@@ -0,0 +1,26 @@
// dvbooking-cli/src/templates/angular-generic/data-provider.service.ts.tpl
// Generated by the CLI
import { inject, Injectable } from '@angular/core';
import { DataProvider, GetDataOptions, GetDataResponse } from '../../../../components/generic-table/data-provider.interface';
import { Booking } from '../../models/booking.model';
import { map, Observable } from 'rxjs';
import { BookingService } from '../../services/booking.service';
@Injectable({
providedIn: 'root',
})
export class BookingDataProvider implements DataProvider<Booking> {
private bookingService = inject(BookingService);
getData(options?: GetDataOptions): Observable<GetDataResponse<Booking>> {
const {q,page,limit} = options?.params ?? {};
// The generic table's params are compatible with our NestJS Query DTO
return this.bookingService.search(q ?? '',page,limit, ).pipe(
map((res) => {
// Adapt the paginated response to the GetDataResponse format
return { data: res };
})
);
}
}

View File

@@ -0,0 +1,11 @@
<!-- dvbooking-cli/src/templates/angular-generic/table.component.html.tpl -->
<!-- Generated by the CLI -->
<div class="p-4 md:p-8 space-y-6">
<div class="flex justify-between items-center">
<h1 class="text-3xl font-bold">Bookings (Generic Table)</h1>
<a routerLink="/bookings/new" class="btn btn-primary">Create New</a>
</div>
<app-generic-table [config]="tableConfig"></app-generic-table>
</div>

View File

@@ -0,0 +1,145 @@
// dvbooking-cli/src/templates/angular-generic/table.component.ts.tpl
// Generated by the CLI
import { Component, inject, OnInit } from '@angular/core';
import { Router, RouterModule } from '@angular/router';
import { Booking } from '../../models/booking.model';
import { BookingDataProvider } from './booking-data-provider.service';
import { ColumnDefinition } from '../../../../components/generic-table/column-definition.interface';
import { GenericTable } from '../../../../components/generic-table/generic-table';
import { GenericTableConfig } from '../../../../components/generic-table/generic-table.config';
import {
ActionDefinition,
GenericActionColumn,
} from '../../../../components/generic-action-column/generic-action-column';
import { BookingService } from '../../services/booking.service';
import { BehaviorSubject } from 'rxjs';
@Component({
selector: 'app-booking-table',
standalone: true,
imports: [GenericTable, RouterModule],
templateUrl: './booking-table.component.html',
})
export class BookingTableComponent implements OnInit {
private refresh$ = new BehaviorSubject<void>(undefined);
private filter$ = new BehaviorSubject<any>({});
private page$ = new BehaviorSubject<number>(1);
private limit$ = new BehaviorSubject<number>(10);
router = inject(Router);
tableConfig!: GenericTableConfig<Booking>;
bookingDataProvider = inject(BookingDataProvider);
bookingService = inject(BookingService);
ngOnInit(): void {
const actionHandler = (action: ActionDefinition<Booking>, item: Booking) => {
switch (action.action) {
case 'view':
this.router.navigate(['/bookings', item?.id]);
break;
case 'edit':
this.router.navigate(['/bookings', item?.id, 'edit']);
break;
case 'delete':
this.deleteItem(item.id);
break;
}
};
this.tableConfig = {
refresh$: this.refresh$,
filter$: this.filter$,
page$: this.page$,
limit$: this.limit$,
dataProvider: this.bookingDataProvider,
columns: [
{
attribute: 'event_id',
headerCell: true,
valueCell: true,
},
{
attribute: 'occurrence_start_time',
headerCell: true,
valueCell: true,
},
{
attribute: 'user_id',
headerCell: true,
valueCell: true,
},
{
attribute: 'notes',
headerCell: true,
valueCell: true,
},
{
attribute: 'reserved_seats_count',
headerCell: true,
valueCell: true,
},
{
attribute: 'created_at',
headerCell: true,
valueCell: true,
},
{
attribute: 'updated_at',
headerCell: true,
valueCell: true,
},
{
attribute: 'canceled_at',
headerCell: true,
valueCell: true,
},
{
attribute: 'canceled_reason',
headerCell: true,
valueCell: true,
},
{
attribute: 'canceled_by_user_id',
headerCell: true,
valueCell: true,
},
{
attribute: 'actions',
headerCell: { value: 'Actions' },
valueCell: {
component: GenericActionColumn,
componentInputs: item => ({
item: item,
actions: [
{ action: 'view', handler: actionHandler },
{ action: 'edit', handler: actionHandler },
{ action: 'delete', handler: actionHandler },
] as ActionDefinition<Booking>[],
}),
},
},
] as ColumnDefinition<Booking>[],
tableCssClass: 'booking-table-container',
};
}
deleteItem(id: number): void {
if (confirm('Are you sure you want to delete this item?')) {
this.bookingService.remove(id).subscribe({
next: () => {
console.log(`Item with ID ${id} deleted successfully.`);
this.refresh$.next();
},
// --- THIS IS THE FIX ---
// Explicitly type 'err' to satisfy strict TypeScript rules.
error: (err: any) => {
console.error(`Error deleting item with ID ${id}:`, err);
}
});
}
}
}

View File

@@ -0,0 +1,16 @@
// dvbooking-cli/src/templates/angular/model.ts.tpl
// Generated by the CLI
export interface Booking {
id: number;
event_id: number;
occurrence_start_time: Date;
user_id: number;
notes: string;
reserved_seats_count: number;
created_at: Date;
updated_at: Date;
canceled_at: Date;
canceled_reason: string;
canceled_by_user_id: number;
}

View File

@@ -0,0 +1,85 @@
// dvbooking-cli/src/templates/angular/service.ts.tpl
// Generated by the CLI
import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs';
import { Booking } from '../models/booking.model';
import { ConfigurationService } from '../../../services/configuration.service';
import { PaginatedResponse } from '../../../../types';
export interface SearchResponse<T> {
data: T[];
total: number;
}
@Injectable({
providedIn: 'root'
})
export class BookingService {
private readonly apiUrl: string;
constructor(
private http: HttpClient,
private configService: ConfigurationService
) {
this.apiUrl = `${this.configService.getApiUrl()}/bookings`;
}
/**
* Find records with pagination and filtering.
*/
public find(filter: Record<string, any>): Observable<PaginatedResponse<Booking>> {
// --- THIS IS THE FIX ---
// The incorrect line: .filter(([_, v]) for v != null)
// is now correctly written with an arrow function.
const cleanFilter = Object.fromEntries(
Object.entries(filter).filter(([_, v]) => v != null)
);
// --- END OF FIX ---
const params = new HttpParams({ fromObject: cleanFilter });
return this.http.get<PaginatedResponse<Booking>>(this.apiUrl, { params });
}
/**
* Search across multiple fields with a single term.
* @param term The search term (q).
*/
public search(term: string, page: number = 1, limit: number = 10): Observable<PaginatedResponse<Booking>> {
const params = new HttpParams()
.set('q', term)
.set('page', page.toString())
.set('limit', limit.toString());
return this.http.get<PaginatedResponse<Booking>>(`${this.apiUrl}/search`, { params });
}
/**
* Find a single record by its ID.
*/
public findOne(id: number): Observable<Booking> {
return this.http.get<Booking>(`${this.apiUrl}/${id}`);
}
/**
* Create a new record.
*/
public create(data: Omit<Booking, 'id'>): Observable<Booking> {
return this.http.post<Booking>(this.apiUrl, data);
}
/**
* Update an existing record.
*/
public update(id: number, data: Partial<Omit<Booking, 'id'>>): Observable<Booking> {
return this.http.patch<Booking>(`${this.apiUrl}/${id}`, data);
}
/**
* Remove a record by its ID.
*/
public remove(id: number): Observable<any> {
return this.http.delete(`${this.apiUrl}/${id}`);
}
}