For my phase 3 project I decided to make a calorie tracker. I had a lot of fun working on this project. In this project I have two models, Day
and Food
. There are many foods belonging to one day. For the backend, one thing I enjoyed working on was setting up the routes to view the various endpoints for my databases. For viewing the full database I kept my endpoint simple: /days
.
get "/days" do
days = Day.all
days.to_json(include: :foods)
end
What this code block is doing is grabbing all instances of the Day class and converting it to JSON. Then, it is nesting each food instance with it's associated Day.
For my POST routes, I have an endpoint to create a new day and another one to create a new food through the day by id.
post '/days' do
day = Day.create(
date: params[:date],
foods: params[:foods],
)
day.to_json(include: :foods)
end
This is creating a Day instance and gives it a date that the user enters in the frontend, and an empty foods array for the value of foods.
post '/day/:id/foods' do
day = Day.find(params[:id])
food = day.foods.create!(
name: params[:name],
calories: params[:calories],
fat: params[:fat],
fiber: params[:fiber],
day_id: params[:day_id],
)
{}.to_json(include: :foods)
end
This is finding a day instance in the class Day. Then, it is creating a food instance in that day.
I have a PATCH route for both models. They are very similar so I will only talk about the route to patch foods.
patch '/foods/:id' do
food = Food.find(params[:id])
food.update(
name: params[:name],
calories: params[:calories],
fat: params[:fat],
fiber: params[:fiber],
day_id: params[:day_id],
)
food.to_json
end
This is finding an instance of a food by id. And then, it is updating the instance with information provided by the frontend.
Similar to my PATCH routes, my DELETE routes are exactly the same just with differnt values.
delete '/days/:id' do
day = Day.find(params[:id])
day.destroy
day.to_json
end
This finds a day instance by id. Then it destroys it. In the Day model there is a dependent: :destroy
so that when a day is deleted all of the foods associated with that day are deleted as well. I've been told the last line of the code block is unnecessary, but every time I delete it my delete button stops working in the front end.
I also wrote some routes that I am not using in this project, but I have seen similar functionality in APIs that I have used. So, I wanted to create some extra endpoints. I have a route to get all days without their associated foods; I have a route to get all foods without their day, and I have a route to get one day by id and another one to get one food by id.
get "/days" do
days = Day.all
days.to_json
end
This returns all days
get "/foods" do
foods = Food.all
foods.to_json
end
This returns all foods.
get "/days/:id" do
day = Day.find(params[:id])
day.to_json
end
This returns one day by id. The id is provided from the front end.
get "/foods/:id" do
food = Food.find(params[:id])
food.to_json
end
This returns one food by id. The id is provided from the front end.
I also created a route to get the last seven days with their nested foods.
get "/days_last_seven" do
week = Day.last(7)
week.to_json(include: :foods)
end
I really enjoyed this project and thinking about all of the different routes I may want to access to interact with the front end application.
Top comments (0)