toSignal via Gemini

This commit is contained in:
tanshu committed 2026-08-18 07:55:54 +00:00
1 parent 59eb45da96
commit 993f83c786
350 files changed
+6231 -9801

No files matched your search

+30 -13
View File
@@ -1,5 +1,5 @@
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Injectable, inject } from '@angular/core';
import { HttpClient, HttpHeaders, httpResource, HttpResourceRef } from '@angular/common/http';
import { Injectable, Signal, inject } from '@angular/core';
import { Observable } from 'rxjs';
import { catchError } from 'rxjs/operators';
@@ -20,11 +20,11 @@ export class CustomerService {
private http = inject(HttpClient);
private log = inject(ErrorLoggerService);
get(id: string | null): Observable<Customer> {
const getUrl: string = id === null ? `${url}` : `${url}/${id}`;
return this.http
.get<Customer>(getUrl)
.pipe(catchError(this.log.handleError(serviceName, `get id=${id}`))) as Observable<Customer>;
get(id: Signal<string | null> | string | null): HttpResourceRef<Customer | undefined> {
return httpResource<Customer>(() => {
const idVal = typeof id === 'function' ? id() : id;
return idVal === null || idVal === undefined ? url : `${url}/${idVal}`;
});
}
list(q: string | null, page = 0, size = 50, field = 'name', direction = 'asc'): Observable<PagedResult<Customer>> {
@@ -35,12 +35,29 @@ export class CustomerService {
f: field,
d: direction,
};
console.log('CustomerService.list', params);
return this.http
.get<PagedResult<Customer>>(`${url}/list`, { params })
.pipe(catchError(this.log.handleError(serviceName, 'list'))) as Observable<PagedResult<Customer>>;
}
query(
params: Signal<{ q: string; p: number; s: number; f: string; d: string }>,
): HttpResourceRef<PagedResult<Customer> | undefined> {
return httpResource<PagedResult<Customer>>(() => {
const p = params();
return {
url: `${url}/list`,
params: {
...(p.q ? { q: p.q } : {}),
p: p.p,
s: p.s,
f: p.f,
d: p.d,
},
};
});
}
save(customer: Customer): Observable<Customer> {
return this.http
.post<Customer>(`${url}`, customer, httpOptions)
@@ -66,10 +83,10 @@ export class CustomerService {
.pipe(catchError(this.log.handleError(serviceName, 'delete'))) as Observable<Customer>;
}
autocomplete(query: string): Observable<Customer[]> {
const options = { params: new HttpParams().set('q', query) };
return this.http
.get<Customer[]>(`${url}/query`, options)
.pipe(catchError(this.log.handleError(serviceName, 'autocomplete'))) as Observable<Customer[]>;
autocomplete(query: Signal<string | null> | string | null): HttpResourceRef<Customer[] | undefined> {
return httpResource<Customer[]>(() => {
const qVal = typeof query === 'function' ? query() : query;
return `${url}/query?q=${qVal ?? ''}`;
});
}
}