Skip to main content

Command Palette

Search for a command to run...

[TIL] HW 5 - Refactoring

04/25/25

Published
[TIL] HW 5 - Refactoring

Issues encountered

  1. Utilizing foreign key in Liked Posts GET API by using include

  2. Using transactions in Likes POST API

What I tried

  1. 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"]],
        });
  1. 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