Skip to content

Commit

Permalink
Merge pull request #272 from Quickchive/refactor/#270-add-multiple-co…
Browse files Browse the repository at this point in the history
…ntent

refactor: replace entity manager in multiple contents adding method
  • Loading branch information
stae1102 authored Feb 11, 2024
2 parents 5057317 + 2aa1652 commit 69fb55a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 38 deletions.
64 changes: 32 additions & 32 deletions src/contents/contents.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,25 +111,27 @@ export class ContentsService {
{ contentLinks, categoryName, parentId }: AddMultipleContentsBodyDto,
entityManager?: EntityManager,
): Promise<AddContentOutput> {
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,
);
Expand All @@ -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()
Expand Down
14 changes: 8 additions & 6 deletions src/contents/entities/content.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 })
Expand Down Expand Up @@ -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;
}

0 comments on commit 69fb55a

Please sign in to comment.