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() 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); + } }