Skip to content

Commit

Permalink
Merge pull request #13 from netCard-TUK/hm
Browse files Browse the repository at this point in the history
req인자 예외 처리 추가 / delete 삭제 / 특정 명함 정보 검색 쿼리 수정 / 내 명함 정보 로직 수정(photo)
  • Loading branch information
ohamin26 authored Jan 22, 2024
2 parents 958a914 + 330ac9a commit 7b482b0
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 120 deletions.
189 changes: 98 additions & 91 deletions src/api/cards/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@ const jwt = require("jsonwebtoken");
//내 명함 정보 등록
exports.register = async (req, res) => {
let { userId, position, organization, address, tell, email } = req.body;
// 숫자 최소 1자리
const numRegex = /^[0-9]+$/;
if (!(numRegex.test(userId) && numRegex.test(tell))) {
return res.send({
isSuccess: false,
message: "userId와 tell은 숫자만 입력 가능합니다.",
});
}
userId = Number(userId);
tell = Number(tell);

//파일 예외 처리 파일 정보가 있을 경우만 인자로 받음
let photo = req.files && Object.keys(req.files).length > 0 ? req.files : null;
let time = new Date();
photo =
photo != null
? "http://" +
req.get("host") +
"/" +
+time.getTime() +
photo["photo"][0].filename
? "http://" + req.get("host") + "/" + photo["photo"][0].filename
: null;

// req 값이 있는지 검사
Expand All @@ -34,14 +39,6 @@ exports.register = async (req, res) => {
});
}

// userId와 tell 타입(int) 검사
if (typeof userId !== "number" || typeof tell !== "number") {
return res.send({
isSuccess: false,
message: "userId와 tell은 정수(int)여야 합니다.",
});
}

