作成しているCRUDアプリ
参考教材
コントローラー作成
①シフトの新規作成
controllers/shift_controller.js
// シフトの新規登録
export const create = async (req, res) => {
const { shift_date, start_time, end_time, shift_memo } = req.body;
if(!shift_date){
res.status(400).send({
message: 'いつのシフトですか?'
});
return
}
if(!start_time){
res.status(400).send({
message: '何時から出勤ですか?'
});
return
}
if(!end_time){
res.status(400).send({
message: '何時まで出勤ですか?'
});
return
}
const newShift = {
shift_date,
start_time,
end_time,
shift_memo
};
await createShift(newShift);
// 成功レスポンス
res.status(201).send({
message:'シフト管理の追加に成功しました'
});
return
};
②シフトの全部取得
controllers/shift_controller.js
// 全件取得
export const getAll = async (req, res) => {
// repositoryを呼ぶ処理
const repoGetAll = await getAllShift();
//res.jsonを返す処理
res.json(repoGetAll);
};
応用
try / catchを使ったDBエラー対策(コントローラー)
③シフトのid検索
controllers/shift_controller.js
// id取得
export const getId = async (req, res) => {
const id = req.params.id;
const repoGetId = await getShift(id);
res.json(repoGetId)
};
④シフトの更新
controllers/shift_controller.js
// ④シフトの更新
export const update = async (req, res) => {
const id = Number(req.params.id);
const up = req.body;
const repoUpdate = await updateShift(up, id);
res.json(repoUpdate);
};
⑤シフトの削除
controllers/shift_controller.js
// ⑤シフトの削除
export const deleteId = async (req, res) => {
const erase = Number(req.params.id)
const repoDelete = await deleteShift(erase);
res.json(repoDelete);
};
応用
全てのシフトデータの削除

