Skip to content

Commit

Permalink
[Server] ai와 관련한 db 구조 개선 (#220)
Browse files Browse the repository at this point in the history
* refactor: 경로 이름 수정 entity->entities

* feat: category 엔티티 생성

* feat: reason 모델 생성 및 각 테이블 relation 설정

* feat: 각 테이블 relation 수정 및 의존성 주입

* feat: relation 방식 변경으로 인한folders.service.ts 메서드 수정, 의존성 주입
  • Loading branch information
pminsung12 authored Dec 10, 2023
1 parent 462fe15 commit 6f0b7a8
Show file tree
Hide file tree
Showing 19 changed files with 143 additions and 48 deletions.
8 changes: 7 additions & 1 deletion server/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { ChecklistAiModule } from './checklist-ai/checklist-ai.module';
import { CommonModule } from './common/common.module';
import { LoggingInterceptor } from './common/interceptor/log.interceptor';
import { LoggerMiddleware } from './common/middlewares/logger.middleware';
import { FeedModel } from './feeds/entity/feed.entity';
import { FeedModel } from './feeds/entities/feed.entity';
import { FeedsModule } from './feeds/feeds.module';
import { FolderModel } from './folders/entities/folder.entity';
import { FoldersModule } from './folders/folders.module';
Expand All @@ -30,6 +30,9 @@ import { SharedChecklistsModule } from './shared-checklists/shared-checklists.mo
import { UserModel } from './users/entities/user.entity';
import { UsersModule } from './users/users.module';
import { winstonConfig } from './utils/winston.config';
import { CategoryModel } from './categories/entities/category.entity';
import { AiChecklistItemModel } from './checklist-ai/entities/ai-checklist-item';
import { AiChecklistItemNaverReasonModel } from './checklist-ai/entities/ai-checklist-item-naver-reason.entity';

@Module({
imports: [
Expand All @@ -52,6 +55,9 @@ import { winstonConfig } from './utils/winston.config';
SharedChecklistModel,
SharedChecklistItemModel,
FeedModel,
CategoryModel,
AiChecklistItemModel,
AiChecklistItemNaverReasonModel,
],
synchronize: true, // DO NOT USE IN PRODUCTION
}),
Expand Down
4 changes: 4 additions & 0 deletions server/src/categories/categories.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { Module } from '@nestjs/common';
import { CategoriesService } from './categories.service';
import { CategoriesController } from './categories.controller';
import { TypeOrmModule } from '@nestjs/typeorm';
import { CategoryModel } from './entities/category.entity';

@Module({
imports: [TypeOrmModule.forFeature([CategoryModel])],
controllers: [CategoriesController],
providers: [CategoriesService],
exports: [TypeOrmModule],
})
export class CategoriesModule {}
17 changes: 17 additions & 0 deletions server/src/categories/entities/category.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
import { BaseModel } from '../../common/entities/base.entity';

@Entity()
export class CategoryModel extends BaseModel {
@PrimaryGeneratedColumn()
categoryId: number;

@Column()
mainCategory: string;

@Column()
subCategory: string;

@Column()
minorCategory: string;
}
11 changes: 10 additions & 1 deletion server/src/checklist-ai/checklist-ai.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@ import { Module } from '@nestjs/common';
import { ChecklistAiService } from './checklist-ai.service';
import { ChecklistAiController } from './checklist-ai.controller';
import { HttpModule } from '@nestjs/axios';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AiChecklistItemModel } from './entities/ai-checklist-item';
import { AiChecklistItemNaverReasonModel } from './entities/ai-checklist-item-naver-reason.entity';

