dvbooking/admin/src/app/features/bookings/services/booking.service.ts
Roland Schneider 02cad3dbcd add booking
2025-11-20 22:55:31 +01:00

85 lines
2.4 KiB
TypeScript

// 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}`);
}
}