[TIL] HW 5 - Refactoring
04/25/25
![[TIL] HW 5 - Refactoring](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1682591207423%2Ffd61fa59-afa9-4196-99e6-2c3e91fa2207.png&w=3840&q=75)
Issues encountered
Utilizing foreign key in Liked Posts GET API by using
includeUsing transactions in Likes POST API
What I tried
- Initially, we used a double map to retrieve the postId that the user liked from the Like model and then used that postId to retrieve the desired attributes from the Post model. However, we later discovered that this could be done in one go using include, and implemented it as follows:
findLikedPosts = async (userId) => {
const likedPosts = await Likes.findAll({
include: [
{
model: Posts,
attributes: [
"postId",
"UserId",
"nickname",
"title",
"createdAt",
"updatedAt",
"like",
],
},
],
where: { UserId: userId },
attributes: [],
order: [[Posts, "like", "DESC"]],
});
- The like POST API required a rollback in case of errors as it required accessing the database twice. In this API, when a user likes a post, we added their userId and postId to the Like table and incremented the like attribute value in the Posts table. We implemented this in such a way that when the user unlikes the post, the opposite happens. To handle the scenario where only the first operation was successful, and an error occurred, we used transactions in the controller.
const t = await sequelize.transaction({
isolationLevel: Transaction.ISOLATION_LEVELS.READ_COMMITTED,
});
try {
const existingLike = await this.postService.findLike(
_postId,
userId
);
if (!existingLike) {
await this.postService.createLike(userId, _postId, {
transaction: t,
});
await this.postService.incrementLike(_postId, {
transaction: t,
});
await t.commit();
res.status(200).json({
message: "You clicked Like button.",
});
} else {
await this.postService.deleteLike(userId, _postId, {
transaction: t,
});
await this.postService.decrementLike(_postId, {
transaction: t,
});
await t.commit();
res.status(200).json({
message: "You unclicked Like button.",
});
}
} catch (error) {
await t.rollback();
throw new Error(
error.message || "400/Fail to click Like button."
);
}
What I newly learned
Refactoring HW4 by adapting 3-layered architecture
Redis
What to learn next
Store refresh token in Redis
Resolve refresh token expiration error
![[코테] 그리디 문제 - 무지의 먹방 라이브](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1712215455263%2F1ac1f35a-8862-4e42-8d0c-e2bea01e04c0.png&w=3840&q=75)
![[코테] Bfs 토마토](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1709032619170%2F70056896-c857-444b-9c99-45bfcb466806.png&w=3840&q=75)
![[코테] Dfs 문제 유형 - 그래프 내에서 구분하여 카운트 하기](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1709019361383%2Fb0585d72-c808-4169-83a9-2724f312e927.png&w=3840&q=75)
![[코테] DFS vs BFS](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1708971211123%2F71f9386c-6a62-43b2-a602-4d084c24d6cf.png&w=3840&q=75)
![[코테] 여행경로](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1708971251412%2F27ce72ed-8ee7-4d13-a02f-ff4bbe50c4be.png&w=3840&q=75)