added category filtering

This commit is contained in:
Joe Arndt 2026-02-27 21:48:33 -06:00
parent 9daca90d6b
commit 69723d0801
8 changed files with 42 additions and 11 deletions

View file

@ -5,7 +5,11 @@ meta {
} }
get { get {
url: {{localBaseUrl}}/{{resourcePath}} url: {{localBaseUrl}}/{{resourcePath}}?sort=desc
body: none body: none
auth: inherit auth: inherit
} }
params:query {
sort: desc
}

View file

@ -5,7 +5,11 @@ meta {
} }
get { get {
url: {{localBaseUrl}}/{{resourcePath}} url: {{localBaseUrl}}/{{resourcePath}}?sort=desc
body: none body: none
auth: inherit auth: inherit
} }
params:query {
sort: desc
}

View file

@ -9,3 +9,7 @@ get {
body: none body: none
auth: inherit auth: inherit
} }
params:query {
~tags: Tundra
}

View file

@ -6,6 +6,7 @@ import {
Delete, Delete,
Body, Body,
Param, Param,
Query,
HttpCode, HttpCode,
HttpStatus, HttpStatus,
BadRequestException, BadRequestException,
@ -23,8 +24,12 @@ export class CategoriesController {
@Get() @Get()
@HttpCode(HttpStatus.OK) @HttpCode(HttpStatus.OK)
public async findAll(): Promise<Category[]> { public async find(@Query() filters: GetCategoryFilters): Promise<Category[]> {
return await this.categoriesService.findAll(); const ids = Array.isArray(filters.ids) ? filters.ids : filters.ids ? [filters.ids] : [];
const categories = Array.isArray(filters.categories) ? filters.categories : filters.categories ? [filters.categories] : [];
const sort = filters.sort?.toLowerCase() === 'desc' ? 'desc' : 'asc';
return await this.categoriesService.find({ ids, categories, sort});
} }
@Get(':id') @Get(':id')
@ -73,3 +78,9 @@ export class CategoriesController {
return await this.categoriesService.remove(id); return await this.categoriesService.remove(id);
} }
} }
export interface GetCategoryFilters {
ids?: string[];
categories?: string[];
sort?: 'asc' | 'desc';
}

View file

@ -3,13 +3,14 @@ import { CreateCategoryDto } from './dto/create-category.dto';
import { UpdateCategoryDto } from './dto/update-category.dto'; import { UpdateCategoryDto } from './dto/update-category.dto';
import { CategoryDataService } from './category-data.service'; import { CategoryDataService } from './category-data.service';
import { Category } from './entities/category.entity'; import { Category } from './entities/category.entity';
import { GetCategoryFilters } from './categories.controller';
@Injectable() @Injectable()
export class CategoriesService { export class CategoriesService {
public constructor(private categoryDataService: CategoryDataService) { } public constructor(private categoryDataService: CategoryDataService) { }
public async findAll(): Promise<Category[]> { public async find(filters: GetCategoryFilters): Promise<Category[]> {
return await this.categoryDataService.getAll(); return await this.categoryDataService.get(filters);
} }
public async findById(id: string): Promise<Category> { public async findById(id: string): Promise<Category> {

View file

@ -1,7 +1,8 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { DataSource, Repository } from 'typeorm'; import { DataSource, ILike, Repository } from 'typeorm';
import { Category } from './entities/category.entity'; import { Category } from './entities/category.entity';
import { UpdateCategoryDto } from './dto/update-category.dto'; import { UpdateCategoryDto } from './dto/update-category.dto';
import { GetCategoryFilters } from './categories.controller';
@Injectable() @Injectable()
export class CategoryDataService { export class CategoryDataService {
@ -11,8 +12,14 @@ export class CategoryDataService {
this.categories = this.dataSource.getRepository(Category); this.categories = this.dataSource.getRepository(Category);
} }
public async getAll(): Promise<Category[]> { public async get(filters: GetCategoryFilters): Promise<Category[]> {
return await this.categories.find(); const idFilters = filters.ids?.map(id => { return { id }}) ?? [];
const nameFilters = filters.categories?.map(tag => { return { name: ILike('%' + tag + '%') }}) ?? [];
return await this.categories.find({
where: [...idFilters, ...nameFilters],
order: { name: filters.sort === 'desc' ? 'desc' : 'asc' }
});
} }
public async getById(id: string): Promise<Category | null> { public async getById(id: string): Promise<Category | null> {

View file

@ -18,7 +18,7 @@ export class MerchantDataService {
return await this.merchants.find({ return await this.merchants.find({
where: [...idFilters, ...nameFilters], where: [...idFilters, ...nameFilters],
order: { name: filters.sort === 'desc' ? 'desc' : 'asc' }, order: { name: filters.sort === 'desc' ? 'desc' : 'asc' }
}); });
} }

View file

@ -18,7 +18,7 @@ export class TagDataService {
return await this.tags.find({ return await this.tags.find({
where: [...idFilters, ...nameFilters], where: [...idFilters, ...nameFilters],
order: { name: filters.sort === 'desc' ? 'desc' : 'asc' }, order: { name: filters.sort === 'desc' ? 'desc' : 'asc' }
}); });
} }