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

[refactor] change database to MongoDB #118

Merged
merged 7 commits into from
Feb 24, 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
16 changes: 8 additions & 8 deletions src/controller/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const getEmailController = async (req: Request, res: Response) => {
{ isValid: true }
);
} catch (err) {
slack.slackWebhook(req, err.message);
// slack.slackWebhook(req, err.message);
console.error(err.message);
return response.basicResponse(
res,
Expand Down Expand Up @@ -308,18 +308,18 @@ const postSignupController = async (req: Request, res: Response) => {
* @๋กœ๊ทธ์ธ_์—ฌ๋ถ€_๊ฒ€์‚ฌ
* @route GET /auth/check
* @access public
* @err
* @err
*/
const getLoginFlagController = async(req: Request, res: Response) => {
const getLoginFlagController = async (req: Request, res: Response) => {
try {
const resData = await authService.getLoginFlagService(
req.user ? true : false
)
);
return response.dataResponse(
res,
res,
returnCode.OK,
"๋กœ๊ทธ์ธ ์—ฌ๋ถ€ ํ™•์ธ ์„ฑ๊ณต.",
true,
"๋กœ๊ทธ์ธ ์—ฌ๋ถ€ ํ™•์ธ ์„ฑ๊ณต.",
true,
resData
);
} catch (err) {
Expand All @@ -332,7 +332,7 @@ const getLoginFlagController = async(req: Request, res: Response) => {
"์„œ๋ฒ„ ์˜ค๋ฅ˜"
);
}
}
};

const authController = {
getEmailController,
Expand Down
4 changes: 2 additions & 2 deletions src/controller/book.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const postBookController = async (req: Request, res: Response) => {
try {
const resData = await bookService.postBookService(
req.user ? true : false,
req.user ? Number(req.user.id) : constant.ANONYMOUS_USER,
req.user ? req.user.id : constant.ANONYMOUS_USER,
req.body.isbn,
req.body.thumbnail,
req.body.title,
Expand Down Expand Up @@ -70,7 +70,7 @@ const postBookController = async (req: Request, res: Response) => {
resData
);
} catch (err) {
slack.slackWebhook(req, err.message);
// slack.slackWebhook(req, err.message);
console.error(err.message);
return response.basicResponse(
res,
Expand Down
51 changes: 51 additions & 0 deletions src/library/convertSnakeToCamel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import _ from "lodash";

export const toCamel = (s) => {
return s.replace(/([-_][a-z])/gi, ($1) => {
return $1.toUpperCase().replace("-", "").replace("_", "");
});
};

export const isArray = (a) => {
return Array.isArray(a);
};

export const isObject = (o) => {
return o === Object(o) && !isArray(o) && typeof o !== "function";
};

export const keysToCamel = (o) => {
if (isObject(o)) {
const n = {};

Object.keys(o).forEach((k) => {
n[toCamel(k)] = o[k];
});

return n;
} else if (isArray(o)) {
return o.map((i) => {
return keysToCamel(i);
});
}

return o;
};

export const keysToSnake = (o) => {
if (isObject(o)) {
const n = {};

Object.keys(o).forEach((k) => {
n[_.snakeCase(k)] = o[k];
});

return n;
} else if (isArray(o)) {
return o.map((i) => {
return _.snakeCase(i);
});
}

return o;
};
6 changes: 3 additions & 3 deletions src/router/book.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const router = express.Router();

router.post("/", isLogin, bookController.postBookController);
router.get("/", auth, bookController.getBookController);
router.get("/pre", auth, bookController.getBookPreController);
router.get("/peri", auth, bookController.getBookPeriController);
router.get("/post", auth, bookController.getBookPostController);
// router.get("/pre", auth, bookController.getBookPreController);
// router.get("/peri", auth, bookController.getBookPeriController);
// router.get("/post", auth, bookController.getBookPostController);

module.exports = router;
4 changes: 2 additions & 2 deletions src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ router.get("", async (req: Request, res: Response) => {
}
});

// router.use("/auth", require("./auth"));
router.use("/auth", require("./auth"));
router.use("/book", require("./book"));
// router.use("/user", require("./user"));
router.use("/user", require("./user"));
router.use("/review", require("./review"));

export default router;
211 changes: 100 additions & 111 deletions src/service/auth.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import index from "../config";
import mongoose from "mongoose"
import mongoose from "mongoose";

// library
import jwt from "jsonwebtoken";
Expand All @@ -10,6 +10,7 @@ import {
checkNicknameValid,
checkPasswordValid,
} from "../library/checkValidation";
import { keysToSnake } from "../library/convertSnakeToCamel";

// model
import User from "../models/User";
Expand All @@ -20,35 +21,33 @@ import User from "../models/User";
* @access public
* @err 1. ํ•„์š”ํ•œ ๊ฐ’์ด ์—†์„ ๋•Œ
*/
// const getEmailService = async (email?: string) => {
// // ์ž˜๋ชป๋œ ์š”์ฒญ๊ฐ’์ด ๋“ค์–ด์™”์„ ๋•Œ (query !== email)
// if (email === undefined) {
// return constant.WRONG_REQUEST_VALUE;
// }

// // ํ•„์š”ํ•œ ๊ฐ’์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ
// if (!email) {
// return constant.NULL_VALUE;
// }

// // email ํ˜•์‹์ด ์ž˜๋ชป๋˜์—ˆ์„ ๋•Œ
// if (!isEmail(email)) {
// return constant.WRONG_EMAIL_CONVENTION;
// }

// // email์ด ์ด๋ฏธ ์กด์žฌํ•  ๋•Œ
// const emailExist = await User.findAll({
// where: {
// email,
// isDeleted: false,
// },
// });
// if (emailExist.length > 0) {
// return constant.EMAIL_ALREADY_EXIST;
// }

// return constant.SUCCESS;
// };
const getEmailService = async (email?: string) => {
// ์ž˜๋ชป๋œ ์š”์ฒญ๊ฐ’์ด ๋“ค์–ด์™”์„ ๋•Œ (query !== email)
if (email === undefined) {
return constant.WRONG_REQUEST_VALUE;
}

// ํ•„์š”ํ•œ ๊ฐ’์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ
if (!email) {
return constant.NULL_VALUE;
}

// email ํ˜•์‹์ด ์ž˜๋ชป๋˜์—ˆ์„ ๋•Œ
if (!isEmail(email)) {
return constant.WRONG_EMAIL_CONVENTION;
}

// email์ด ์ด๋ฏธ ์กด์žฌํ•  ๋•Œ
// TODO: DB์—์„œ isDeleted string์œผ๋กœ ๋˜์žˆ๋Š” ๊ฒƒ ๋ณ€๊ฒฝ
const emailExist = await User.exists(
keysToSnake({ email, isDeleted: false })
);
if (emailExist) {
return constant.EMAIL_ALREADY_EXIST;
}

return constant.SUCCESS;
};

/**
* @๋‹‰๋„ค์ž„_์œ ํšจ์„ฑ_๊ฒ€์‚ฌ
Expand All @@ -73,12 +72,10 @@ const getNicknameService = async (nickname?: string) => {
}

// nickname์ด ์ด๋ฏธ ์กด์žฌํ•  ๋•Œ
const nicknameExist = await User.find(
{
nickname,
isDeleted: false
},
)
const nicknameExist = await User.find({
nickname,
isDeleted: false,
});

if (nicknameExist.length > 0) {
return constant.NICKNAME_ALREADY_EXIST;
Expand All @@ -102,12 +99,10 @@ const postLoginService = async (email: string, password: string) => {
}

// ์กด์žฌํ•˜์ง€ ์•Š๋Š” ์ด๋ฉ”์ผ
const user = await User.findOne(
{
email,
isDeleted: false
}
);
const user = await User.findOne({
email,
isDeleted: false,
});
if (!user) {
return constant.EMAIL_NOT_FOUND;
}
Expand Down Expand Up @@ -141,73 +136,67 @@ const postLoginService = async (email: string, password: string) => {
* 5. ์ด๋ฉ”์ผ์ด ์ด๋ฏธ ์กด์žฌํ•  ๋•Œ
* 6. ๋‹‰๋„ค์ž„์ด ์ด๋ฏธ ์กด์žฌํ•  ๋•Œ
*/
// const postSignupService = async (
// email: string,
// nickname: string,
// password: string
// ) => {
// // ํ•„์š”ํ•œ ๊ฐ’์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ
// if (!email || !nickname || !password) {
// return constant.NULL_VALUE;
// }

// // email ํ˜•์‹์ด ์ž˜๋ชป๋˜์—ˆ์„ ๋•Œ
// if (!isEmail(email)) {
// return constant.WRONG_EMAIL_CONVENTION;
// }

// // nickname ํ˜•์‹์ด ์ž˜๋ชป๋˜์—ˆ์„ ๋•Œ
// if (!checkNicknameValid(nickname)) {
// return constant.WRONG_NICKNAME_CONVENTION;
// }

// // password ํ˜•์‹์ด ์ž˜๋ชป๋˜์—ˆ์„ ๋•Œ
// if (!checkPasswordValid(password)) {
// return constant.WRONG_PASSWORD_CONVENTION;
// }

// // email์ด ์ด๋ฏธ ์กด์žฌํ•  ๋•Œ
// const emailExist = await User.findAll({
// where: {
// email,
// isDeleted: false,
// },
// });
// if (emailExist.length > 0) {
// return constant.EMAIL_ALREADY_EXIST;
// }

// // nickname์ด ์ด๋ฏธ ์กด์žฌํ•  ๋•Œ
// const nicknameExist = await User.findAll({
// where: {
// nickname,
// isDeleted: false,
// },
// });
// if (nicknameExist.length > 0) {
// return constant.NICKNAME_ALREADY_EXIST;
// }

// // ์ƒˆ๋กœ์šด ์œ ์ € ์ƒ์„ฑ & ํ† ํฐ ๋ฐœ๊ธ‰
// const salt = await bcrypt.genSalt(10);
// const hashedPassword = await bcrypt.hash(password, salt);
// const user = await User.create({
// email,
// password: hashedPassword,
// nickname,
// });

// const payload = {
// user: {
// id: user.id,
// },
// };

// const token = jwt.sign(payload, index.jwtSecret, {
// expiresIn: "14d",
// });
// return token;
// };
const postSignupService = async (
email: string,
nickname: string,
password: string
) => {
// ํ•„์š”ํ•œ ๊ฐ’์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ
if (!email || !nickname || !password) {
return constant.NULL_VALUE;
}

// email ํ˜•์‹์ด ์ž˜๋ชป๋˜์—ˆ์„ ๋•Œ
if (!isEmail(email)) {
return constant.WRONG_EMAIL_CONVENTION;
}

// nickname ํ˜•์‹์ด ์ž˜๋ชป๋˜์—ˆ์„ ๋•Œ
if (!checkNicknameValid(nickname)) {
return constant.WRONG_NICKNAME_CONVENTION;
}

// password ํ˜•์‹์ด ์ž˜๋ชป๋˜์—ˆ์„ ๋•Œ
if (!checkPasswordValid(password)) {
return constant.WRONG_PASSWORD_CONVENTION;
}

// email์ด ์ด๋ฏธ ์กด์žฌํ•  ๋•Œ
const emailExist = await User.exists(
keysToSnake({ email, isDeleted: false })
);
if (emailExist) {
return constant.EMAIL_ALREADY_EXIST;
}

// nickname์ด ์ด๋ฏธ ์กด์žฌํ•  ๋•Œ
const nicknameExist = await User.exists(
keysToSnake({ nickname, isDeleted: false })
);
if (nicknameExist) {
return constant.NICKNAME_ALREADY_EXIST;
}

// ์ƒˆ๋กœ์šด ์œ ์ € ์ƒ์„ฑ & ํ† ํฐ ๋ฐœ๊ธ‰
const salt = await bcrypt.genSalt(10);
const hashedPassword = await bcrypt.hash(password, salt);
const user = await User.create({
email,
password: hashedPassword,
nickname,
});

const payload = {
user: {
id: user.id,
},
};

const token = jwt.sign(payload, index.jwtSecret, {
expiresIn: "14d",
});
return token;
};

/**
* @๋กœ๊ทธ์ธ_์—ฌ๋ถ€_๊ฒ€์‚ฌ
Expand All @@ -220,10 +209,10 @@ const getLoginFlagService = async (isLogin: Boolean) => {
};

const authService = {
// getEmailService,
getEmailService,
getNicknameService,
postLoginService,
// postSignupService,
postSignupService,
getLoginFlagService,
};

Expand Down
Loading