diff --git a/api/audit.js b/api/audit.js new file mode 100644 index 0000000..623b533 --- /dev/null +++ b/api/audit.js @@ -0,0 +1,89 @@ +// Required modules +const router = require('express').Router(); +const util = require("./../utilities/utilities_main.js"); +const moment = require("moment"); + +router.get("/", async function(req, res) { + try { + let conn; + try { + conn = await util.getDBConnection(); // get connection from db + const query = ` //語法有問題 + SELECT + item_form.application_id, + item_form.item_info_id, + item_form.application_unit, + item_form.subsidy, + scholarship_application.application_date, + scholarship_application.student_id, + student.student_name + FROM + item_form + RIGHT JOIN + scholarship_application ON item_form.application_id = scholarship_application.application_id; + RIGHT JOIN + student ON scholarship_application.student_id = student.student_id; + `; + const result = await conn.query(query); + res.json({ success: true, data: result }); + } + catch(e) { + console.error(e); + res.json({suc : false}); + } + finally { + util.closeDBConnection(conn); // close db connection + } + } + catch(e) { + console.log(e); + } +}) + +router.post("/", async function(req, res) { + try { + let conn; + try { + console.log(req.body); + // data + const apply_infos = req.body.apply_infos; // get data from request + const time = moment(new Date()).format("YYYY-MM-DD"); + + if (!assistant_id) { + throw new Error("Assistant ID is missing in the request body."); + } + + conn = await util.getDBConnection(); // get connection from db + await conn.beginTransaction(); + + // insert data into table : scholarship_application + const scholarship_audit_info = await conn.batch("INSERT INTO audit_form(``, ``) VALUES(?, ?);", [time, req.body.student_id]); + const scholarship_audit_id = scholarship_audit_info.insertId; // get the application_id of previous record + + + console.log(scholarship_audit_info.insertId); + console.log(apply_infos); + + // insert each apply item into item_form + for (let i = 0;i < apply_infos.length;i++) { + await conn.batch("INSERT INTO item_form(`application_id`, `item_info_id`, `application_unit`, `subsidy`) VALUES(?, ?, ?, ?);", [scholarship_application_id, apply_infos[i], application_unit, subsidy]); + } + await conn.commit(); + + res.json({suc : true}); + } + catch(e) { + console.error(e); + await conn.rollback(); + res.json({suc : false}); + } + finally { + util.closeDBConnection(conn); // close db connection + } + } + catch(e) { + console.log(e); + } +}) + +module.exports = router; \ No newline at end of file diff --git a/api/login.js b/api/login.js index bb38a79..38ec3b2 100644 --- a/api/login.js +++ b/api/login.js @@ -1,6 +1,6 @@ // Required modules const router = require('express').Router(); -const util = require("./../utilities/utilities.js"); +const util = require("./../utilities/utilities_main.js"); const jwt = require('jsonwebtoken'); router.post("/", async function(req, res) { diff --git a/api/main.js b/api/main.js index 7428bd7..28cad07 100644 --- a/api/main.js +++ b/api/main.js @@ -33,6 +33,10 @@ router.post("/", async function(req, res) { const apply_infos = req.body.apply_infos; // get data from request const time = moment(new Date()).format("YYYY-MM-DD"); + if (!student_id) { + throw new Error("Student ID is missing in the request body."); + } + conn = await util.getDBConnection(); // get connection from db await conn.beginTransaction(); diff --git a/db.js b/db.js index 88079bd..7a0dceb 100644 --- a/db.js +++ b/db.js @@ -27,7 +27,7 @@ const db = require("mariadb"); // create pool const pool = db.createPool({ - connectionLimit : 500, + connectionLimit : 10, host : 'localhost', user : 'user', password : '', diff --git a/index.js b/index.js index 52d0961..0b423fd 100644 --- a/index.js +++ b/index.js @@ -16,13 +16,15 @@ app.use(cookieParser()); //解析 HTTP 請求的 cookie // // pages +app.use("/example", require("./pages/example.js")); +app.use("/main", require("./pages/main.js")); -// app.use("/login", require("./pages/login.js")); -// app.use("/apply", require("./pages/apply.js")); ++app.use("/login", require("./pages/login.js")); ++app.use("/apply", require("./pages/apply.js")); ++app.use("/audit", require("./pages/audit.js")); // // api -// app.use("/api/login", require("./api/login.js")); +app.use("/api/login", require("./api/login.js")); app.use("/api/example", require("./api/example.js")); app.use("/api/main", require("./api/main.js")); +app.use("/api/audit", require("./api/audit.js")); // // static files app.use('/js', express.static('./js')); app.use('/css', express.static('./css')); @@ -48,70 +50,9 @@ app.get('/', (req, res) => { console.log(`Example app listening at http://localhost:${port}`) }) -// // 設定 view engine -// app.set('view engine', 'ejs') -// // 建立 todos data -// const todos = [ -// 'first todo', 'second todo', 'third todo' -// ] - -// app.get('/todos', (req, res) => { -// // 第二個參數可傳入資料 -// res.render('todos', { -// todos // todos: todos 一樣的話可省略寫法 -// }) -// }) - -// // 加上 :id 代表不確定的參數 -// app.get('/todos/:id', (req, res) => { -// // params: 可拿到網址列上指定的參數 -// const id = req.params.id -// const todo = todos[id] -// res.render('todo', { -// todo -// }) -// }) - -// app.get('/', (req, res) => { -// //res.send('index') -// }) - -// app.get('/main.ejs', (req, res) => { -// // 叫 express 去 render views 底下叫做 hello 的檔案,副檔名可省略 -// //res.render('hello') -// }) - -// app.listen(port, () => { -// console.log(`listening at http://localhost:${port}`) -// }) -// // 引入 controller -// const todoController = require('./controllers/todo') - -// app.set('view engine', 'ejs') - -// const todos = [ -// 'first todo', 'second todo', 'third todo' -// ] - -// // 可直接使用 controller 的方法拿取資料和進行 render -// app.get('/todos', todoController.getAll) - -// app.get('/todos/:id',) -// const todoController = require('./controllers/todo') - -// app.set('view engine', 'ejs') - -// app.get('/todos',) -// app.get('/todos/:id', todoController.get) - -// app.listen(port, () => { -// // 連線資料庫 -// db.connect() -// console.log(`Example app listening at http://localhost:${port}`) -// }) \ No newline at end of file diff --git a/js/audit.js b/js/audit.js new file mode 100644 index 0000000..463ef44 --- /dev/null +++ b/js/audit.js @@ -0,0 +1,121 @@ +let item_info_len = 0; + +async function getItemInfo() { + // get the data from table : item_info + try { + let result = await axios.get('/api/audit'); + result = result.data; + return result; + } + catch (e) { + console.error(e); + return null; + } +} + +async function putDataInTable(table_id) { + // get data + const data = await getItemInfo(); + if (data != null && table_id != null) { + // insert data into table + let table_content = document.getElementById(table_id).innerHTML; + + for (let i = 0; i < data.length; i++) { + + table_content += ` +
++ + ++ + ++
${data[i].item_content} -- 申請單位:- 獲得補助金額:
| 是否申請 | -項目內容 | -
+ 說明:
+ 一、依據「國立暨南國際大學資訊管理學系獎助學金作業要點」辦理。
+ 二、申辦流程:由學生詳填本申請書並檢附相關證明文件,逕送系辦審查資料是否齊全,提交學生事務委員會審核後,由系辦通知申請學生核定結果(獎學金將逕行撥入申請學生帳戶)。
+ 三、獲得本系獎助學金補助者須提供申請事項之詳細資訊,供系上公告與相關宣傳使用。若後續有召開分享會等相關需要,獲補助者需配合辦理。
+
+