Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add tags api #66

Merged
merged 1 commit into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/admin-api/src/api/profile/profile.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('ProfileController', () => {
let controller: ProfileController;
let profileServiceValue: Partial<Record<keyof ProfileService, jest.Mock>>;

beforeEach(async () => {
beforeAll(async () => {
profileServiceValue = {
getProfile: jest.fn(),
follow: jest.fn(),
Expand Down
2 changes: 1 addition & 1 deletion apps/admin-api/src/api/profile/profile.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('ProfileService', () => {
Record<keyof Repository<UserFollowsEntity>, jest.Mock>
>;

beforeEach(async () => {
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
ProfileService,
Expand Down
6 changes: 6 additions & 0 deletions apps/admin-api/src/api/tag/dto/tag-list.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { StringField } from '@repo/api';

export class TagListResDto {
@StringField({ each: true, expose: true })
tags: string[];
}
14 changes: 13 additions & 1 deletion apps/admin-api/src/api/tag/tag.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -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<Record<keyof TagService, jest.Mock>>;

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>(TagController);
Expand Down
21 changes: 18 additions & 3 deletions apps/admin-api/src/api/tag/tag.controller.ts
Original file line number Diff line number Diff line change
@@ -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<TagListResDto> {
return await this.tagService.list();
}
}
3 changes: 3 additions & 0 deletions apps/admin-api/src/api/tag/tag.module.ts
Original file line number Diff line number Diff line change
@@ -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],
})
Expand Down
16 changes: 14 additions & 2 deletions apps/admin-api/src/api/tag/tag.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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<keyof Repository<TagEntity>, 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>(TagService);
Expand Down
18 changes: 17 additions & 1 deletion apps/admin-api/src/api/tag/tag.service.ts
Original file line number Diff line number Diff line change
@@ -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<TagEntity>,
) {}

async list(): Promise<TagListResDto> {
const tagEntities = await this.tagRepository.find();
const tags = tagEntities.map((tag) => tag.name);

return { tags };
}
}