Skip to content

Commit

Permalink
Merge pull request #351 from Quickchive/fix/QA-347/delete-category
Browse files Browse the repository at this point in the history
[QA-347] 콘텐츠 추가 시, 카테고리 id를 받도록 수정
  • Loading branch information
stae1102 authored Sep 16, 2024
2 parents 44d65ca + 1670027 commit e0da7f4
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 24 deletions.
22 changes: 12 additions & 10 deletions src/contents/contents.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ export class ContentsService {
comment,
reminder,
favorite,
categoryName,
parentId,
categoryId,
}: AddContentBodyDto,
entityManager?: EntityManager,
): Promise<AddContentOutput> {
Expand All @@ -64,6 +64,8 @@ export class ContentsService {
throw new NotFoundException('User not found');
}

categoryId = categoryId ? categoryId : parentId;

const {
title: linkTitle,
siteName,
Expand All @@ -72,32 +74,32 @@ export class ContentsService {
} = await getLinkInfo(link);
title = title ? title : linkTitle;

let category: Category | undefined = undefined;
if (categoryName) {
category = await this.categoryRepository.getOrCreateCategory(
// TODO 명령과 조회를 분리
categoryName,
parentId,
userInDb,
const content = new Content();

if (categoryId) {
const category = await this.categoryRepository.findById(
categoryId,
entityManager,
);

if (!category) throw new NotFoundException('Category not found');

await checkContentDuplicateAndAddCategorySaveLog(
link,
category,
userInDb,
);

content.category = category;
}

const content = new Content();
content.link = link;
content.title = title;
content.siteName = siteName;
content.coverImg = coverImg;
content.description = description;
content.comment = comment;
content.reminder = reminder;
content.category = category;
content.user = user;
content.favorite = favorite;

Expand Down
82 changes: 68 additions & 14 deletions src/contents/dtos/content.dto.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,90 @@
import {
ApiProperty,
ApiPropertyOptional,
IntersectionType,
PartialType,
PickType,
} from '@nestjs/swagger';
import { IsNumber, IsOptional, IsString } from 'class-validator';
import {
IsBoolean,
IsDate,
IsInt,
IsNotEmpty,
IsNumber,
IsOptional,
IsPositive,
IsString,
} from 'class-validator';
import { CoreOutput } from '../../common/dtos/output.dto';
import { Content } from '../entities/content.entity';
import { Type } from 'class-transformer';

class ContentBodyExceptLink extends PartialType(
PickType(Content, ['title', 'comment', 'reminder', 'favorite']),
) {
@ApiProperty({ description: 'Category Name', required: false })
export class AddContentBodyDto {
@ApiProperty({ example: 'ex.com', description: '아티클 주소' })
@IsString()
link: string;

@ApiPropertyOptional({
description: '아티클 제목',
type: String,
})
@IsOptional()
@IsString()
title?: string;

@ApiPropertyOptional({
description: '아티클 설명/메모',
type: String,
})
@IsOptional()
@IsString()
comment?: string;

@ApiPropertyOptional({
description: 'Article Reminder Date(YYYY-MM-DD HH:mm:ss)',
type: Date,
})
@IsOptional()
@IsDate()
@Type(() => Date)
reminder?: Date;

@ApiPropertyOptional({
description: '즐겨찾기 여부',
type: Boolean,
})
@IsOptional()
@IsBoolean()
favorite?: boolean;

@ApiPropertyOptional({
description: '카테고리 이름',
type: String,
})
@IsOptional()
@IsString()
@IsNotEmpty()
categoryName?: string;

@ApiProperty({
@ApiPropertyOptional({
description: '부모 카테고리 id',
example: 1,
required: false,
type: Number,
})
@IsNumber()
@IsOptional()
@IsInt()
@IsPositive()
parentId?: number;

@ApiPropertyOptional({
description: '카테고리 id',
type: Number,
})
@IsOptional()
@IsInt()
@IsPositive()
categoryId?: number;
}
class ContentBodyWithLinkOnly extends PickType(Content, ['link']) {}

export class AddContentBodyDto extends IntersectionType(
ContentBodyWithLinkOnly,
ContentBodyExceptLink,
) {}
export class AddContentOutput extends CoreOutput {}

export class AddMultipleContentsBodyDto {
Expand Down

0 comments on commit e0da7f4

Please sign in to comment.