This is a simple API for managing recipes
-
Install dependecies:
composer install
-
Create .env file
cp .env.example .env
-
Generate key
php artisan key:generate
-
Edit database settings on .env file
-
Run the migrations with seed
php artisan migrate --seed
There are 5 endpoint (postman collection is on the root folder recipe_api_postman_collection.json
)
GET api/recipes ...................... recipes.index › RecipeController@index
GET api/recipes/{recipe} ............. recipes.show › RecipeController@show
POST api/recipes ...................... recipes.store › RecipeController@store
PUT api/recipes/{recipe} ............. recipes.update › RecipeController@update
DELETE api/recipes/{recipe} ............. recipes.destroy › RecipeController@destroy
-
These methods allows us to make simple CRUD operations.
-
All requests should be in JSON format. All responses are returns also JSON format.
-
I prepared postman collection
- On the root folder. filename:
recipe_api_postman_collection.json
- It has all get,post,put,delete examples
- All examples have data including post, put, delete
- Default get api/recipe/{recipe} example is get api/recipes/1 and if returns "Record not found." probably this record's deleted_at column is not null (removed example). Because data coming from seeder randomly.
- On the root folder. filename:
Notes:
- If any error occurs like
Target class does not exists: RecipeController
please runphp artisan route:clear
What I didn't do:
- I didn't add any views.
- I didn't put an authentication middleware like
auth:sanctum
because its a demo project. At first I was added but commented later. - I didn't handle the some sql errors like
SQLSTATE[HY000]: General error: ...
, if APP_DEBUG is set true this error will show. Otherwise just displaysserver error
.- Its not a error actually. Its the default way but I want to mention.
- Examples: if user_id is not given on the post request or max string size is passed this error will show
- I didn't handle to duplicate data case on create/update.
- Maybe I should add unique option to title column. In the RecipeRequest class unique option can be add next to required option. This was a good approach
- But in this case I have to add another controls. I passed it right now.
- Lastly, I made a
ingredients
column but normally I would make this as another table, not a column. Then I will add relationships probably. But I used relationship in the Recipe model. When using the get Requests, user will show. So I didn't want to make another table and new relationship.