Skip to content

Commit

Permalink
Merge pull request #31 from mic050r/main
Browse files Browse the repository at this point in the history
feat : 오답 포스트잇 개별조회, 수정, 삭제 api 추가 Back
  • Loading branch information
mic050r authored Dec 15, 2023
2 parents c983e82 + 087c270 commit efef5e5
Showing 1 changed file with 83 additions and 2 deletions.
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

0 comments on commit efef5e5

Please sign in to comment.