Login and get codingNow that we're able to create and retrieve
Food
objects, it's time to add code to update and delete them.Create the two corresponding endpoints:
1. To update, the API should
1. receive a
PUT
request with theFood
object's id and new / updated object2. update the item in the
foods
dictionary (to keep it simple, no need to update individual attributes, just swap out the wholeFood
object matching itsid
)3. return the updated item.
2. To delete the API should
1. receive a
DELETE
request with the id of thefood
to be deleted2. delete it from the
foods
dictionary3. return a respone of
{"ok": True}
(as found here).* We use
PUT
because we're replacing the whole item. For a partial update,PATCH
would be the more appropriate HTTP verb / action.For both endpoints raise an
HTTPException
with astatus_code
of404
and message (detail
) ofFood not found
, when theid
passed in is not in thefoods
dictionary.WIN: You have a simple API for food objects up and running with minimal code! We still need to add authentication, but first we'll make two more Pydantic models so that a user can start logging daily food intake.
147 out of 151 users completed this Bite.
Will you be the 148th person to crack this Bite?
Resolution time: ~36 min. (avg. submissions of 5-240 min.)
Our community rates this Bite 5.0 on a 1-10 difficulty scale.
» Up for a challenge? 💪