From f8491fda6d0de1a62fe6cc81ce55e4b242bfdd6a Mon Sep 17 00:00:00 2001 From: stae1102 Date: Sun, 12 Jan 2025 20:19:37 +0900 Subject: [PATCH] fix: change content updating api --- src/categories/category.service.ts | 4 +- src/contents/contents.service.ts | 50 ++++++++++++++----------- src/contents/dtos/content.dto.ts | 6 +++ src/contents/entities/content.entity.ts | 13 +++++-- 4 files changed, 47 insertions(+), 26 deletions(-) diff --git a/src/categories/category.service.ts b/src/categories/category.service.ts index 01fd617..4365ccc 100644 --- a/src/categories/category.service.ts +++ b/src/categories/category.service.ts @@ -241,7 +241,7 @@ export class CategoryService { ? await queryRunnerManager.findOneOrFail(Category, { where: { id: category.parentId }, }) - : undefined; + : null; // find children categories const childrenCategories = await queryRunnerManager.find(Category, { @@ -522,7 +522,7 @@ You can only answer a single category name. Here is the article's information: } ${siteName && `site name: "${siteName.trim()}"`} -Given the categories below, please provide the most suitable category for the article following the rules. +Given the categories below, please provide suitable category for the article following the rules. [RULES] - The deeper the category depth, the more specific the category is. - If the 1, 2, and 3 depth categories are equally worthy of saving links, then the deeper categories should be recommended more. diff --git a/src/contents/contents.service.ts b/src/contents/contents.service.ts index 76bd3c9..2a57f2c 100644 --- a/src/contents/contents.service.ts +++ b/src/contents/contents.service.ts @@ -131,7 +131,7 @@ export class ContentsService { } if (contentLinks.length > 0) { - let category: Category | undefined = undefined; + let category: Category | null = null; if (categoryName) { category = await this.categoryRepository.getOrCreateCategory( categoryName, @@ -183,6 +183,7 @@ export class ContentsService { comment, reminder, favorite, + categoryId, categoryName, parentId, }: UpdateContentBodyDto, @@ -210,31 +211,38 @@ export class ContentsService { throw new NotFoundException('Content not found.'); } - let category: Category | undefined = undefined; - if (categoryName) { - category = await this.categoryRepository.getOrCreateCategory( - categoryName, - parentId, - userInDb, + if (categoryId !== undefined) { + const category = + categoryId !== null + ? await this.categoryRepository.findById(categoryId, entityManager) + : null; + + if (category) { + await checkContentDuplicateAndAddCategorySaveLog( + link, + category, + userInDb, + ); + } + + await this.contentRepository.updateOne( + { + id: content.id, + ...newContentObj, + category, + }, entityManager, ); - - await checkContentDuplicateAndAddCategorySaveLog( - link, - category, - userInDb, + } else { + await this.contentRepository.updateOne( + { + id: content.id, + ...newContentObj, + }, + entityManager, ); } - await this.contentRepository.updateOne( - { - id: content.id, - ...newContentObj, - category, - }, - entityManager, - ); - return {}; } diff --git a/src/contents/dtos/content.dto.ts b/src/contents/dtos/content.dto.ts index 103bbf5..25384e5 100644 --- a/src/contents/dtos/content.dto.ts +++ b/src/contents/dtos/content.dto.ts @@ -104,6 +104,12 @@ export class AddMultipleContentsBodyDto { @IsOptional() categoryName?: string; + @ApiProperty({ description: '카테고리 id', required: false }) + @IsInt() + @IsPositive() + @IsOptional() + categoryId?: number; + @ApiProperty({ description: '부모 카테고리 id', example: 1, diff --git a/src/contents/entities/content.entity.ts b/src/contents/entities/content.entity.ts index e261094..a0faa39 100644 --- a/src/contents/entities/content.entity.ts +++ b/src/contents/entities/content.entity.ts @@ -62,13 +62,20 @@ export class Content extends CoreEntity { @IsBoolean() favorite?: boolean; - @ApiProperty({ description: 'Article Category', required: false }) + @ApiProperty({ + description: 'Article Category', + required: false, + default: null, + }) @ManyToOne(() => Category, (category) => category.contents, { nullable: true, onDelete: 'SET NULL', }) - @JoinColumn({ name: 'categoryId', referencedColumnName: 'id' }) - category?: Category; + @JoinColumn({ + name: 'categoryId', + referencedColumnName: 'id', + }) + category: Category | null; @ManyToOne(() => User, (user) => user.contents, { onDelete: 'CASCADE',