From 7338eb4f29e6766fb713efd4221961325c285ea5 Mon Sep 17 00:00:00 2001 From: stae1102 Date: Sun, 11 Feb 2024 16:34:27 +0900 Subject: [PATCH] refactor: replace entity manager in content service update content method --- src/contents/contents.service.ts | 68 ++++++++++++++++---------------- 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/src/contents/contents.service.ts b/src/contents/contents.service.ts index 27bc4bb..bda6778 100644 --- a/src/contents/contents.service.ts +++ b/src/contents/contents.service.ts @@ -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()