0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

シフト管理CURDアプリ ~Controller編~ 

0
Last updated at Posted at 2026-01-09

作成している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)
};

req.params
2026-01-19 22.30の画像.jpeg
2026-01-19 22.31の画像.jpeg

④シフトの更新

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);
};

応用
全てのシフトデータの削除

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?