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?

シフト管理CRUDアプリ ~API設計編~

0
Last updated at Posted at 2026-01-06

参考教材

RESTful API設計の流れ(チャッピー5.2ver)

① リソース定義(何を扱うAPIか)
② URI設計
③ HTTPメソッド
④ パスパラメータ / クエリパラメータ
⑤ リクエストボディ
⑥ ステータスコード
⑦ レスポンスデータのフォーマット
⑧ データ内部構造設計
⑨ その他設計(認証・セキュリティ)

⭐️完成したAPI設計

# シフト管理CRUDアプリ

## リソース定義
### shifts
- 勤務予定1件を表すリソース
- 勤務日、勤務時間、勤務中のお知らせなどの情報を持つ
- 主な属性: id, shift_date, start_time, end_time, shift_memo

### users
- 店舗で働くスタッフを表すリソース
- スタッフの名前、役職などの情報を持つ
- 主な属性: id, user_name, user_role

### shift_members
- 誰がどのシフトに入るかの関係を表すリソース
- 1つのシフトに複数人入れるために使用
- 主な属性: shift_id, user_id

---

## URI設計
https://example.com/api/shift-members

---

## HTTPメソッド
### GET
- シフト情報一覧の取得: GET https://example.com/api/shifts
- 特定ユーザー情報の取得: GET https://example.com/api/users/{id}

### POST
- 新しいシフトの追加: POST https://example.com/api/shifts
- ユーザーを追加: POST https://example.com/api/users

### PUT
- シフトの更新: PUT https://example.com/api/shifts/{id}
- ユーザーの更新: PUT https://example.com/api/users/{id}

### DELETE
- ユーザーの削除: DELETE https://example.com/api/users/{id}

---

## データフォーマット
### リクエストヘッダー
Host: example.com
Accept: application/json
Content-Type: application/json

### JSONフォーマット
{
  "shift": {
    "id":1,
    "shift_date":"2025-01-01",
    "start_time":"05:00",
    "end_time":"10:00",
    "shift_memo":"朝ぴー"
  },
  "user": {
    "id":1,
    "user_name":"Matsuda",
    "user_role":"manager"
  }
}
{
  "error":{
    "code": "NOT_FOUND",
    "message": "リソースが存在しません"
    }
}

リソース定義

見様見真似でやったリソース定義

## リソース定義
### shifts
- 勤務の詳細情報
- shiftsID, shift_date, start_time, end_time, shift_memoを持つ

### users
- お店のスタッフの名前と役職の情報
- usersID, user_name, user_roleを持つ

### shift_members
- shiftsとusersを繋げるためのテーブル

チャッピー5.2に添削されたリソース定義

## リソース定義

### shifts
- 勤務予定1件を表すリソース
- 勤務日・開始時間・終了時間・メモなどの勤務情報を持つ
- 主な属性: id, shift_date, start_time, end_time, shift_memo

### users
- 店舗で働くスタッフを表すリソース
- スタッフの名前や役職などの基本情報を持つ
- 主な属性: id, user_name, user_role

### shift_members
- シフトとスタッフの割り当て関係を表すリソース
- 1つのシフトに複数スタッフを割り当てるために使用する
- 主な属性: id, shift_id, user_id

⭐️修正したリソース定義

## リソース定義

### shifts
- 勤務予定1件を表すリソース
- 勤務日、勤務時間、勤務中のお知らせなどの情報を持つ
- 主な属性: id, shift_date, start_time, end_time, shift_memo

### users
- 店舗で働くスタッフを表すリソース
- スタッフの名前、役職などの情報を持つ
- 主な属性: id, user_name, user_role

### shift_members
- 誰がどのシフトに入るかの関係を表すリソース
- 1つのシフトに複数人入れるために使用
- 主な属性: shift_id, user_id

URI設計

URI設計のルール

  • 短くて入力がしやすい
  • 省略しない
  • 大文字と小文字が混ざってない(基本は小文字)
  • 単語はハイフン繋ぎ
  • 単語は複数形を使う
  • エンコードが必要な文字を使わない
  • 更新がしやすい
  • ルールが統一されている

見様見真似でやったURI設計

## URI設計
https://example.com/shift-members

チャッピー5.2に添削されたURI定義

最低限の修正(今の形を活かす)
https://example.com/api/shift-members

シフト管理っぽくして読みやすく(おすすめ)
https://example.com/api/shifts/{shift_id}/members

※おすすめのURI(ネスト)がよく分からないので保留

⭐️修正したURI定義

## URI設計
https://example.com/api/shift-members

HTTPメソッド

2026-01-04 23.59の画像.jpeg

見様見真似でやったHTTPメソッド

### GET
- シフト情報一覧の取得: GET https://example.com/api/shifts
- 特定ユーザー情報の取得: GET https://example.com/api/users

### POST
- 新しいシフトの追加: POST https://example.com/api/shifts
- ユーザーを追加: POST https://example.com/api/users

### PUT
- シフトの更新: PUT https://example.com/api/shifts
- ユーザーの更新: PUT https://example.com/api/users

### DELETE
- ユーザーの削除: DELETE https://example.com/api/usersz

クエリパラメーターとパスパラメーター

パスパラメーター
一意なリソースを表すときに利用

クエリパラメーター
省略してもいい場合に利用

⭐️修正したHTTPメソッド

## HTTPメソッド
### GET
- シフト情報一覧の取得: GET https://example.com/api/shifts
- 特定ユーザー情報の取得: GET https://example.com/api/users/{id}

### POST
- 新しいシフトの追加: POST https://example.com/api/shifts
- ユーザーを追加: POST https://example.com/api/users

### PUT
- シフトの更新: PUT https://example.com/api/shifts/{id}
- ユーザーの更新: PUT https://example.com/api/users/{id}

### DELETE
- ユーザーの削除: DELETE https://example.com/api/users/{id}

パスパラメーターの追加
一意なリソースが必要なもの
特定ユーザー情報の取得、シフトとユーザーの更新、削除

⚠️新しいシフトの追加
POSTは「まだ存在していない新しいリソース」を作成する操作なので、パスは必要ない

ステータスコード

データフォーマット

## データフォーマット
### リクエストヘッダー
Host: example.com
Accept: application/json
Content-Type: application/json

### JSONフォーマット
{
  "shift": {
    "id":1,
    "shift_date":"2025-01-01",
    "start_time":"05:00",
    "end_time":"10:00",
    "shift_memo":"朝ピー..."
  },
  "user": {
    "id":1,
    "user_name":"Matsuda",
    "user_role":"manager"
  }
};

Content-Type
自分が送るデータ形式

Accept
相手に返して欲しいデータ形式

データの内部構造

エラー表現

{
  "error":{
    "code": "NOT_FOUND",
    "message": "リソースが存在しません"
    }
}

※本来は代表される(予想される)エラー全てのエラー表現を記入

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?