A calories counter backend made with Phoenix.
Built-in:
Meals
GET
http://localhost:4000/api/mealsGET
http://localhost:4000/api/meals/:idPOST
http://localhost:4000/api/mealsPUT
http://localhost:4000/api/meals/:idDELETE
http://localhost:4000/api/meals/:id
Install Erlang
, Elixir
and Phoenix
.
Create a postgress docker container with:
$ docker run --name postgres -e POSTGRES_PASSWORD=postgres -p 5432:5432 -d postgres
# Create phoenix app without webpacker or html views
$ mix phx.new app_name --no-webpack --no-html
# Intall dependencies
$ mix deps.get
# Compile project
$ mix compile
# Generate linter config file
$ mix credo.gen.config
# Run linter
$ mix credo --strict
# Start Phoenix dev server on http://localhost:4000
$ mix phx.server
# Start your project as an Interactive Elixir session
$ iex -S mix
# List all configured routes
$ mix phx.routes
Ecto:
# Create and migrate database
$ mix ecto.setup
# Create a migration
$ mix ecto.gen.migration create_users_table
# Run pending migrations
$ mix ecto.migrate
# Drop and migrate databases
$ mix ecto.reset
# Drop databases
$ mix ecto.drop
# Create databases
$ mix ecto.create
Tests
# Run tests
$ mix test
# Run tests w/ coverage report
$ mix test --cover
Create a meal:
> meal_params = %{
description: "Royal with cheese",
date: "1994-05-21 12:00:00",
calories: 500
}
> Nutri.Meals.Create.call(meal_params)
Fetch meals:
> Nutri.Repo.all(Nutri.Meal)
> Nutri.Repo.get(Nutri.Meal, 1)
> Nutri.get_meals()
> Nutri.get_meal_by_id(1)
Update meal:
> new_meal_params = %{
"id" => 1,
"description" => "UPDATEDDDDDDD",
"date" => "2021-03-24 12:00:00",
"calories" => 1000
}
> Nutri.Meals.Update.call(new_meal_params)
Delete meal:
> Nutri.delete_meal(1)