From 790fe9d08f0a3b21473065f1bdcff9984a839ce2 Mon Sep 17 00:00:00 2001 From: stae1102 Date: Sun, 11 Feb 2024 16:34:06 +0900 Subject: [PATCH 1/2] feat: create update method at content repository --- src/contents/repository/content.repository.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/contents/repository/content.repository.ts b/src/contents/repository/content.repository.ts index 106ed80..6c194e5 100644 --- a/src/contents/repository/content.repository.ts +++ b/src/contents/repository/content.repository.ts @@ -50,4 +50,10 @@ export class ContentRepository extends Repository { ): Promise { return entityManager ? entityManager.save(content) : this.save(content); } + + async updateOne(content: Partial, entityManager?: EntityManager) { + return entityManager + ? entityManager.update(Content, { id: content.id }, content) + : this.update({ id: content.id }, content); + } } From 7338eb4f29e6766fb713efd4221961325c285ea5 Mon Sep 17 00:00:00 2001 From: stae1102 Date: Sun, 11 Feb 2024 16:34:27 +0900 Subject: [PATCH 2/2] 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()