Skip to content

Commit dd7c6db

Browse files
authored
Merge pull request #378 from Quickchive/fix/change-patch-content-api
fix: change content updating api
2 parents faf5319 + f8491fd commit dd7c6db

File tree

4 files changed

+47
-26
lines changed

4 files changed

+47
-26
lines changed

src/categories/category.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ export class CategoryService {
241241
? await queryRunnerManager.findOneOrFail(Category, {
242242
where: { id: category.parentId },
243243
})
244-
: undefined;
244+
: null;
245245

246246
// find children categories
247247
const childrenCategories = await queryRunnerManager.find(Category, {
@@ -522,7 +522,7 @@ You can only answer a single category name. Here is the article's information:
522522
}</description>
523523
<siteName>${siteName && `site name: "${siteName.trim()}"`}</siteName>
524524
525-
Given the categories below, please provide the most suitable category for the article following the rules.
525+
Given the categories below, please provide suitable category for the article following the rules.
526526
[RULES]
527527
- The deeper the category depth, the more specific the category is.
528528
- If the 1, 2, and 3 depth categories are equally worthy of saving links, then the deeper categories should be recommended more.

src/contents/contents.service.ts

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export class ContentsService {
131131
}
132132

133133
if (contentLinks.length > 0) {
134-
let category: Category | undefined = undefined;
134+
let category: Category | null = null;
135135
if (categoryName) {
136136
category = await this.categoryRepository.getOrCreateCategory(
137137
categoryName,
@@ -183,6 +183,7 @@ export class ContentsService {
183183
comment,
184184
reminder,
185185
favorite,
186+
categoryId,
186187
categoryName,
187188
parentId,
188189
}: UpdateContentBodyDto,
@@ -210,31 +211,38 @@ export class ContentsService {
210211
throw new NotFoundException('Content not found.');
211212
}
212213

213-
let category: Category | undefined = undefined;
214-
if (categoryName) {
215-
category = await this.categoryRepository.getOrCreateCategory(
216-
categoryName,
217-
parentId,
218-
userInDb,
214+
if (categoryId !== undefined) {
215+
const category =
216+
categoryId !== null
217+
? await this.categoryRepository.findById(categoryId, entityManager)
218+
: null;
219+
220+
if (category) {
221+
await checkContentDuplicateAndAddCategorySaveLog(
222+
link,
223+
category,
224+
userInDb,
225+
);
226+
}
227+
228+
await this.contentRepository.updateOne(
229+
{
230+
id: content.id,
231+
...newContentObj,
232+
category,
233+
},
219234
entityManager,
220235
);
221-
222-
await checkContentDuplicateAndAddCategorySaveLog(
223-
link,
224-
category,
225-
userInDb,
236+
} else {
237+
await this.contentRepository.updateOne(
238+
{
239+
id: content.id,
240+
...newContentObj,
241+
},
242+
entityManager,
226243
);
227244
}
228245

229-
await this.contentRepository.updateOne(
230-
{
231-
id: content.id,
232-
...newContentObj,
233-
category,
234-
},
235-
entityManager,
236-
);
237-
238246
return {};
239247
}
240248

src/contents/dtos/content.dto.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ export class AddMultipleContentsBodyDto {
104104
@IsOptional()
105105
categoryName?: string;
106106

107+
@ApiProperty({ description: '카테고리 id', required: false })
108+
@IsInt()
109+
@IsPositive()
110+
@IsOptional()
111+
categoryId?: number;
112+
107113
@ApiProperty({
108114
description: '부모 카테고리 id',
109115
example: 1,

src/contents/entities/content.entity.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,20 @@ export class Content extends CoreEntity {
6262
@IsBoolean()
6363
favorite?: boolean;
6464

65-
@ApiProperty({ description: 'Article Category', required: false })
65+
@ApiProperty({
66+
description: 'Article Category',
67+
required: false,
68+
default: null,
69+
})
6670
@ManyToOne(() => Category, (category) => category.contents, {
6771
nullable: true,
6872
onDelete: 'SET NULL',
6973
})
70-
@JoinColumn({ name: 'categoryId', referencedColumnName: 'id' })
71-
category?: Category;
74+
@JoinColumn({
75+
name: 'categoryId',
76+
referencedColumnName: 'id',
77+
})
78+
category: Category | null;
7279

7380
@ManyToOne(() => User, (user) => user.contents, {
7481
onDelete: 'CASCADE',

0 commit comments

Comments
 (0)