-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
129 lines (118 loc) · 3.86 KB
/
Copy pathindex.js
File metadata and controls
129 lines (118 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// index.js
const express = require("express");
const cors = require("cors");
const oracledb = require("oracledb");
const app = express();
const port = 3000;
// Middleware
app.use(cors());
app.use(express.json());
// ข้อมูลการเชื่อมต่อ Oracle
const dbConfig = {
user: "c##devuser", // เปลี่ยนให้ตรงกับ user ที่ใช้
password: "mypassword", // เปลี่ยนให้ตรงกับรหัสผ่านที่ตั้งไว้
connectString: "localhost:1521/XE", // เปลี่ยนตามค่าที่ใช้ใน Oracle XE
};
async function runQuery(query, binds = [], options = {}) {
let connection;
try {
// เชื่อมต่อฐานข้อมูล
connection = await oracledb.getConnection(dbConfig);
console.log("Connected to Oracle Database"); // ตรวจสอบการเชื่อมต่อ
// รันคำสั่ง SQL
const result = await connection.execute(query, binds, options);
return result;
} catch (err) {
console.error("Error during query execution:", err); // แสดงข้อผิดพลาด
throw err;
} finally {
if (connection) {
try {
await connection.close();
console.log("Connection closed");
} catch (err) {
console.error("Error closing connection:", err);
}
}
}
}
// อ่านข้อมูลผู้ใช้ทั้งหมด
app.get("/users", async (req, res) => {
try {
const result = await runQuery("SELECT * FROM employees");
res.json(result.rows);
} catch (err) {
res.status(500).send("Error fetching users");
}
});
// อ่านข้อมูลผู้ใช้ตาม ID
app.get("/users/:id", async (req, res) => {
try {
const result = await runQuery(
"SELECT * FROM employees WHERE employee_id = :id",
[req.params.id]
);
if (result.rows.length === 0) {
res.status(404).send("User not found");
} else {
res.json(result.rows[0]);
}
} catch (err) {
res.status(500).send("Error fetching user");
}
});
// สร้างผู้ใช้ใหม่
app.post("/users", async (req, res) => {
try {
const { name, email } = req.body;
const result = await runQuery(
"INSERT INTO employees (first_name, last_name, email, hire_date) VALUES (:first_name, :last_name, :email, SYSDATE) RETURNING employee_id INTO :id",
{
first_name: name.split(" ")[0],
last_name: name.split(" ")[1] || "",
email: email,
id: { dir: oracledb.BIND_OUT, type: oracledb.NUMBER },
},
{ autoCommit: true }
);
res.status(201).json({ id: result.outBinds.id[0], name, email });
} catch (err) {
res.status(500).send("Error creating user");
}
});
// อัปเดตข้อมูลผู้ใช้
app.put("/users/:id", async (req, res) => {
try {
const { name, email } = req.body;
await runQuery(
"UPDATE employees SET first_name = :first_name, last_name = :last_name, email = :email WHERE employee_id = :id",
{
first_name: name.split(" ")[0],
last_name: name.split(" ")[1] || "",
email: email,
id: req.params.id,
},
{ autoCommit: true }
);
res.send("User updated successfully");
} catch (err) {
res.status(500).send("Error updating user");
}
});
// ลบผู้ใช้
app.delete("/users/:id", async (req, res) => {
try {
await runQuery(
"DELETE FROM employees WHERE employee_id = :id",
[req.params.id],
{ autoCommit: true }
);
res.send("User deleted successfully");
} catch (err) {
res.status(500).send("Error deleting user");
}
});
// เริ่มต้นเซิร์ฟเวอร์
app.listen(port, () => {
console.log(`API is running at http://localhost:${port}`);
});