Skip to content

Commit

Permalink
refactor: convert category util to function
Browse files Browse the repository at this point in the history
  • Loading branch information
stae1102 committed Jan 31, 2024
1 parent eecc484 commit 72f7e17
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 213 deletions.
10 changes: 1 addition & 9 deletions src/categories/category.module.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { Module } from '@nestjs/common';
import { ContentsModule } from '../contents/contents.module';
import { CategoryService } from './category.service';
import { CategoryUtil } from '../contents/util/category.util';
import { OpenaiModule } from '../openai/openai.module';
import { UsersModule } from '../users/users.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Category } from './category.entity';
import { CategoryController } from './category.controller';
import { ContentRepository } from '../contents/repository/content.repository';
import { CategoryRepository } from './category.repository';
import { ContentUtil } from '../contents/util/content.util';

@Module({
imports: [
Expand All @@ -19,13 +17,7 @@ import { ContentUtil } from '../contents/util/content.util';
UsersModule,
],
controllers: [CategoryController],
providers: [
CategoryService,
CategoryUtil,
ContentRepository,
CategoryRepository,
ContentUtil,
],
providers: [CategoryService, ContentRepository, CategoryRepository],
exports: [CategoryRepository],
})
export class CategoryModule {}
11 changes: 4 additions & 7 deletions src/categories/category.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@ import {
NotFoundException,
} from '@nestjs/common';
import { Category } from './category.entity';
import { CategoryUtil } from '../contents/util/category.util';
import { User } from '../users/entities/user.entity';
import { generateSlug } from '../contents/util/category.util';

@Injectable()
export class CategoryRepository extends Repository<Category> {
constructor(
private readonly dataSource: DataSource,
private readonly categoryUtil: CategoryUtil,
) {
constructor(private readonly dataSource: DataSource) {
super(Category, dataSource.createEntityManager());
}

Expand All @@ -34,7 +31,7 @@ export class CategoryRepository extends Repository<Category> {
queryRunnerManager: EntityManager,
): Promise<Category> {
// generate category name and slug
const { categorySlug } = this.categoryUtil.generateSlug(categoryName);
const { categorySlug } = generateSlug(categoryName);

if (parentId) {
// category depth should be 3
Expand Down Expand Up @@ -112,7 +109,7 @@ export class CategoryRepository extends Repository<Category> {
.values(
defaultCategories.map((categoryName) => ({
name: categoryName,
slug: this.categoryUtil.generateSlug(categoryName).categorySlug,
slug: generateSlug(categoryName).categorySlug,
user: user,
})),
)
Expand Down
42 changes: 20 additions & 22 deletions src/categories/category.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,23 @@ import { Category } from './category.entity';
import { Content } from '../contents/entities/content.entity';
import { CategoryRepository } from './category.repository';
import { ContentRepository } from '../contents/repository/content.repository';
import { CategoryUtil } from '../contents/util/category.util';
import { ContentUtil } from '../contents/util/content.util';
import { getLinkContent, getLinkInfo } from '../contents/util/content.util';
import { OpenaiService } from '../openai/openai.service';
import { User } from '../users/entities/user.entity';
import { UserRepository } from '../users/repository/user.repository';
import {
generateCategoriesTree,
generateSlug,
loadLogs,
makeCategoryListWithSaveCount,
} from '../contents/util/category.util';

@Injectable()
export class CategoryService {
constructor(
private readonly contentRepository: ContentRepository,
private readonly categoryRepository: CategoryRepository,
private readonly categoryUtil: CategoryUtil,
private readonly userRepository: UserRepository,
private readonly contentUtil: ContentUtil,
private readonly openaiService: OpenaiService,
) {}

Expand All @@ -52,7 +55,7 @@ export class CategoryService {
throw new NotFoundException('User not found');
}

const { categorySlug } = this.categoryUtil.generateSlug(categoryName);
const { categorySlug } = generateSlug(categoryName);

if (parentId) {
// category depth should be 3
Expand Down Expand Up @@ -152,7 +155,7 @@ export class CategoryService {
if (category) {
// Check if user has category with same slug
if (categoryName) {
const { categorySlug } = this.categoryUtil.generateSlug(categoryName);
const { categorySlug } = generateSlug(categoryName);
if (
userInDb.categories?.filter(
(category) =>
Expand Down Expand Up @@ -296,8 +299,7 @@ export class CategoryService {
}

// make categories tree by parentid
const categoriesTree =
this.categoryUtil.generateCategoriesTree(categories);
const categoriesTree = generateCategoriesTree(categories);

return {
categoriesTree,
Expand All @@ -312,8 +314,7 @@ export class CategoryService {
): Promise<LoadFrequentCategoriesOutput> {
try {
// 로그 파일 내의 기록을 불러온다.
const recentCategoryList: RecentCategoryList[] =
this.categoryUtil.loadLogs(user.id);
const recentCategoryList: RecentCategoryList[] = loadLogs(user.id);

// 캐시 내의 카테고리 리스트를 최신 순으로 정렬하고, 동시에 저장된 횟수를 추가한다.

Expand All @@ -331,12 +332,11 @@ export class CategoryService {

// 10개의 로그를 확인한다.
i += 10;
recentCategoriesWithSaveCount =
this.categoryUtil.makeCategoryListWithSaveCount(
recentCategoryList,
recentCategoriesWithSaveCount,
i,
);
recentCategoriesWithSaveCount = makeCategoryListWithSaveCount(
recentCategoryList,
recentCategoriesWithSaveCount,
i,
);
// 10개의 로그를 확인했으므로 남은 로그 수를 10개 감소시킨다.
remainLogCount -= 10;

Expand Down Expand Up @@ -430,10 +430,9 @@ export class CategoryService {
categories.push(category.name);
}
});
const { title, siteName, description } =
await this.contentUtil.getLinkInfo(link);
const { title, siteName, description } = await getLinkInfo(link);

const content = await this.contentUtil.getLinkContent(link);
const content = await getLinkContent(link);

let questionLines = [
"You are a machine tasked with auto-categorizing articles based on information obtained through web scraping. You can only answer a single category name. Here is the article's information:",
Expand Down Expand Up @@ -489,15 +488,14 @@ export class CategoryService {
): Promise<AutoCategorizeOutput> {
try {
const { link, categories } = autoCategorizeBody;
const { title, siteName, description } =
await this.contentUtil.getLinkInfo(link);
const { title, siteName, description } = await getLinkInfo(link);

/**
* TODO: 본문 크롤링 개선 필요
* 현재 p 태그만 크롤링하는데, 불필요한 내용이 포함되는 경우가 많음
* 그러나 하나하나 예외 처리하는 방법을 제외하곤 방법을 못 찾은 상황
*/
const content = await this.contentUtil.getLinkContent(link);
const content = await getLinkContent(link);

let questionLines = [
"You are a machine tasked with auto-categorizing articles based on information obtained through web scraping. You can only answer a single category name. Here is the article's information:",
Expand Down
Loading

0 comments on commit 72f7e17

Please sign in to comment.