Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] Add mongo id form valid checker #124

Merged
merged 1 commit into from
Mar 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions src/controller/review.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ const patchReviewPreController = async (req: Request, res: Response) => {
);
}

if (resData === constant.WRONG_REQUEST_VALUE) {
return response.basicResponse(
res,
returnCode.BAD_REQUEST,
false,
"ID ํ˜•์‹์ด ์ž˜๋ชป๋˜์—ˆ์Šต๋‹ˆ๋‹ค."
);
}

if (resData === constant.DB_NOT_FOUND) {
return response.basicResponse(
res,
Expand Down Expand Up @@ -91,6 +100,15 @@ const getQuestionController = async (req: Request, res: Response) => {
);
}

if (resData === constant.WRONG_REQUEST_VALUE) {
return response.basicResponse(
res,
returnCode.BAD_REQUEST,
false,
"ID ํ˜•์‹์ด ์ž˜๋ชป๋˜์—ˆ์Šต๋‹ˆ๋‹ค."
);
}

if (resData === constant.DB_NOT_FOUND) {
return response.basicResponse(
res,
Expand Down Expand Up @@ -146,6 +164,15 @@ const patchReviewPeriController = async (req: Request, res: Response) => {
}

if (resData === constant.WRONG_REQUEST_VALUE) {
return response.basicResponse(
res,
returnCode.BAD_REQUEST,
false,
"ID ํ˜•์‹์ด ์ž˜๋ชป๋˜์—ˆ์Šต๋‹ˆ๋‹ค."
);
}

if (resData === constant.DB_NOT_FOUND) {
return response.basicResponse(
res,
returnCode.BAD_REQUEST,
Expand Down Expand Up @@ -198,6 +225,15 @@ const getReviewController = async (req: Request, res: Response) => {
}

if (resData === constant.WRONG_REQUEST_VALUE) {
return response.basicResponse(
res,
returnCode.BAD_REQUEST,
false,
"ID ํ˜•์‹์ด ์ž˜๋ชป๋˜์—ˆ์Šต๋‹ˆ๋‹ค."
);
}

if (resData === constant.DB_NOT_FOUND) {
return response.basicResponse(
res,
returnCode.BAD_REQUEST,
Expand Down Expand Up @@ -250,6 +286,15 @@ const getReviewPreController = async (req: Request, res: Response) => {
}

if (resData === constant.WRONG_REQUEST_VALUE) {
return response.basicResponse(
res,
returnCode.BAD_REQUEST,
false,
"ID ํ˜•์‹์ด ์ž˜๋ชป๋˜์—ˆ์Šต๋‹ˆ๋‹ค."
);
}

if (resData === constant.DB_NOT_FOUND) {
return response.basicResponse(
res,
returnCode.BAD_REQUEST,
Expand Down Expand Up @@ -302,6 +347,15 @@ const getReviewPeriController = async (req: Request, res: Response) => {
}

if (resData === constant.WRONG_REQUEST_VALUE) {
return response.basicResponse(
res,
returnCode.BAD_REQUEST,
false,
"ID ํ˜•์‹์ด ์ž˜๋ชป๋˜์—ˆ์Šต๋‹ˆ๋‹ค."
);
}

if (resData === constant.DB_NOT_FOUND) {
return response.basicResponse(
res,
returnCode.BAD_REQUEST,
Expand Down Expand Up @@ -355,6 +409,15 @@ const patchReviewController = async (req: Request, res: Response) => {
);
}

if (resData === constant.WRONG_REQUEST_VALUE) {
return response.basicResponse(
res,
returnCode.BAD_REQUEST,
false,
"ID ํ˜•์‹์ด ์ž˜๋ชป๋˜์—ˆ์Šต๋‹ˆ๋‹ค."
);
}

if (resData === constant.DB_NOT_FOUND) {
return response.basicResponse(
res,
Expand Down Expand Up @@ -408,6 +471,15 @@ const deleteReviewController = async (req: Request, res: Response) => {
}

if (resData === constant.WRONG_REQUEST_VALUE) {
return response.basicResponse(
res,
returnCode.BAD_REQUEST,
false,
"ID ํ˜•์‹์ด ์ž˜๋ชป๋˜์—ˆ์Šต๋‹ˆ๋‹ค."
);
}

if (resData === constant.DB_NOT_FOUND) {
return response.basicResponse(
res,
returnCode.BAD_REQUEST,
Expand Down
50 changes: 46 additions & 4 deletions src/service/review.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import mongoose from "mongoose";
// library
import constant from "../library/constant";
import { keysToSnake, keysToCamel } from "../library/convertSnakeToCamel";
import { isValidObjectId } from "mongoose";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good


// model
import User from "../models/User";
Expand Down Expand Up @@ -40,6 +41,11 @@ const patchReviewPreService = async (
return constant.NULL_VALUE;
}

// MongoDB id ํ˜•์‹์ด ์•„๋‹ ๋•Œ
if (!isValidObjectId(reviewId) || !isValidObjectId(userId)) {
return constant.WRONG_REQUEST_VALUE;
}

// review ์ฒดํฌ
const review = await Review.findById(
new mongoose.Types.ObjectId(reviewId)
Expand Down Expand Up @@ -77,6 +83,11 @@ const getQuestionService = async (userId: string, reviewId: string) => {
return constant.NULL_VALUE;
}

// MongoDB id ํ˜•์‹์ด ์•„๋‹ ๋•Œ
if (!isValidObjectId(reviewId) || !isValidObjectId(userId)) {
return constant.WRONG_REQUEST_VALUE;
}

// review ์กฐํšŒ
const review = await Review.findById(
new mongoose.Types.ObjectId(reviewId)
Expand Down Expand Up @@ -121,14 +132,19 @@ const patchReviewPeriService = async (
return constant.NULL_VALUE;
}

// MongoDB id ํ˜•์‹์ด ์•„๋‹ ๋•Œ
if (!isValidObjectId(reviewId) || !isValidObjectId(userId)) {
return constant.WRONG_REQUEST_VALUE;
}

// ํ•ด๋‹น review ์กฐํšŒ
const review = await Review.findById(
new mongoose.Types.ObjectId(reviewId)
).where(keysToSnake({ userId, isDeleted: false }));

// 2. ์กด์žฌํ•˜์ง€ ์•Š๋Š” review
if (!review) {
return constant.WRONG_REQUEST_VALUE;
return constant.DB_NOT_FOUND;
}

let finishSt = Number(reviewSt) === 4 ? true : false;
Expand Down Expand Up @@ -186,14 +202,19 @@ const getReviewService = async (userId: string, reviewId: string) => {
return constant.NULL_VALUE;
}

// MongoDB id ํ˜•์‹์ด ์•„๋‹ ๋•Œ
if (!isValidObjectId(reviewId) || !isValidObjectId(userId)) {
return constant.WRONG_REQUEST_VALUE;
}

// review ์กฐํšŒ
const reviewToShow = await Review.findById(
new mongoose.Types.ObjectId(reviewId)
).where(keysToSnake({ userId, isDeleted: false }));

// ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๋ฆฌ๋ทฐ์ผ ๋•Œ
if (!reviewToShow) {
return constant.WRONG_REQUEST_VALUE;
return constant.DB_NOT_FOUND;
}
// snake to camel
const originReview = keysToCamel(reviewToShow);
Expand Down Expand Up @@ -226,13 +247,19 @@ const getReviewPreService = async (userId: string, reviewId: string) => {
if (!userId || !reviewId) {
return constant.NULL_VALUE;
}

// MongoDB id ํ˜•์‹์ด ์•„๋‹ ๋•Œ
if (!isValidObjectId(reviewId) || !isValidObjectId(userId)) {
return constant.WRONG_REQUEST_VALUE;
}

const reviewToShow = await Review.findById(
new mongoose.Types.ObjectId(reviewId)
).where(keysToSnake({ userId, isDeleted: false }));

// ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๋ฆฌ๋ทฐ์ผ ๋•Œ
if (!reviewToShow) {
return constant.WRONG_REQUEST_VALUE;
return constant.DB_NOT_FOUND;
}

// snake to camel
Expand Down Expand Up @@ -262,6 +289,11 @@ const getReviewPeriService = async (userId: string, reviewId: string) => {
return constant.NULL_VALUE;
}

// MongoDB id ํ˜•์‹์ด ์•„๋‹ ๋•Œ
if (!isValidObjectId(reviewId) || !isValidObjectId(userId)) {
return constant.WRONG_REQUEST_VALUE;
}

const reviewToShow = await Review.findById(
new mongoose.Types.ObjectId(reviewId)
).where(keysToSnake({ userId, isDeleted: false }));
Expand Down Expand Up @@ -307,6 +339,11 @@ const patchReviewService = async (
return constant.NULL_VALUE;
}

// MongoDB id ํ˜•์‹์ด ์•„๋‹ ๋•Œ
if (!isValidObjectId(reviewId)) {
return constant.WRONG_REQUEST_VALUE;
}

// find review
const reviewToChange = await Review.findById(
new mongoose.Types.ObjectId(reviewId)
Expand Down Expand Up @@ -344,14 +381,19 @@ const deleteReviewService = async (userId: string, reviewId: string) => {
return constant.NULL_VALUE;
}

// MongoDB id ํ˜•์‹์ด ์•„๋‹ ๋•Œ
if (!isValidObjectId(reviewId) || !isValidObjectId(userId)) {
return constant.WRONG_REQUEST_VALUE;
}

// ํ•ด๋‹น review ์กฐํšŒ
const review = await Review.findById(
new mongoose.Types.ObjectId(reviewId)
).where(keysToSnake({ userId, isDeleted: false }));

// 2. ์กด์žฌํ•˜์ง€ ์•Š๊ฑฐ๋‚˜ ์‚ญ์ œ๋œ review
if (!review) {
return constant.WRONG_REQUEST_VALUE;
return constant.DB_NOT_FOUND;
}

// ๋…ํ›„๊ฐ ์‚ญ์ œ
Expand Down