diff --git a/back/src/modules/categories/categories.service.ts b/back/src/modules/categories/categories.service.ts index 3cf5153..6656069 100644 --- a/back/src/modules/categories/categories.service.ts +++ b/back/src/modules/categories/categories.service.ts @@ -22,7 +22,26 @@ export class CategoriesService { private readonly apiRepo: Repository, ) {} - async findAll(): Promise { + async findAll(search?: string): Promise { + if (search?.trim()) { + const text = search.trim(); + const entities = await this.categoryRepo + .createQueryBuilder('cat') + .where( + `( + cat.name ILIKE :likeText + OR COALESCE(cat.description, '') ILIKE :likeText + OR word_similarity(:text, cat.name) >= 0.4 + OR word_similarity(:text, COALESCE(cat.description, '')) > 0.5 + )`, + { likeText: `%${text}%`, text }, + ) + .orderBy('cat.order_index', 'ASC') + .addOrderBy('cat.name', 'ASC') + .getMany(); + return { items: entities.map(toCategory) }; + } + const entities = await this.categoryRepo.find({ order: { orderIndex: 'ASC', name: 'ASC' }, }); diff --git a/front-public/src/app/core/category.service.ts b/front-public/src/app/core/category.service.ts index c861198..e375e79 100644 --- a/front-public/src/app/core/category.service.ts +++ b/front-public/src/app/core/category.service.ts @@ -12,8 +12,10 @@ export class CategoryService { private http = inject(HttpClient); private baseUrl = '/api/categories'; - list(): Observable { - return this.http.get(this.baseUrl); + list(search?: string): Observable { + const params: Record = {}; + if (search?.trim()) params['search'] = search; + return this.http.get(this.baseUrl, { params }); } browse(id: string | null): Observable {