@Module({
imports: [HttpModule],
imports: [
HttpModule,
TypeOrmModule.forFeature([
AiChecklistItemModel,
AiChecklistItemNaverReasonModel,
]),
],
controllers: [ChecklistAiController],
providers: [ChecklistAiService],
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from 'typeorm';
import { BaseModel } from '../../common/entities/base.entity';
import { AiChecklistItemModel } from './ai-checklist-item';

@Entity()
export class AiChecklistItemNaverReasonModel extends BaseModel {
@PrimaryGeneratedColumn()
aiChecklistItemNaverReasonid: number;

@Column()
reason: string;

@ManyToOne(
() => AiChecklistItemModel,
(aiChecklistItem) => aiChecklistItem.reasons,
{
nullable: false,
},
)
aiChecklistItem: AiChecklistItemModel;
}
43 changes: 43 additions & 0 deletions server/src/checklist-ai/entities/ai-checklist-item.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {
Column,
Entity,
JoinColumn,
ManyToOne,
OneToMany,
PrimaryGeneratedColumn,
} from 'typeorm';
import { AiChecklistItemNaverReasonModel } from './ai-checklist-item-naver-reason.entity';
import { BaseModel } from '../../common/entities/base.entity';
import { CategoryModel } from '../../categories/entities/category.entity';

@Entity()
export class AiChecklistItemModel extends BaseModel {
@PrimaryGeneratedColumn()
aiChecklistItemId: number;

@Column()
content: string;

@Column({ default: 0 })
selected_count_by_user: number;

@Column({ default: 0 })
selected_count_by_naver_ai: number;

@Column({ default: 0 })
evaluated_count_by_naver_ai: number;

// final_score는 user_selected_count + naver_ai_selected_count + naver_ai_evaluate_count
@Column({ default: 0 })
final_score: number;

@ManyToOne(() => CategoryModel)
@JoinColumn({ name: 'categoryId' })
category: CategoryModel;

@OneToMany(
() => AiChecklistItemNaverReasonModel,
(reason) => reason.aiChecklistItem,
)
reasons: AiChecklistItemNaverReasonModel[];
}
17 changes: 0 additions & 17 deletions server/src/checklist-ai/entities/checklist-ai.entity.ts

This file was deleted.

File renamed without changes.
12 changes: 12 additions & 0 deletions server/src/common/entities/checklist.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { BaseModel } from './base.entity';
import { Column, JoinColumn, ManyToOne } from 'typeorm';
import { CategoryModel } from '../../categories/entities/category.entity';

export abstract class ChecklistModel extends BaseModel {
@Column()
title: string;

@ManyToOne(() => CategoryModel)
@JoinColumn({ name: 'categoryId' })
category: CategoryModel;
}
17 changes: 0 additions & 17 deletions server/src/common/entity/checklist.entity.ts

This file was deleted.

File renamed without changes.
5 changes: 3 additions & 2 deletions server/src/feeds/feeds.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { Module } from '@nestjs/common';
import { FeedsService } from './feeds.service';
import { FeedsController } from './feeds.controller';
import { TypeOrmModule } from '@nestjs/typeorm';
import { FeedModel } from './entity/feed.entity';
import { FeedModel } from './entities/feed.entity';
import { CategoriesModule } from '../categories/categories.module';

@Module({
imports: [TypeOrmModule.forFeature([FeedModel])],
imports: [TypeOrmModule.forFeature([FeedModel]), CategoriesModule],
controllers: [FeedsController],
providers: [FeedsService],
})
Expand Down
2 changes: 1 addition & 1 deletion server/src/feeds/feeds.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { BadRequestException } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { FeedModel } from './entity/feed.entity';
import { FeedModel } from './entities/feed.entity';
import { FeedsService } from './feeds.service';

type MockRepository<T = any> = Partial<Record<keyof Repository<T>, jest.Mock>>;
Expand Down
24 changes: 20 additions & 4 deletions server/src/feeds/feeds.service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { BadRequestException, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { FeedModel } from './entity/feed.entity';
import { FeedModel } from './entities/feed.entity';
import { Repository } from 'typeorm';
import { CategoryModel } from '../categories/entities/category.entity';

@Injectable()
export class FeedsService {
constructor(
@InjectRepository(FeedModel)
private readonly repository: Repository<FeedModel>,
@InjectRepository(CategoryModel)
private readonly categoryRepository: Repository<CategoryModel>,
) {}

async findFeedById(feedId: number) {
Expand All @@ -21,13 +24,26 @@ export class FeedsService {
}

async findAllFeedsByCategory(mainCategory: string) {
const feed = await this.repository.find({ where: { mainCategory } });
if (feed.length === 0) {
// mainCategory를 사용하여 CategoryModel 찾기
const category = await this.categoryRepository.findOne({
where: { mainCategory },
});
if (!category) {
throw new BadRequestException(
`${mainCategory}는 존재하지 않는 카테고리입니다.`,
);
}

// 해당 카테고리 ID를 가진 모든 피드 검색
const feeds = await this.repository.find({
where: { category: category },
});
if (feeds.length === 0) {
throw new BadRequestException(
`${mainCategory}에 대한 피드가 존재하지 않습니다.`,
);
}
return feed;
return feeds;
}

async updateLikeCount(feedId: number) {
Expand Down
2 changes: 1 addition & 1 deletion server/src/folders/entities/folder.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
OneToMany,
PrimaryGeneratedColumn,
} from 'typeorm';
import { BaseModel } from '../../common/entity/base.entity';
import { BaseModel } from '../../common/entities/base.entity';
import { UserModel } from '../../users/entities/user.entity';
import { PrivateChecklistModel } from '../private-checklists/entities/private-checklist.entity';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IsBoolean, IsString } from 'class-validator';
import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from 'typeorm';
import { ChecklistModel } from '../../../common/entity/checklist.entity';
import { ChecklistModel } from '../../../common/entities/checklist.entity';
import { UserModel } from '../../../users/entities/user.entity';
import { FolderModel } from '../../entities/folder.entity';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BaseModel } from 'src/common/entity/base.entity';
import { BaseModel } from 'src/common/entities/base.entity';
import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from 'typeorm';
import { SharedChecklistModel } from './shared-checklist.entity';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
OneToMany,
PrimaryColumn,
} from 'typeorm';
import { ChecklistModel } from '../../common/entity/checklist.entity';
import { ChecklistModel } from '../../common/entities/checklist.entity';
import { UserModel } from '../../users/entities/user.entity';
import { SharedChecklistItemModel } from './shared-checklist-item.entity';

Expand Down
2 changes: 1 addition & 1 deletion server/src/users/entities/user.entity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BaseModel } from 'src/common/entity/base.entity';
import { BaseModel } from 'src/common/entities/base.entity';
import {
Column,
Entity,
Expand Down

0 comments on commit 6f0b7a8

Please sign in to comment.