added tag filtering

This commit is contained in:
Joe Arndt 2026-02-27 21:30:24 -06:00
parent f390e2a8c3
commit 846e3e6368
3 changed files with 25 additions and 7 deletions

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 { Tag } from './entities/tag.entity'; import { Tag } from './entities/tag.entity';
import { UpdateTagDto } from './dto/update-tag.dto'; import { UpdateTagDto } from './dto/update-tag.dto';
import { GetTagFilters } from './tags.controller';
@Injectable() @Injectable()
export class TagDataService { export class TagDataService {
@ -11,8 +12,13 @@ export class TagDataService {
this.tags = this.dataSource.getRepository(Tag); this.tags = this.dataSource.getRepository(Tag);
} }
public async getAll(): Promise<Tag[]> { public async getAll(filters: GetTagFilters): Promise<Tag[]> {
return await this.tags.find(); const idFilters = filters.ids?.map(id => { return { id }}) ?? [];
const nameFilters = filters.tags?.map(tag => { return { name: ILike('%' + tag + '%') }}) ?? [];
return await this.tags.find({
where: [...idFilters, ...nameFilters],
order: { name: filters.sort === 'desc' ? 'desc' : 'asc' },
});
} }
public async getById(id: string): Promise<Tag | null> { public async getById(id: string): Promise<Tag | null> {

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 TagsController {
@Get() @Get()
@HttpCode(HttpStatus.OK) @HttpCode(HttpStatus.OK)
public async findAll(): Promise<Tag[]> { public async findAll(@Query() filters: GetTagFilters): Promise<Tag[]> {
return await this.tagsService.findAll(); const ids = Array.isArray(filters.ids) ? filters.ids : filters.ids ? [filters.ids] : [];
const tags = Array.isArray(filters.tags) ? filters.tags : filters.tags ? [filters.tags] : [];
const sort = filters.sort?.toLowerCase() === 'desc' ? 'desc' : 'asc';
return await this.tagsService.findAll({ ids, tags, sort });
} }
@Get(':id') @Get(':id')
@ -73,3 +78,9 @@ export class TagsController {
return await this.tagsService.remove(id); return await this.tagsService.remove(id);
} }
} }
export interface GetTagFilters {
ids?: string[];
tags?: string[];
sort?: 'asc' | 'desc';
}

View file

@ -3,13 +3,14 @@ import { CreateTagDto } from './dto/create-tag.dto';
import { UpdateTagDto } from './dto/update-tag.dto'; import { UpdateTagDto } from './dto/update-tag.dto';
import { TagDataService } from './tag-data.service'; import { TagDataService } from './tag-data.service';
import { Tag } from './entities/tag.entity'; import { Tag } from './entities/tag.entity';
import { GetTagFilters } from './tags.controller';
@Injectable() @Injectable()
export class TagsService { export class TagsService {
public constructor(private tagDataService: TagDataService) { } public constructor(private tagDataService: TagDataService) { }
public async findAll(): Promise<Tag[]> { public async findAll(filters: GetTagFilters): Promise<Tag[]> {
return await this.tagDataService.getAll(); return await this.tagDataService.getAll(filters);
} }
public async findById(id: string): Promise<Tag> { public async findById(id: string): Promise<Tag> {