-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add: routes/favoriteRoutes.ts, Add: services/favoriteRoutes.ts, Add: …
…favoriteRoutesSchema.ts, Refactor: index.ts, Refactor: mongo.ts, Refactor: routes/index.ts, Refactor: mongo.d.ts
- Loading branch information
Showing
7 changed files
with
196 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { z } from "zod"; | ||
import patterns from "../../../modules/patterns"; | ||
|
||
const objectId = patterns.objectId; | ||
|
||
export const favoriteRoutesZod = { | ||
createHandler: z.object({ | ||
from: z.string().regex(objectId, "Invalid from location ID"), | ||
to: z.string().regex(objectId, "Invalid to location ID"), | ||
}), | ||
}; | ||
|
||
export type FavoriteRoutesSchema = typeof favoriteRoutesZod; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import express from "express"; | ||
import { validateBody, validateParams, validateQuery } from "@/middlewares/zod"; // Zod 검증 미들웨어 | ||
import { favoriteRoutesZod } from "./docs/schemas/favoriteRoutesSchema"; // Zod 스키마 | ||
import { | ||
createHandler, | ||
getHandler, | ||
deleteHandler, | ||
} from "@/services/favoriteRoutes"; | ||
import authMiddleware from "@/middlewares/auth"; // 인증 미들웨어 | ||
|
||
const router = express.Router(); | ||
|
||
// 라우터 접근 시 로그인 필요 | ||
router.use(authMiddleware); | ||
|
||
// 즐겨찾기 생성 | ||
router.post( | ||
"/create", | ||
validateBody(favoriteRoutesZod.createHandler), | ||
createHandler | ||
); | ||
|
||
// 즐겨찾기 조회 | ||
router.get("/", getHandler); | ||
|
||
// 즐겨찾기 삭제 | ||
router.delete("/:id", deleteHandler); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { Request, Response } from "express"; | ||
import { favoriteRouteModel } from "@/modules/stores/mongo"; | ||
import { Types } from "mongoose"; | ||
import { getLoginInfo } from "@/modules/auths/login"; // 로그인 정보 가져오기 | ||
|
||
// ✅ 즐겨찾기 생성 (POST) | ||
export const createHandler = async ( | ||
req: Request, | ||
res: Response | ||
): Promise<void> => { | ||
try { | ||
const user = getLoginInfo(req); // 로그인된 사용자 정보 가져오기 | ||
if (!user.oid) { | ||
res.status(401).json({ error: "Unauthorized: No valid session found" }); | ||
return; | ||
} | ||
|
||
const userId = user.oid; | ||
const { from, to } = req.body; | ||
|
||
const existingRoute = await favoriteRouteModel.findOne({ | ||
user: userId, | ||
from, | ||
to, | ||
}); | ||
if (existingRoute) { | ||
res | ||
.status(400) | ||
.json({ error: "FavoriteRoutes/create: route already exists" }); | ||
return; | ||
} | ||
|
||
const newRoute = new favoriteRouteModel({ user: userId, from, to }); | ||
await newRoute.save(); | ||
|
||
res.status(201).json(newRoute); | ||
} catch (error) { | ||
res | ||
.status(500) | ||
.json({ error: "FavoriteRoutes/create: internal server error" }); | ||
} | ||
}; | ||
|
||
// ✅ 즐겨찾기 조회 (GET) | ||
export const getHandler = async ( | ||
req: Request, | ||
res: Response | ||
): Promise<void> => { | ||
try { | ||
const user = getLoginInfo(req); // 로그인된 사용자 정보 가져오기 | ||
if (!user.oid) { | ||
res.status(401).json({ error: "Unauthorized: No valid session found" }); | ||
return; | ||
} | ||
|
||
const userId = user.oid; | ||
const routes = await favoriteRouteModel | ||
.find({ user: userId }) | ||
.populate("from to"); | ||
|
||
res.status(200).json(routes); | ||
} catch (error) { | ||
res | ||
.status(500) | ||
.json({ error: "FavoriteRoutes/get: internal server error" }); | ||
} | ||
}; | ||
|
||
// ✅ 즐겨찾기 삭제 (DELETE) | ||
export const deleteHandler = async ( | ||
req: Request<{ id: string }>, | ||
res: Response | ||
): Promise<void> => { | ||
try { | ||
const user = getLoginInfo(req); // 로그인된 사용자 정보 가져오기 | ||
|
||
if (!user.oid) { | ||
res.status(401).json({ error: "Unauthorized: No valid session found" }); | ||
return; | ||
} | ||
|
||
const userId = user.oid; | ||
const { id } = req.params; | ||
|
||
if (!id || !Types.ObjectId.isValid(id)) { | ||
res | ||
.status(400) | ||
.json({ error: "Invalid request: Missing or invalid route ID" }); | ||
return; | ||
} | ||
|
||
const route = await favoriteRouteModel.findOneAndDelete({ | ||
_id: id, | ||
user: userId, | ||
}); | ||
|
||
if (!route) { | ||
res | ||
.status(400) | ||
.json({ error: "FavoriteRoutes/delete: no corresponding route" }); | ||
return; | ||
} | ||
|
||
res.status(200).json(route); | ||
} catch (error) { | ||
res | ||
.status(500) | ||
.json({ error: "FavoriteRoutes/delete: internal server error" }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters