I'm thinking about how to structure the end points of a RESTful API.
Problem description
- There are recipes and users.
- A user can save recipes and unsave them.
- A user can favorite recipes that he has saved and defavorite them.
- A user has to give saved recipes a name the first time he saves them. He can edit that name later on.
- It should be possible to easily retrieve all saved recipes of a user, along with information regarding which recipes are his favorite and the names of the recipes.
Question
How to structure the back-end API end points?
Proposals
These are 4 proposals to structure it. Which one do you think is better? Do you have another proposal?
Proposal 1:
| End point | Method | Meaning |
|---|---|---|
users/{userId}/saved-recipes |
GET | Retrieve all saved recipes of a user. The response also contains information about the names of these recipes and whether they're the favorite of the user. |
recipes/{recipeId}/save |
POST | Save a recipe. |
recipes/{recipeId}/unsave |
POST | Unsave a recipe. |
recipes/{recipeId}/favorite |
POST | Favorite a saved recipe. |
recipes/{recipeId}/defavorite |
POST | Defavorite a saved recipe. |
recipes/{recipeId}/edit-name |
POST | Edit the name of a saved recipe. |
Proposal 2:
| End point | Method | Meaning |
|---|---|---|
users/{userId}/saved-recipes |
GET | Retrieve all saved recipes of a user. The response also contains information about the names of these recipes and whether they're the favorite of the user. |
recipes/{recipeId}/save |
POST | Save a recipe. |
recipes/{recipeId}/save |
DELETE | Unsave a recipe. |
recipes/{recipeId}/favorite |
POST | Favorite a saved recipe. |
recipes/{recipeId}/favorite |
DELETE | Defavorite a saved recipe. |
recipes/{recipeId}/edit-name |
POST | Edit the name of a saved recipe. |
Proposal 3 (my personal favorite):
| End point | Method | Meaning |
|---|---|---|
users/{userId}/saved-recipes |
GET | Retrieve all saved recipes of a user. The response also contains information about the names of these recipes and whether they're the favorite of the user. |
users/{userId}/saved-recipes |
POST | Save a recipe. |
users/{userId}/saved-recipes/{recipeId} |
DELETE | Unsave a recipe. |
users/{userId}/saved-recipes/{recipeId}/favorite |
POST | Favorite a saved recipe. |
users/{userId}/saved-recipes/{recipeId}/favorite |
DELETE | Defavorite a saved recipe. |
users/{userId}/saved-recipes/{recipeId}/edit-name |
POST | Edit the name of a saved recipe. |
Proposal 4:
| End point | Method | Meaning |
|---|---|---|
users/{userId}/saved-recipes |
GET | Retrieve all saved recipes of a user. The response also contains information about the names of these recipes and whether they're the favorite of the user. |
users/{userId}/saved-recipes |
POST | Save a recipe. |
users/{userId}/saved-recipes/{recipeId} |
DELETE | Unsave a recipe. |
users/{userId}/saved-recipes/{recipeId}/edit |
POST | Single end point to change the information about the relation between a user and a saved recipe. You can favorite and defavorite a recipe and edit the name of it using this single end point by providing fields in the body of the request such as {favorite: true, name: "Saved 1"}. The provided fields will be updated accordingly. |