// モジュール読み込み
const express = require("express");
const app = express();
const mysql = require("mysql2");
const cors = require("cors");
// クロスオリジン対策
app.use(cors());
// JSON
app.use(express.json());
// MySQLの接続情報
const db = mysql.createConnection({
user: "root", // ユーザー名
host: "localhost", // host名
password: "P@ssw0rd", // パスワード
port: 3306, // MySQLのデフォルトポート
});
// インシデント管理API
app.post("/incidents", (req, res) => {
let id = req.body.id;
const pj_name = req.body.pj_name;
const conetent = req.body.conetent;
const tss_person = req.body.tss_person;
const client_person = req.body.client_person;
const day = req.body.day;
const flag = req.body.flag;
const keyword = req.body.keyword;
const type = req.body.type;
db.connect( (error) => {
if (error) {
console.log(error);
} else {
console.log('MySQL Connected...');
}
});
let lid;
switch (type) {
// 新規登録
case "1":
new Promise((resolve, reject) => {
db.query(`select id from rsc.incidents where pj_name = "${pj_name}" order by id desc limit 1;`, (err, results) => {
if (err) {
reject(err);
return;
}
resolve(results);
});
}).then(results => {
if (results.length === 0) {
lid = 0;
} else {
lid = results[0].id;
}
}).then(() => {
id = lid + 1; // ID採番
db.query(`insert into rsc.incidents (id, pj_name, conetent, tss_person, client_person , day, flag) VALUES (${id}, "${pj_name}", "${conetent}", "${tss_person}", "${client_person}", ${day}, ${flag});`, (err, result) => {
if (err) {
res.status(500).send({ error: "登録失敗" });
} else {
res.status(200).json({ message: "登録完了" });
}
});
})
break;
// 更新
case "2":
db.query(`update rsc.incidents set conetent = "${conetent}", tss_person = "${tss_person}", client_person = "${client_person}", day = "${day}", flag = "${flag}" where id = ${id} and pj_name = "${pj_name}";`, (err, result) => {
if (err) {
console.log(err);
res.status(500).send({ error: "更新失敗" });
} else {
res.status(200).json({ message: "更新完了" });
}
});
break;
// 削除
case "3":
db.query(`delete from rsc.incidents where id = ${id} and pj_name = "${pj_name}";`, (err, result) => {
if (err) {
res.status(500).send({ error: "削除失敗" });
} else {
res.status(200).json({ message: "削除完了" });
}
});
break;
// 検索(トップページ)
case "4":
// 前日日付取得
let zfill = function(str) {
return ("00" + str).slice(-2);
}
let strftime = function(date) {
return date.getFullYear() + zfill(date.getMonth() + 1) + zfill(date.getDate());
}
let now = new Date();
now.setDate(now.getDate() - 1)
let yesterday = strftime(now);
db.query(`select * from rsc.incidents where flag=0 or day="${yesterday}" order by id asc;`, (err, result) => {
if (err) {
res.status(500).send({ error: "検索失敗" });
} else {
res.status(200).json(result);
}
});
break;
// 検索
case "5":
db.query(`select * from rsc.incidents where concat(id,pj_name,conetent,tss_person,client_person,day,flag) like "%${keyword}%" order by id asc;`, (err, result) => {
if (err) {
res.status(500).send({ error: "検索失敗" });
} else {
res.status(200).json(result);
}
});
break;
}
});
// マスター変更API
app.post("/master", (req, res) => {
let id = req.body.id;
const pj_name = req.body.pj_name;
const flag = req.body.flag;
const type = req.body.type;
db.connect( (error) => {
if (error) {
console.log(error);
} else {
console.log('MySQL Connected...');
}
});
let lid;
switch (type) {
// 新規登録
case "1":
new Promise((resolve, reject) => {
db.query(`select id from rsc.projects where pj_name = "${pj_name}" order by id desc limit 1;`, (err, results) => {
if (err) {
reject(err);
return;
}
resolve(results);
});
}).then(results => {
if (results.length === 0) {
lid = 0;
} else {
lid = results[0].id;
}
}).then(() => {
id = lid + 1; // ID採番
db.query(`insert into rsc.projects (id, pj_name, flag) VALUES (${id}, "${pj_name}", 0);`, (err, result) => {
if (err) {
res.status(500).send({ error: "登録失敗" });
} else {
res.status(200).json({ message: "登録完了" });
}
});
})
break;
// 更新
case "2":
db.query(`update rsc.projects set flag = "${flag}" where pj_name = "${pj_name}";`, (err, result) => {
if (err) {
console.log(err);
res.status(500).send({ error: "更新失敗" });
} else {
res.status(200).json({ message: "更新完了" });
}
});
break;
// 削除
case "3":
db.query(`delete from rsc.projects where pj_name = "${pj_name}";`, (err, result) => {
if (err) {
res.status(500).send({ error: "削除失敗" });
} else {
res.status(200).json({ message: "削除完了" });
}
});
break;
// 検索
case "4":
db.query(`select * from rsc.projects order by id asc;`, (err, result) => {
if (err) {
res.status(500).send({ error: "検索失敗" });
} else {
res.status(200).json(result);
}
});
break;
}
});
// データの取得
app.get("/animals", (req, res) => {
const query = "SELECT * FROM animals";
db.query(query, (err, result) => {
if (err) {
console.log(err);
res.status(500).send("Error retrieving data from database");
} else {
res.status(200).json(result);
}
});
});
// サーバーの起動
const port = 3001;
app.listen(port, () => {
console.log(Yey, your server is running on port ${port}
);
});