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 : 오답 포스트잇 개별조회, 수정, 삭제 api 추가 Back #31

Merged
merged 3 commits into from
Dec 15, 2023
Merged
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
85 changes: 83 additions & 2 deletions routes/wrong.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ router.post("/", (req, res) => {
});
});

// 퀴즈 리스트 GET API 생성
// 오답 리스트 GET API 생성
router.get("/list", (req, res) => {
const { token } = req.query;

Expand Down Expand Up @@ -69,7 +69,88 @@ router.get("/list", (req, res) => {
});
});

// 퀴즈 포스트잇 중요도 태그 별 GET API
// 오답 포스트잇 조회 API
router.get("/:id", (req, res) => {
const id = req.params.id;

// 데이터베이스 풀에서 연결 얻기
pool
.getConnection()
.then((conn) => {
// 데이터베이스에서 데이터 조회
conn
.query("SELECT * FROM Wrong WHERE id = ?", [id])
.then((results) => {
if (results.length === 0) {
// 해당 ID에 대한 데이터가 없을 경우
res.status(404).json({ error: "데이터를 찾을 수 없습니다." });
} else {
// 조회된 데이터를 JSON 응답으로 반환
const data = {
id: results[0].id,
token: results[0].token,
importance: results[0].importance,
description: results[0].description,
};
res.json(data);
}
conn.release(); // 연결 반환
})
.catch((err) => {
console.error("데이터 조회 오류:", err);
res.status(500).json({ error: "데이터 조회 오류" });
conn.release(); // 연결 반환
});
})
.catch((err) => {
console.error("데이터베이스 연결 오류:", err);
res.status(500).json({ error: "데이터베이스 연결 오류" });
});
});

// 오답 포스트잇 설명, 중요도 수정
router.put("/:id", async (req, res) => {
try {
const { id } = req.params;
const { importance, description } = req.body;
const conn = await pool.getConnection();
const result = await conn.query(
"UPDATE Wrong SET importance = ? , description =? WHERE id = ?",
[importance, description, id]
);
conn.release();

if (result.affectedRows === 0) {
res.status(404).json({ error: "데이터를 찾을 수 없습니다." });
} else {
res.json({ message: "데이터가 성공적으로 업데이트되었습니다." });
}
} catch (err) {
console.error("데이터 업데이트 오류:", err);
res.status(500).json({ error: "데이터 업데이트 오류" });
}
});

// 오답 포스트잇 삭제
router.delete("/:id", async (req, res) => {
try {
const { id } = req.params;
const conn = await pool.getConnection();
const result = await conn.query("DELETE FROM Wrong WHERE id = ?", [id]);
conn.release();

if (result.affectedRows === 0) {
res.status(404).json({ error: "데이터를 찾을 수 없습니다." });
} else {
res.json({ message: "데이터가 성공적으로 삭제되었습니다." });
}
} catch (err) {
console.error("데이터 삭제 오류:", err);
res.status(500).json({ error: "데이터 삭제 오류" });
}
});

// 오답 포스트잇 중요도 태그 별 GET API
router.get("/importance", (req, res) => {
const { token, importance } = req.query;

Expand Down