Skip to content

Commit

Permalink
Merge pull request #25 from mic050r/main
Browse files Browse the repository at this point in the history
refactor : api 라우터 분리하기
  • Loading branch information
mic050r authored Dec 12, 2023
2 parents ae0b099 + 47575a5 commit d2b74de
Show file tree
Hide file tree
Showing 8 changed files with 572 additions and 537 deletions.
541 changes: 13 additions & 528 deletions api.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions public/category(concept).html
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,19 @@
// 중요도 순으로 개념 포스트잇 데이터를 가져오는 함수
async function fetchConceptList() {
await getToken();
fetchData("/concept-list");
fetchData("/concept/list");
}

// 선택된 중요도에 따라 개념 포스트잇 데이터를 가져오는 함수
async function fetchConceptImportance(importance) {
await getToken();
fetchData("/concept-importance", "", importance);
fetchData("/concept/importance", "", importance);
}

// 중요도에 따라 정렬된 개념 포스트잇 데이터를 가져오는 함수
async function fetchConceptSort(order) {
await getToken();
fetchData("/concept-sort", order);
fetchData("/concept/sort", order);
}

// 페이지 로드 시 초기 데이터 로딩
Expand Down
6 changes: 3 additions & 3 deletions public/category(quiz).html
Original file line number Diff line number Diff line change
Expand Up @@ -162,19 +162,19 @@
// 서버에서 퀴즈 목록을 가져와 초기 데이터 로딩
async function fetchQuizList() {
await getToken();
fetchData("/quiz-list");
fetchData("/quiz/list");
}

// 중요도에 따라 퀴즈를 가져오는 함수
async function fetchQuizImportance(importance) {
await getToken();
fetchData("/quiz-importance", "", importance);
fetchData("/quiz/importance", "", importance);
}

// 중요도 정렬에 따라 퀴즈를 가져오는 함수
async function fetchQuizSort(order) {
await getToken();
fetchData("/quiz-sort", order);
fetchData("/quiz/sort", order);
}

// 페이지 로드 시 초기 데이터 로딩
Expand Down
6 changes: 3 additions & 3 deletions public/category(wronganswer).html
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,19 @@
// 서버에서 오답 목록을 가져와 초기 데이터 로딩
async function fetchWrongList() {
await getToken();
fetchData("/wrong-list");
fetchData("/wrong/list");
}

// 중요도에 따라 오답을 가져오는 함수
async function fetchWrongImportance(importance) {
await getToken();
fetchData("/wrong-importance", "", importance);
fetchData("/wrong/importance", "", importance);
}

// 중요도 정렬에 따라 오답을 가져오는 함수
async function fetchWrongSort(order) {
await getToken();
fetchData("/wrong-sort", order);
fetchData("/wrong/sort", order);
}

// 페이지 로드 시 초기 데이터 로딩
Expand Down
150 changes: 150 additions & 0 deletions routes/concept.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
const express = require("express");
const router = express.Router();
const pool = require("../db/conn"); // 데이터베이스 연결 모듈 가져오기

// 개념 포스트잇 POST API 생성
router.post("/", (req, res) => {
const { token, importance, description } = req.body;

// 데이터베이스 풀에서 연결 얻기
pool
.getConnection()
.then((conn) => {
// 데이터베이스에 데이터 삽입
conn
.query(
"INSERT INTO Concept (token, importance, description) VALUES (?, ?, ?)",
[token, importance, description]
)
.then((result) => {
console.log("데이터가 성공적으로 삽입되었습니다.");
res.json({ message: "데이터가 성공적으로 삽입되었습니다." });
conn.release(); // 연결 반환
})
.catch((err) => {
console.error("데이터 삽입 오류:", err);
res.status(500).json({ error: "데이터 삽입 오류" });
conn.release(); // 연결 반환
});
})
.catch((err) => {
console.error("데이터베이스 연결 오류:", err);
res.status(500).json({ error: "데이터베이스 연결 오류" });
});
});

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

