From a7c6967b493e6225311a25019cdda2f137777fc2 Mon Sep 17 00:00:00 2001 From: Lam Ngoc Khuong Date: Sun, 17 Nov 2024 23:51:34 +0700 Subject: [PATCH] feat: add tags api --- .../api/profile/profile.controller.spec.ts | 2 +- .../src/api/profile/profile.service.spec.ts | 2 +- .../admin-api/src/api/tag/dto/tag-list.dto.ts | 6 ++++++ .../src/api/tag/tag.controller.spec.ts | 14 ++++++++++++- apps/admin-api/src/api/tag/tag.controller.ts | 21 ++++++++++++++++--- apps/admin-api/src/api/tag/tag.module.ts | 3 +++ .../admin-api/src/api/tag/tag.service.spec.ts | 16 ++++++++++++-- apps/admin-api/src/api/tag/tag.service.ts | 18 +++++++++++++++- 8 files changed, 73 insertions(+), 9 deletions(-) create mode 100644 apps/admin-api/src/api/tag/dto/tag-list.dto.ts diff --git a/apps/admin-api/src/api/profile/profile.controller.spec.ts b/apps/admin-api/src/api/profile/profile.controller.spec.ts index adcb096..f9b344f 100644 --- a/apps/admin-api/src/api/profile/profile.controller.spec.ts +++ b/apps/admin-api/src/api/profile/profile.controller.spec.ts @@ -6,7 +6,7 @@ describe('ProfileController', () => { let controller: ProfileController; let profileServiceValue: Partial>; - beforeEach(async () => { + beforeAll(async () => { profileServiceValue = { getProfile: jest.fn(), follow: jest.fn(), diff --git a/apps/admin-api/src/api/profile/profile.service.spec.ts b/apps/admin-api/src/api/profile/profile.service.spec.ts index ce966ae..f31e3fa 100644 --- a/apps/admin-api/src/api/profile/profile.service.spec.ts +++ b/apps/admin-api/src/api/profile/profile.service.spec.ts @@ -14,7 +14,7 @@ describe('ProfileService', () => { Record, jest.Mock> >; - beforeEach(async () => { + beforeAll(async () => { const module: TestingModule = await Test.createTestingModule({ providers: [ ProfileService, diff --git a/apps/admin-api/src/api/tag/dto/tag-list.dto.ts b/apps/admin-api/src/api/tag/dto/tag-list.dto.ts new file mode 100644 index 0000000..d4bacd1 --- /dev/null +++ b/apps/admin-api/src/api/tag/dto/tag-list.dto.ts @@ -0,0 +1,6 @@ +import { StringField } from '@repo/api'; + +export class TagListResDto { + @StringField({ each: true, expose: true }) + tags: string[]; +} diff --git a/apps/admin-api/src/api/tag/tag.controller.spec.ts b/apps/admin-api/src/api/tag/tag.controller.spec.ts index 3ac7b0a..656928b 100644 --- a/apps/admin-api/src/api/tag/tag.controller.spec.ts +++ b/apps/admin-api/src/api/tag/tag.controller.spec.ts @@ -1,12 +1,24 @@ import { Test, TestingModule } from '@nestjs/testing'; import { TagController } from './tag.controller'; +import { TagService } from './tag.service'; describe('TagController', () => { let controller: TagController; + let tagServiceValue: Partial>; + + beforeAll(async () => { + tagServiceValue = { + list: jest.fn(), + }; - beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ controllers: [TagController], + providers: [ + { + provide: TagService, + useValue: tagServiceValue, + }, + ], }).compile(); controller = module.get(TagController); diff --git a/apps/admin-api/src/api/tag/tag.controller.ts b/apps/admin-api/src/api/tag/tag.controller.ts index 3c05d6c..54e968e 100644 --- a/apps/admin-api/src/api/tag/tag.controller.ts +++ b/apps/admin-api/src/api/tag/tag.controller.ts @@ -1,6 +1,21 @@ -import { Controller } from '@nestjs/common'; +import { Controller, Get } from '@nestjs/common'; import { ApiTags } from '@nestjs/swagger'; +import { ApiAuth } from '@repo/api/decorators/http.decorators'; +import { TagListResDto } from './dto/tag-list.dto'; +import { TagService } from './tag.service'; @ApiTags('Tag') -@Controller('tag') -export class TagController {} +@Controller('tags') +export class TagController { + constructor(private readonly tagService: TagService) {} + + @Get() + @ApiAuth({ + summary: 'Get Tags', + type: TagListResDto, + isAuthOptional: true, + }) + async list(): Promise { + return await this.tagService.list(); + } +} diff --git a/apps/admin-api/src/api/tag/tag.module.ts b/apps/admin-api/src/api/tag/tag.module.ts index 4882f20..d3e34b5 100644 --- a/apps/admin-api/src/api/tag/tag.module.ts +++ b/apps/admin-api/src/api/tag/tag.module.ts @@ -1,8 +1,11 @@ import { Module } from '@nestjs/common'; +import { TypeOrmModule } from '@nestjs/typeorm'; +import { TagEntity } from '@repo/database-typeorm'; import { TagController } from './tag.controller'; import { TagService } from './tag.service'; @Module({ + imports: [TypeOrmModule.forFeature([TagEntity])], controllers: [TagController], providers: [TagService], }) diff --git a/apps/admin-api/src/api/tag/tag.service.spec.ts b/apps/admin-api/src/api/tag/tag.service.spec.ts index eedee15..1437989 100644 --- a/apps/admin-api/src/api/tag/tag.service.spec.ts +++ b/apps/admin-api/src/api/tag/tag.service.spec.ts @@ -1,12 +1,24 @@ import { Test, TestingModule } from '@nestjs/testing'; +import { getRepositoryToken } from '@nestjs/typeorm'; +import { TagEntity } from '@repo/database-typeorm'; +import { Repository } from 'typeorm'; import { TagService } from './tag.service'; describe('TagService', () => { let service: TagService; + let tagRepositoryValue: Partial< + Record, jest.Mock> + >; - beforeEach(async () => { + beforeAll(async () => { const module: TestingModule = await Test.createTestingModule({ - providers: [TagService], + providers: [ + TagService, + { + provide: getRepositoryToken(TagEntity), + useValue: tagRepositoryValue, + }, + ], }).compile(); service = module.get(TagService); diff --git a/apps/admin-api/src/api/tag/tag.service.ts b/apps/admin-api/src/api/tag/tag.service.ts index bb728fe..f2e4a15 100644 --- a/apps/admin-api/src/api/tag/tag.service.ts +++ b/apps/admin-api/src/api/tag/tag.service.ts @@ -1,4 +1,20 @@ import { Injectable } from '@nestjs/common'; +import { InjectRepository } from '@nestjs/typeorm'; +import { TagEntity } from '@repo/database-typeorm'; +import { Repository } from 'typeorm'; +import { TagListResDto } from './dto/tag-list.dto'; @Injectable() -export class TagService {} +export class TagService { + constructor( + @InjectRepository(TagEntity) + private readonly tagRepository: Repository, + ) {} + + async list(): Promise { + const tagEntities = await this.tagRepository.find(); + const tags = tagEntities.map((tag) => tag.name); + + return { tags }; + } +}