// position, organization, address, email 타입(string) 검사
if (
typeof position !== "string" ||
Expand Down Expand Up @@ -95,21 +92,25 @@ exports.register = async (req, res) => {

//특정 명함 정보 조회
exports.inquiry = async (req, res) => {
const cardId = Number(req.params.cardId);
let cardId = req.params.cardId;

if (cardId == null) {
// 숫자 최소 1자리
const numRegex = /^[0-9]+$/;
if (!numRegex.test(cardId)) {
return res.send({
isSuccess: false,
message: "card_id가 null 값입니다.",
message: "cardId는 숫자만 입력 가능합니다.",
});
}
cardId = Number(cardId);

if (typeof cardId !== "number") {
if (cardId == null) {
return res.send({
isSuccess: false,
message: "card_id는 정수(int)여야 합니다.",
message: "card_id가 null 값입니다.",
});
}

// 명함 정보 가져오기
const item = await repository.show(cardId);
if (item === null) {
Expand All @@ -120,10 +121,10 @@ exports.inquiry = async (req, res) => {
}
//유저 정보 가져오기
const user_info = await userRepository.show_user(item.user_id);
if (user_info == null) {
if (user_info === null) {
return res.send({
isSuccess: false,
message: "조회된 유저 정보가 없습니다.",
isSuccess: true,
result: item,
});
}

Expand All @@ -135,7 +136,7 @@ exports.inquiry = async (req, res) => {
photo: item.photo,
tell: item.tell,
email: item.email,
user_name: user_info.name,
name: user_info.name,
phone: user_info.phone,
};

Expand All @@ -150,16 +151,16 @@ exports.inquiry_list = async (req, res) => {
if (typeof name !== "string") {
return res.send({
isSuccess: false,
message: "userName는 문자열(string)이여야 합니다.",
message: "userName은 문자열(string)이여야 합니다.",
});
}

const item_all = await repository.show_all_as_name(name);

if (Object.keys(item_all).length == 0) {
return res.send({
isSuccess: false,
message: "조회된 명함 정보가 없습니다.",
isSuccess: true,
result: item_all,
});
}

Expand All @@ -173,20 +174,23 @@ exports.inquiry_list = async (req, res) => {
exports.inquiry_all = async (req, res) => {
//user_id 값 가져오기
const { access_token } = req.headers;
let userId = Number(req.params.userId);
let userId = req.params.userId;
const { id } = jwt.verify(access_token, process.env.JWT_KEY);

if (userId !== id) {
// 숫자 최소 1자리
const numRegex = /^[0-9]+$/;
if (!numRegex.test(userId)) {
return res.send({
isSuccess: false,
message: "올바른 토큰 값이 아닙니다.",
message: "userId는 숫자만 입력 가능합니다.",
});
}
userId = Number(userId);

if (typeof userId !== "number") {
if (userId !== id) {
return res.send({
isSuccess: false,
message: "userId는 정수(int)여야 합니다.",
message: "올바른 토큰 값이 아닙니다.",
});
}

Expand Down Expand Up @@ -215,23 +219,22 @@ exports.inquiry_all = async (req, res) => {

//내 명함 정보 업데이트
exports.update = async (req, res) => {
const cardId = req.params.cardId;
let cardId = req.params.cardId;

const { access_token } = req.headers;
const { id } = jwt.verify(access_token, process.env.JWT_KEY);
let { userId, position, organization, address, tell, email } = req.body;

//파일 예외 처리 파일 정보가 있을 경우만 인자로 받음
let photo = req.files && Object.keys(req.files).length > 0 ? req.files : null;
let time = new Date();
photo =
photo != null
? "http://" +
req.get("host") +
"/" +
+time.getTime() +
photo["photo"][0].filename
: null;
// 숫자 최소 1자리
const numRegex = /^[0-9]+$/;
if (!(numRegex.test(userId) && numRegex.test(cardId))) {
return res.send({
isSuccess: false,
message: "userId와 cardId는 숫자만 입력 가능합니다.",
});
}
userId = Number(userId);
cardId = Number(cardId);

if (userId !== id) {
return res.send({
Expand Down Expand Up @@ -272,18 +275,9 @@ exports.update = async (req, res) => {
? (organization = organization)
: (organization = item.organization);
address ? (address = address) : (address = item.address);
photo ? (photo = photo) : (photo = item.photo);
tell ? (tell = tell) : (tell = item.tell);
email ? (email = email) : (email = item.email);

// userId와 tell 타입(int) 검사
if (typeof userId !== "number" || typeof tell !== "number") {
return res.send({
isSuccess: false,
message: "userId와 tell은 정수(int)여야 합니다.",
});
}

// position, organization, address, email 타입(string) 검사
if (
typeof position !== "string" ||
Expand All @@ -298,12 +292,22 @@ exports.update = async (req, res) => {
});
}

// 숫자 최소 1자리
if (!numRegex.test(tell)) {
console.log(userId, tell);
console.log(typeof userId, typeof tell);
return res.send({
isSuccess: false,
message: "tell은 숫자만 입력 가능합니다.",
});
}
tell = Number(tell);

const { affectedRows } = await repository.update(
cardId,
position,
organization,
address,
photo,
tell,
email
);
Expand All @@ -314,41 +318,44 @@ exports.update = async (req, res) => {
return res.send({ isSuccess: false, message: "저장 실패" });
};

//내 명함 목록 삭제
exports.delete = async (req, res) => {
const cardId = Number(req.params.cardId);
const { access_token } = req.headers;
const { id } = jwt.verify(access_token, process.env.JWT_KEY);
let { userId } = req.body;
userId = Number(userId);

if (userId !== id) {
return res.send({
isSuccess: false,
message: "올바른 토큰 값이 아닙니다.",
});
}

if (typeof userId !== "number" || typeof cardId !== "number") {
return res.send({
isSuccess: false,
message: "userId와 cardId는 정수(int)여야 합니다.",
});
}

const user_info = await userRepository.show_user(userId);
if (user_info == null) {
return res.send({
isSuccess: false,
message: "조회된 유저 정보가 없습니다.",
});
}

const { affectedRows } = await repository.delete(cardId);

if (affectedRows > 0) {
res.send({ isSuccess: true });
} else {
res.send({ isSuccess: false, message: "삭제 실패" });
}
};
/**
* 내 명함 목록 삭제
* 미사용으로 주석 처리
*/
// exports.delete = async (req, res) => {
// const cardId = Number(req.params.cardId);
// const { access_token } = req.headers;
// const { id } = jwt.verify(access_token, process.env.JWT_KEY);
// let { userId } = req.body;
// userId = Number(userId);

// if (userId !== id) {
// return res.send({
// isSuccess: false,
// message: "올바른 토큰 값이 아닙니다.",
// });
// }

// if (typeof userId !== "number" || typeof cardId !== "number") {
// return res.send({
// isSuccess: false,
// message: "userId와 cardId는 정수(int)여야 합니다.",
// });
// }

// const user_info = await userRepository.show_user(userId);
// if (user_info == null) {
// return res.send({
// isSuccess: false,
// message: "조회된 유저 정보가 없습니다.",
// });
// }

// const { affectedRows } = await repository.delete(cardId);

// if (affectedRows > 0) {
// res.send({ isSuccess: true });
// } else {
// res.send({ isSuccess: false, message: "삭제 실패" });
// }
// };
29 changes: 6 additions & 23 deletions src/api/cards/repogitory.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ exports.create = async (

//내 명함 조회 쿼리
exports.show = async (card_id) => {
console.log(card_id);
const query = `SELECT * FROM cards WHERE card_id=?`;
const query = `SELECT * FROM cards JOIN user ON cards.user_id = user.id WHERE card_id=?`;
let result = await pool(query, card_id);

return result.length < 0 ? null : result[0];
return result.length == 0 ? null : result[0];
};

//내 명함 전체 조회
Expand All @@ -42,7 +41,7 @@ exports.show_all = async (id) => {
//특정 명함 전체 조회(이름 일치)
exports.show_all_as_name = async (name) => {
const query = `
SELECT cards.*, user.phone, user.name FROM cards JOIN user ON cards.user_id = user.id WHERE name=?`;
SELECT cards.*, user.phone, user.name FROM cards JOIN user ON cards.user_id = user.id WHERE user.name LIKE '%${name}%'`;
const result = await pool(query, [name]);
return result.length < 0 ? null : result;
};
Expand All @@ -55,30 +54,14 @@ exports.show_all_as_name = async (name) => {
// };

//내 명함 업데이트 쿼리
exports.update = async (
id,
position,
organization,
address,
photo,
tell,
email
) => {
exports.update = async (id, position, organization, address, tell, email) => {
const query = `
UPDATE cards
SET position=?, organization=?, address=?, photo=?, tell=?, email=?
SET position=?, organization=?, address=?, tell=?, email=?
WHERE card_id = ?;
`;

return await pool(query, [
position,
organization,
address,
photo,
tell,
email,
id,
]);
return await pool(query, [position, organization, address, tell, email, id]);
};

//내 명함 삭제 쿼리
Expand Down
8 changes: 2 additions & 6 deletions src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,8 @@ router.post(
router.get("/api/cards/search/:cardId", cardsController.inquiry);
router.get("/api/cards/search/list/:name", cardsController.inquiry_list);
router.get("/api/cards/search/all/:userId", cardsController.inquiry_all);
router.post(
"/api/cards/update/:cardId",
upload.fields([{ name: "photo", maxCount: 1 }]),
cardsController.update
);
router.post("/api/cards/delete/:cardId", cardsController.delete);
router.post("/api/cards/update/:cardId", cardsController.update);
// router.post("/api/cards/delete/:cardId", cardsController.delete);
module.exports = router;

// 명함 지갑 관련 API
Expand Down

0 comments on commit 7b482b0

Please sign in to comment.