Skip to content

Commit

Permalink
Merge pull request #273 from Quickchive/refactor/#271-update-content
Browse files Browse the repository at this point in the history
Refactor/#271 update content
  • Loading branch information
stae1102 authored Feb 11, 2024
2 parents 69fb55a + 7338eb4 commit e009e1f
Show file tree
Hide file tree
Showing 2 changed files with 41 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
6 changes: 6 additions & 0 deletions src/contents/repository/content.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,10 @@ export class ContentRepository extends Repository<Content> {
): Promise<Content> {
return entityManager ? entityManager.save(content) : this.save(content);
}

async updateOne(content: Partial<Content>, entityManager?: EntityManager) {
return entityManager
? entityManager.update(Content, { id: content.id }, content)
: this.update({ id: content.id }, content);
}
}

0 comments on commit e009e1f

Please sign in to comment.