Skip to content

Commit

Permalink
Merge pull request #251 from Quickchive/refactor-#249/divide-contents…
Browse files Browse the repository at this point in the history
…-util

refactor: contents-util과 category-util을 함수로 변경
  • Loading branch information
stae1102 authored Jan 31, 2024
2 parents abb246a + 72f7e17 commit 30a8cd2
Show file tree
Hide file tree
Showing 7 changed files with 285 additions and 319 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
11 changes: 1 addition & 10 deletions src/contents/contents.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,16 @@ import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ContentsController } from './contents.controller';
import { ContentsService } from './contents.service';
import { Category } from '../categories/category.entity';
import { Content } from './entities/content.entity';
import { CategoryUtil } from './util/category.util';
import { ContentRepository } from './repository/content.repository';
import { CategoryRepository } from '../categories/category.repository';
import { UsersModule } from '../users/users.module';
import { ContentUtil } from './util/content.util';
import { OpenaiModule } from '../openai/openai.module';

@Module({
imports: [TypeOrmModule.forFeature([Content]), UsersModule, OpenaiModule],
controllers: [ContentsController],
providers: [
ContentsService,
ContentRepository,
ContentUtil,
CategoryRepository,
CategoryUtil,
],
providers: [ContentsService, ContentRepository, CategoryRepository],
exports: [ContentsService],
})
export class ContentsModule {}
21 changes: 10 additions & 11 deletions src/contents/contents.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ import { Content } from './entities/content.entity';
import { LoadReminderCountOutput } from './dtos/load-personal-remider-count.dto';
import { UserRepository } from '../users/repository/user.repository';
import { ContentRepository } from './repository/content.repository';
import { CategoryUtil } from './util/category.util';
import { CategoryRepository } from '../categories/category.repository';
import { ContentUtil } from './util/content.util';
import { getLinkInfo } from './util/content.util';
import { GetLinkInfoResponseDto } from './dtos/get-link.response.dto';
import { checkContentDuplicateAndAddCategorySaveLog } from './util/category.util';

@Injectable()
export class ContentsService {
Expand All @@ -39,8 +39,6 @@ export class ContentsService {
private readonly contentRepository: ContentRepository,
private readonly summaryService: SummaryService,
private readonly categoryRepository: CategoryRepository,
private readonly categoryUtil: CategoryUtil,
private readonly contentUtil: ContentUtil,
private readonly dataSource: DataSource,
) {}

Expand Down Expand Up @@ -73,7 +71,7 @@ export class ContentsService {
siteName,
description,
coverImg,
} = await this.contentUtil.getLinkInfo(link);
} = await getLinkInfo(link);
title = title ? title : linkTitle;

let category: Category | null = null;
Expand All @@ -85,7 +83,7 @@ export class ContentsService {
queryRunner.manager,
);

await this.categoryUtil.checkContentDuplicateAndAddCategorySaveLog(
await checkContentDuplicateAndAddCategorySaveLog(
link,
category,
userInDb,
Expand Down Expand Up @@ -141,11 +139,12 @@ export class ContentsService {
);
}
for (const link of contentLinks) {
const { title, description, coverImg, siteName } =
await this.contentUtil.getLinkInfo(link);
const { title, description, coverImg, siteName } = await getLinkInfo(
link,
);

if (category) {
await this.categoryUtil.checkContentDuplicateAndAddCategorySaveLog(
await checkContentDuplicateAndAddCategorySaveLog(
link,
category,
userInDb,
Expand Down Expand Up @@ -220,7 +219,7 @@ export class ContentsService {
queryRunnerManager,
);

await this.categoryUtil.checkContentDuplicateAndAddCategorySaveLog(
await checkContentDuplicateAndAddCategorySaveLog(
link,
category,
userInDb,
Expand Down Expand Up @@ -399,7 +398,7 @@ export class ContentsService {
}

async getLinkInfo(link: string) {
const data = await this.contentUtil.getLinkInfo(link);
const data = await getLinkInfo(link);

return new GetLinkInfoResponseDto(data);
}
Expand Down
Loading

0 comments on commit 30a8cd2

Please sign in to comment.