// 데이터베이스 풀에서 연결 얻기
pool
.getConnection()
.then((conn) => {
// 데이터베이스에서 데이터 조회
conn
.query("SELECT * FROM Concept WHERE token = ?", [token])
.then((results) => {
if (results.length === 0) {
// 해당 토큰에 대한 데이터가 없을 경우
res.status(404).json({ error: "데이터를 찾을 수 없습니다." });
} else {
// 조회된 데이터를 배열로 묶어서 JSON 응답으로 반환
const data = results.map((result) => ({
description: result.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: "데이터베이스 연결 오류" });
});
});

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

// 데이터베이스 풀에서 연결 얻기
pool
.getConnection()
.then((conn) => {
// 데이터베이스에서 데이터 조회
conn
.query("SELECT * FROM Concept WHERE token = ? AND importance = ?", [
token,
importance,
])
.then((results) => {
if (results.length === 0) {
// 해당 조건에 맞는 데이터가 없을 경우
res.status(404).json({ error: "데이터를 찾을 수 없습니다." });
} else {
// 조회된 데이터를 배열로 묶어서 JSON 응답으로 반환
const data = results.map((result) => ({
description: result.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.get("/sort", (req, res) => {
const { token, order } = req.query;

// 데이터베이스 풀에서 연결 얻기
pool
.getConnection()
.then((conn) => {
// 데이터베이스에서 데이터 조회
const sqlQuery = `SELECT * FROM Concept WHERE token = ? ORDER BY importance ${
order === "asc" ? "ASC" : "DESC"
}`;
conn
.query(sqlQuery, [token])
.then((results) => {
if (results.length === 0) {
// 해당 조건에 맞는 데이터가 없을 경우
res.status(404).json({ error: "데이터를 찾을 수 없습니다." });
} else {
// 조회된 데이터를 배열로 묶어서 JSON 응답으로 반환
const data = results.map((result) => ({
description: result.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: "데이터베이스 연결 오류" });
});
});

module.exports = router;
97 changes: 97 additions & 0 deletions routes/inquiries.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// routes/inquiries.js
const express = require("express");
const router = express.Router();
const pool = require("../db/conn"); // 데이터베이스 연결 모듈 가져오기

// Inquiry 목록 조회 API
router.get("/", (req, res) => {
pool
.getConnection()
.then((conn) => {
// 데이터베이스에 데이터 삽입
conn
.query("SELECT * FROM Inquiry")
.then((result) => {
conn.release(); // 연결 반환

// 조회된 결과를 클라이언트에게 응답
res.json(result);
})
.catch((err) => {
conn.release(); // 연결 반환

console.error("조회 중 오류 발생:", err);
res.status(500).json({ error: "서버 오류" });
});
})
.catch((err) => {
console.error("데이터베이스 연결 오류:", err);
res.status(500).json({ error: "서버 오류" });
});
});

// 단일 Inquiry 조회 API
router.get("/:id", (req, res) => {
const inquiryId = req.params.id;

pool
.getConnection()
.then((conn) => {
conn
.query("SELECT * FROM Inquiry WHERE inquiry_id = ?", [inquiryId])
.then((result) => {
conn.release(); // 연결 반환

// 조회된 결과를 클라이언트에게 응답
if (result.length > 0) {
res.json(result[0]);
} else {
res
.status(404)
.json({ error: "해당 ID의 Inquiry를 찾을 수 없습니다." });
}
})
.catch((err) => {
conn.release(); // 연결 반환

console.error("조회 중 오류 발생:", err);
res.status(500).json({ error: "서버 오류" });
});
})
.catch((err) => {
console.error("데이터베이스 연결 오류:", err);
res.status(500).json({ error: "서버 오류" });
});
});

// Inquiry 생성 API
router.post("/", (req, res) => {
const { user_id, title, message, status } = req.body;

pool
.getConnection()
.then((conn) => {
// 데이터베이스에 데이터 삽입
conn
.query(
"INSERT INTO Inquiry (user_id, title, message, status) VALUES (?, ?, ?, ?)",
[user_id, title, message, status]
)
.then((result) => {
console.log("데이터가 성공적으로 삽입되었습니다.");
res.json({ message: "데이터가 성공적으로 삽입되었습니다." });
conn.release(); // 연결 반환
})
.catch((err) => {
console.error("데이터 삽입 오류:", err);
res.status(500).json({ error: "데이터 삽입 오류" });
conn.release(); // 연결 반환
});
})
.catch((err) => {
console.error("데이터베이스 연결 오류:", err);
res.status(500).json({ error: "데이터베이스 연결 오류" });
});
});

module.exports = router;
Loading

0 comments on commit d2b74de

Please sign in to comment.