コンテンツにスキップ

更新の設計

03 章 更新の設計

メモを書き換える操作(Update)を書き足します。

メソッド パス 中身
PUT /notes/{id} 送った中身でメモを置き換えて返す
api/openapi.yaml
111
111
description: ID が UUID の形でない
112
112
'404':
113
113
description: メモが無い
114
put:
115
summary: メモを置き換える(全部の項目を送る)
116
parameters:
117
- name: id
118
in: path
119
required: true
120
schema:
121
type: string
122
format: uuid
123
requestBody:
124
required: true
125
content:
126
application/json:
127
schema:
128
type: object
129
additionalProperties: false
130
required: [title, body]
131
properties:
132
title:
133
type: string
134
minLength: 1
135
maxLength: 100
136
body:
137
type: string
138
maxLength: 1000
139
responses:
140
'200':
141
description: 書き換えたメモ
142
content:
143
application/json:
144
schema:
145
type: object
146
required: [id, title, body, createdAt, updatedAt]
147
properties:
148
id:
149
type: string
150
format: uuid
151
title:
152
type: string
153
body:
154
type: string
155
createdAt:
156
type: string
157
format: date-time
158
updatedAt:
159
type: string
160
format: date-time
161
'400':
162
description: 入力が決まりに合わない
163
'404':
164
description: メモが無い
  • PUT は、送った中身でまるごと置き換えるメソッドです
  • title と body の両方を必ず送ります。required: [title, body] で、どちらも省略できないことを書きます。片方だけ送ると 400 になります
  • Create と違って default を書きません。全部の項目を送るので、補う値は要りません

タイトルだけ・本文だけのように、送った項目だけを書き換えることを 部分更新 と呼びます。部分更新には PATCH を使うのが HTTP の決まりです。PATCH にするときは、メソッドを patch にし、本文の required を外して、どの項目も省略できるようにします。

VS Code で api/openapi.yaml を開き、エディタの右上のプレビューのボタンを押します。横に API のドキュメントの画面が出ます。 PUT /notes/{id} が増え、本文の title と body が必須になっていれば大丈夫です。

この節は、次の公式ドキュメントをもとに書いています。

この節を終えたときのディレクトリ

Section titled “この節を終えたときのディレクトリ”
この節を終えた状態をダウンロード