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

오류 수정 #9

Merged
merged 11 commits into from
Jan 19, 2024
25 changes: 25 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Ignore node modules
node_modules

# Ignore logs
npm-debug.log
npm-error.log

# Ignore the Git repository and its logs
.git
.gitignore
.gitattributes

# Ignore Docker files (optional)
Dockerfile
.dockerignore

# Ignore environment files
.env
.env.*

# Ignore other unnecessary files
README.md
LICENSE
.DS_Store
*.md
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM node:lts

WORKDIR /app

COPY package.json /app/package.json

RUN npm install

COPY . /app

EXPOSE 8000

CMD ["npm", "start"]
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
Loading
Loading