Skip to content

Commit

Permalink
fix: caching bookmark controller, route url
Browse files Browse the repository at this point in the history
  • Loading branch information
ptyoiy committed May 4, 2024
1 parent e22d24c commit 2fde152
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 21 deletions.
19 changes: 2 additions & 17 deletions controllers/event/event_bookmark.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as express from "express";
import { Bookmark, BookmarkAsset, sequelize } from "../../models/index.js";
import { redisClient } from "../../redis/connect.js";
import { findObjectByPk, getEventBookmarkInfo, validateRequestBody } from "../common_method/index.js";
import { findObjectByPk, validateRequestBody } from "../common_method/index.js";
import { IEventUserRequest } from "./request/request.js";

const bodyList = [
Expand Down Expand Up @@ -75,7 +75,6 @@ export const deleteBookmark = async (
if (errorMessage) {
return res.status(400).json({ error: errorMessage });
}

// 북마크에 이미 추가되어 있는지 확인
const bookmark = await Bookmark.findOne({ where: { fk_user_id: user_id } });

Expand All @@ -88,26 +87,12 @@ export const deleteBookmark = async (
},
transaction: transaction
});

// Bookmark 테이블 삭제
await Bookmark.destroy({
where: {
id: bookmark.id
},
transaction: transaction
});
}

await transaction.commit();

// Redis에 있는 해당 사용자의 북마크 정보 삭제 또는 업데이트
const updatedBookmarks = await getEventBookmarkInfo(user_id.toString());
if (updatedBookmarks.length > 0) {
await redisClient.set(`user:bookmarks:${user_id}`, JSON.stringify(updatedBookmarks), { EX: EXPIRE });
} else {
await redisClient.del(`user:bookmarks:${user_id}`);
}

await redisClient.sRem(`user:bookmarks:${user_id}`, event_id.toString());
return res.status(200).json({ message: "북마크 삭제 성공했습니다." });
} catch (error) {
await transaction.rollback();
Expand Down
3 changes: 1 addition & 2 deletions controllers/jwt/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ enum TokenType {

export const verifyToken = (req: Express.Request, res, next) => {
const token = req.cookies.accessToken;
const id = req.params.id || req.cookies.id;
const id = req.cookies.id;
if (!token) {
return res.status(401).json({
code: 401,
Expand All @@ -27,7 +27,6 @@ export const verifyToken = (req: Express.Request, res, next) => {

try {
const payload = jwt.verify(token, process.env.SECRET_KEY) as Payload;
console.log({payload, id});
if (payload.id != id) {
throw new Error('토큰과 다른 사용자.')
}
Expand Down
4 changes: 2 additions & 2 deletions routes/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const eventRouter = Router();
eventRouter.put('/posts/events/like', verifyToken, eventController.setLike);

/** 북마크 */
eventRouter.post('/users/bookmark', verifyToken, eventController.setBookmark);
eventRouter.delete('/users/bookmark', verifyToken, eventController.deleteBookmark);
eventRouter.post('/users/bookmark/add', verifyToken, eventController.setBookmark);
eventRouter.post('/users/bookmark/remove', verifyToken, eventController.deleteBookmark);

/** 행사글 요청 */
eventRouter.get('/posts/events/:eventId', eventController.getEvent)
Expand Down

0 comments on commit 2fde152

Please sign in to comment.