Skip to content

Commit

Permalink
Merge pull request #9 from netCard-TUK/hm
Browse files Browse the repository at this point in the history
오류 수정
  • Loading branch information
Leewonchan14 authored Jan 19, 2024
2 parents 42e1e6f + fc87844 commit 511d805
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 59 deletions.
121 changes: 65 additions & 56 deletions src/api/cards/controller.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
const repository = require("./repogitory");
const userRepogitory = require("../user/repogitory");
const userRepository = require("../user/repogitory");
const jwt = require("jsonwebtoken");

//내 명함 정보 등록
exports.register = async (req, res) => {
const { user_id, position, organization, address, tell, email } = req.body;
let { user_id, position, organization, address, tell, email } = req.body;
const file = req.files;

// 확장자까지 넣기
// 사진 경로 받기
if (!user_id || !position || !organization || !address || !tell || !email) {
return res.send({
isSuccess: false,
message: "항목 중 null 값이 존재합니다.",
});
}

let time = new Date();
const photo =
"http://" +
Expand All @@ -21,9 +26,7 @@ exports.register = async (req, res) => {
const { access_token } = req.headers;
//const [tokenType, tokenValue] = authorization.split(" ");
const { id } = jwt.verify(access_token, process.env.JWT_KEY);

checkUserInfo(res, user_id, id);

//db에 저장 정상적으로 저장 시 ok / 실패 시 fail
const { affectedRows, insertId } = await repository.create(
(user_id = id),
Expand All @@ -35,34 +38,42 @@ exports.register = async (req, res) => {
email
);
if (affectedRows > 0) {
return res.send({ isSuccess: "true", id: insertId });
return res.send({ isSuccess: true, id: insertId });
}

return res.send({ isSuccess: "false", message: "등록 실패" });
return res.send({ isSuccess: false, message: "등록 실패" });
};

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

//user_id 가져오기
const { access_token } = req.headers;
//const [tokenType, tokenValue] = authorization.split(" ");
const { id } = jwt.verify(access_token, process.env.JWT_KEY);
const { user_id } = req.body;

let { user_id } = req.body;
checkUserInfo(res, user_id, id);

// 명함 정보 가져오기
const item = await repository.show({ cardId, userId: id });
const item = await repository.show({ card_id, user_id: id });
if (item === null) {
return res.send({
isSuccess: "false",
message: "조회된 값이 없습니다(cardId나 userId를 확인해주세요)",
isSuccess: false,
message: "조회된 값이 없습니다(card_id나 user_id를 확인해주세요)",
});
}
//유저 정보 가져오기
const user_info = await userRepository.show_user(item.user_id);
if (user_info == null) {
return res.send({
isSuccess: false,
message: "조회된 값이 없습니다(user_id를 확인해주세요)",
});
}

const response = {
isSuccess: "true",
isSuccess: true,
position: item.position,
organization: item.organization,
address: item.address,
Expand All @@ -80,43 +91,42 @@ exports.inquiry = async (req, res) => {
exports.inquiry_all = async (req, res) => {
//user_id 값 가져오기
const { access_token } = req.headers;
const { user_id } = req.body;
let { user_id } = req.body;
//const [tokenType, tokenValue] = access_token.split(" ");
const { id } = jwt.verify(access_token, process.env.JWT_KEY);

checkUserInfo(res, user_id, id);

const item_all = await repository.show_all(id);

res.send({
isSuccess: "true",
resutl: item_all,
isSuccess: true,
result: item_all,
});
};

//다른 유저 명함 정보 조회
exports.inquiry_other = async (req, res) => {
const cardId = req.params.cardId;
const card_id = req.params.cardId;

//다른 유저 명함 정보 가져오기
const item = await repository.show_other(cardId);
const item = await repository.show_other(card_id);
if (item == null) {
res.send({
isSuccess: "false",
message: "조회된 값이 없습니다(cardId를 확인해주세요)",
isSuccess: false,
message: "조회된 값이 없습니다(card_id를 확인해주세요)",
});
}
//유저 정보 가져오기
const user_info = await userRepogitory.show_user(item.user_id);
const user_info = await userRepository.show_user(item.user_id);
if (user_info == null) {
return res.send({
isSuccess: "false",
message: "조회된 값이 없습니다(user_Id를 확인해주세요)",
isSuccess: false,
message: "조회된 값이 없습니다(user_id를 확인해주세요)",
});
}

const response = {
isSuccess: "true",
isSuccess: true,
position: item.position,
organization: item.organization,
address: item.address,
Expand All @@ -132,7 +142,7 @@ exports.inquiry_other = async (req, res) => {

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

const { access_token } = req.headers;
//const [tokenType, tokenValue] = access_token.split(" ");
Expand All @@ -142,24 +152,26 @@ exports.update = async (req, res) => {

checkUserInfo(res, user_id, id);
//명함 정보 가져오기
const item = await repository.show(cardId);
const item = await repository.show({ card_id, user_id });
if (item == null) {
res.send({
isSuccess: "false",
message: "조회된 값이 없습니다(cardId를 확인해주세요)",
isSuccess: false,
message: "조회된 값이 없습니다(card_id 또는 user_id를 확인해주세요)",
});
}

//수정된 항목만 업데이트
position = position || item.position;
organization = organization || item.organization;
address = address || item.address;
photo = photo || item.photo;
tell = tell || item.tell;
email = email || item.email;
//수정된 항목만 업데이트 , body 항목의 null 값 검증
position ? (position = position) : (position = item.position);
organization
? (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);

const { affectedRows, insertId } = await repository.update(
cardId,
card_id,
position,
organization,
address,
Expand All @@ -169,56 +181,53 @@ exports.update = async (req, res) => {
);

if (affectedRows > 0) {
return res.send({ isSuccess: "true", id: insertId });
return res.send({ isSuccess: true, id: insertId });
}
return res.send({ isSuccess: "false", message: "저장 실패" });
return res.send({ isSuccess: false, message: "저장 실패" });
};

//내 명함 목록 삭제
exports.delete = async (req, res) => {
const cardId = req.params.cardId;
const card_id = req.params.cardId;
const { access_token } = req.headers;
//const [tokenType, tokenValue] = access_token.split(" ");
const { id } = jwt.verify(access_token, process.env.JWT_KEY);
let { user_id } = req.body;

checkUserInfo(res, user_id, id);

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

if (affectedRows > 0) {
res.send({ isSuccess: "true" });
res.send({ isSuccess: true });
} else {
res.send({ isSuccess: "false", message: "삭제 실패" });
res.send({ isSuccess: false, message: "삭제 실패" });
}
};

const checkUserInfo = async (res, userId, id) => {
const checkUserInfo = async (res, user_id, id) => {
// 유저 정보 확인하기
if (userId != id) {
if (user_id != id) {
return res.send({
isSuccess: "false",
isSuccess: false,
message: "올바른 토큰 값이 아닙니다.",
});
}

// user_id, id 타입 일치 확인
if (typeof userId !== typeof id) {
if (user_id !== id) {
return res.send({
isSuccess: "false",
isSuccess: false,
message: "타입이 일치하지 않습니다.(user_id 타입은 int형 입니다.)",
});
}

// 유저 정보 가져오기
const user_info = await userRepogitory.show_user(item.user_id);
const user_info = await userRepository.show_user(user_id);
if (user_info === null) {
return res.send({
isSuccess: "false",
message: `조회된 값이 없습니다(${errorMessage})`,
isSuccess: false,
message: `조회된 값이 없습니다`,
});
}

// 함수를 통과했다면, 유효한 정보 반환
return { item, user_info };
};
8 changes: 5 additions & 3 deletions src/api/cards/repogitory.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ exports.create = async (
tell,
email
) => {
const query = `INSERT INTO cards (user_id, position, organization, address, photo, tell, email) VALUES (?,?,?,?,?,?,?);`;
const query = `INSERT INTO cards (user_Id, position, organization, address, photo, tell, email) VALUES (?,?,?,?,?,?,?);`;
return await pool(query, [
user_id,
position,
Expand All @@ -23,9 +23,11 @@ exports.create = async (
};

//내 명함 조회 쿼리
exports.show = async ({ cardId, userId }) => {
exports.show = async ({ card_id, user_id }) => {
console.log(card_id, user_id);
const query = `SELECT * FROM cards WHERE card_id=? AND user_id =?`;
let result = await pool(query, [cardId, userId]);
let result = await pool(query, [card_id, user_id]);

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

Expand Down

0 comments on commit 511d805

Please sign in to comment.