diff --git a/src/contents/contents.service.ts b/src/contents/contents.service.ts index 23f2282..27bc4bb 100644 --- a/src/contents/contents.service.ts +++ b/src/contents/contents.service.ts @@ -111,25 +111,27 @@ export class ContentsService { { contentLinks, categoryName, parentId }: AddMultipleContentsBodyDto, entityManager?: EntityManager, ): Promise { - try { - const userInDb = - await this.userRepository.findOneWithContentsAndCategories(user.id); + const userInDb = await this.userRepository.findOneWithContentsAndCategories( + user.id, + ); - if (!userInDb) { - throw new NotFoundException('User not found'); + if (!userInDb) { + throw new NotFoundException('User not found'); + } + + if (contentLinks.length > 0) { + let category: Category | undefined = undefined; + if (categoryName) { + category = await this.categoryRepository.getOrCreateCategory( + categoryName, + parentId, + userInDb, + entityManager, + ); } - if (contentLinks.length > 0) { - let category: Category | null = null; - if (categoryName) { - category = await this.categoryRepository.getOrCreateCategory( - categoryName, - parentId, - userInDb, - entityManager!, - ); - } - for (const link of contentLinks) { + await Promise.all( + contentLinks.map(async (link) => { const { title, description, coverImg, siteName } = await getLinkInfo( link, ); @@ -142,23 +144,21 @@ export class ContentsService { ); } - const newContent = entityManager!.create(Content, { - link, - title, - siteName, - coverImg, - ...(category && { category }), - description, - user: userInDb, - }); - await this.contentRepository.createOne(newContent, entityManager); - } - } - - return {}; - } catch (e) { - throw e; + const content = new Content(); + content.link = link; + content.title = title; + content.siteName = siteName; + content.coverImg = coverImg; + content.category = category; + content.description = description; + content.user = userInDb; + + await this.contentRepository.createOne(content, entityManager); + }), + ); } + + return {}; } @Transactional() diff --git a/src/contents/entities/content.entity.ts b/src/contents/entities/content.entity.ts index 10993d5..e261094 100644 --- a/src/contents/entities/content.entity.ts +++ b/src/contents/entities/content.entity.ts @@ -7,7 +7,7 @@ import { } from 'class-validator'; import { CoreEntity } from '../../common/entities/core.entity'; import { User } from '../../users/entities/user.entity'; -import { Column, Entity, ManyToOne, RelationId } from 'typeorm'; +import { Column, Entity, JoinColumn, ManyToOne, RelationId } from 'typeorm'; import { ApiProperty } from '@nestjs/swagger'; import { Category } from '../../categories/category.entity'; import { Transform } from 'class-transformer'; @@ -18,7 +18,7 @@ export class Content extends CoreEntity { @Column() @IsString({ message: 'String URL must be required.' }) @IsUrl() - link!: string; + link: string; @ApiProperty({ description: 'Article Title', required: false }) @Column({ nullable: true }) @@ -63,18 +63,20 @@ export class Content extends CoreEntity { favorite?: boolean; @ApiProperty({ description: 'Article Category', required: false }) - @ManyToOne((type) => Category, (category) => category.contents, { + @ManyToOne(() => Category, (category) => category.contents, { nullable: true, onDelete: 'SET NULL', }) + @JoinColumn({ name: 'categoryId', referencedColumnName: 'id' }) category?: Category; - @ManyToOne((type) => User, (user) => user.contents, { + @ManyToOne(() => User, (user) => user.contents, { onDelete: 'CASCADE', }) - user!: User; + @JoinColumn({ name: 'userId', referencedColumnName: 'id' }) + user: User; @ApiProperty({ description: 'Owner ID' }) @RelationId((content: Content) => content.user) - userId!: number; + userId: number; }