-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from mic050r/main
refactor : api 라우터 분리하기
- Loading branch information
Showing
8 changed files
with
572 additions
and
537 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.