diff --git a/src/tags/tag-data.service.ts b/src/tags/tag-data.service.ts index df417c5..7776047 100644 --- a/src/tags/tag-data.service.ts +++ b/src/tags/tag-data.service.ts @@ -1,7 +1,8 @@ import { Injectable } from '@nestjs/common'; -import { DataSource, Repository } from 'typeorm'; +import { DataSource, ILike, Repository } from 'typeorm'; import { Tag } from './entities/tag.entity'; import { UpdateTagDto } from './dto/update-tag.dto'; +import { GetTagFilters } from './tags.controller'; @Injectable() export class TagDataService { @@ -11,8 +12,13 @@ export class TagDataService { this.tags = this.dataSource.getRepository(Tag); } - public async getAll(): Promise { - return await this.tags.find(); + public async getAll(filters: GetTagFilters): Promise { + 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 { diff --git a/src/tags/tags.controller.ts b/src/tags/tags.controller.ts index d627f8e..e240343 100644 --- a/src/tags/tags.controller.ts +++ b/src/tags/tags.controller.ts @@ -6,6 +6,7 @@ import { Delete, Body, Param, + Query, HttpCode, HttpStatus, BadRequestException, @@ -23,8 +24,12 @@ export class TagsController { @Get() @HttpCode(HttpStatus.OK) - public async findAll(): Promise { - return await this.tagsService.findAll(); + public async findAll(@Query() filters: GetTagFilters): Promise { + 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') @@ -73,3 +78,9 @@ export class TagsController { return await this.tagsService.remove(id); } } + +export interface GetTagFilters { + ids?: string[]; + tags?: string[]; + sort?: 'asc' | 'desc'; +} diff --git a/src/tags/tags.service.ts b/src/tags/tags.service.ts index ab5ff92..c47d9be 100644 --- a/src/tags/tags.service.ts +++ b/src/tags/tags.service.ts @@ -3,13 +3,14 @@ import { CreateTagDto } from './dto/create-tag.dto'; import { UpdateTagDto } from './dto/update-tag.dto'; import { TagDataService } from './tag-data.service'; import { Tag } from './entities/tag.entity'; +import { GetTagFilters } from './tags.controller'; @Injectable() export class TagsService { public constructor(private tagDataService: TagDataService) { } - public async findAll(): Promise { - return await this.tagDataService.getAll(); + public async findAll(filters: GetTagFilters): Promise { + return await this.tagDataService.getAll(filters); } public async findById(id: string): Promise {