Skip to content

Commit

Permalink
refactor: replace entity manager in content service update content me…
Browse files Browse the repository at this point in the history
…thod
  • Loading branch information
stae1102 committed Feb 11, 2024
1 parent 790fe9d commit 7338eb4
Showing 1 changed file with 35 additions and 33 deletions.
68 changes: 35 additions & 33 deletions src/contents/contents.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,44 +185,46 @@ export class ContentsService {
reminder,
favorite,
};
try {
const userInDb =
await this.userRepository.findOneWithContentsAndCategories(user.id);
if (!userInDb) {
throw new NotFoundException('User not found');
}
const userInDb = await this.userRepository.findOneWithContentsAndCategories(
user.id,
);
if (!userInDb) {
throw new NotFoundException('User not found');
}

const content = userInDb?.contents?.filter(
(content) => content.id === contentId,
)[0];
if (!content) {
throw new NotFoundException('Content not found.');
}
const content = userInDb?.contents?.filter(
(content) => content.id === contentId,
)[0];
if (!content) {
throw new NotFoundException('Content not found.');
}

let category: Category | null = null;
if (categoryName) {
category = await this.categoryRepository.getOrCreateCategory(
categoryName,
parentId,
userInDb,
entityManager!,
);
let category: Category | undefined = undefined;
if (categoryName) {
category = await this.categoryRepository.getOrCreateCategory(
categoryName,
parentId,
userInDb,
entityManager,
);

await checkContentDuplicateAndAddCategorySaveLog(
link,
category,
userInDb,
);
}
await checkContentDuplicateAndAddCategorySaveLog(
link,
category,
userInDb,
);
}

await entityManager!.save(Content, [
{ id: content.id, ...newContentObj, ...(category && { category }) },
]);
await this.contentRepository.updateOne(
{
id: content.id,
...newContentObj,
category,
},
entityManager,
);

return {};
} catch (e) {
throw e;
}
return {};
}

@Transactional()
Expand Down

0 comments on commit 7338eb4

Please sign in to comment.