Todo List
Mix.install([
{:kino, github: "livebook-dev/kino", override: true},
{:kino_lab, "~> 0.1.0-dev", github: "jonatanklosko/kino_lab"},
{:vega_lite, "~> 0.1.4"},
{:kino_vega_lite, "~> 0.1.1"},
{:benchee, "~> 0.1"},
{:ecto, "~> 3.7"},
{:math, "~> 0.7.0"},
{:faker, "~> 0.17.0"},
{:utils, path: "#{__DIR__}/../utils"},
{:tested_cell, git: "https://github.com/BrooklinJazz/tested_cell"}
])
Navigation
Create A New Mix Project
Create a new project in the projects
folder called candy_store
using the command line.
mix new todo_list --sup
Evaluate the cell below to ensure you have correctly created the project.
Utils.feedback(:created_project, "todo_list")
Install Ecto Dependencies
Add the following to your dependencies in mix.exs
.
{:ecto_sql, "~> 3.0"},
{:postgrex, ">= 0.0.0"}
Install dependencies with mix deps.get
.
Generate the Repo
Set up the Ecto configuration by running the following from the project folder.
mix ecto.gen.repo -r TodoList.Repo
You may need to change your config/config.exs
file to match the username and password of your local PostgreSQL installation.
For debugging instructions, see the Ecto Documentation.
config :todo_list, TodoList.Repo,
database: "friends",
username: "user",
password: "pass",
hostname: "localhost"
Add the following to your config/config.exs
file.
config :todo_list,
ecto_repos: [TodoList.Repo]
Add the Repo to the applications supervision tree in application.ex
.
{TodoList.Repo, []}
Set Up the Database
In the project folder, run the following from the command line. This will create the corresponding database for your application.
mix ecto.create
Hopefully, everything should run, and you will see:
The database for TodoList.Repo has been created.
However, you may encounter a bug. Here are some debugging tips to consider.
- Ensure that the PostgreSQL service is running.
- Ensure that your PostgreSQL username and password match.
If you are stuck, you can speak with your teacher or classmates. You can also raise an issue on the DockYard Academy github with an explanation of your error.
Todo List
Now that we have a mix project with Ecto, you will create a to-do list application. We should be able to:
- create a to-do item
- get all to-do items
- update a to-do item
- delete a to-do item
Create An Item Migration
We can generate a new migration for items with:
mix ecto.gen.migration create_items
Define an :items
table. Each item should have a :title
, which is a :string
.
Define An Item Schema
Define a TodoList.Item
Schema. Each TodoList.Item
should have a :title
which is a :string
.
Create An Item
Define a TodoList.create_item/1
function which should accept a map to create a TodoList.Item
.
The created TodoList.Item
should persist in the database.
TodoList.create_item(%{title: "My Title"})
{:ok, %TodoList.Item{__meta__: #Ecto.Schema.Metadata<:loaded>, title: "My Title", id: 1})
Get Items
Define a TodoList.all_items/0
function which should retrieve all created TodoList.Item
s from the database.
TodoList.all_items()
{:ok, [%TodoList.Item{__meta__: #Ecto.Schema.Metadata<:loaded>, title: "My Title", id: 1}])
Update Item
Define a TodoList.update_item/2
function. It should update an existing TodoList.Item
.
It should accept the id
of an item and a map of fields to update.
This new item should persist with the changes in the database.
TodoList.update_item(1, %{title: "My New Title"})
{:ok, [%TodoList.Item{__meta__: #Ecto.Schema.Metadata<:loaded>, title: "My New Title", id: 1}])
Delete Item
Define a TodoList.delete_item/1
function. It should delete an existing TodoList.Item
from the
database.
TodoList.update_item(1, %{title: "My New Title"})
{:ok, [%TodoList.Item{__meta__: #Ecto.Schema.Metadata<:loaded>, title: "My New Title", id: 1}])
(Bonus) Filter Items
Define a TodoList.all_items/1
function which accepts a map
of params to search for.
It should be able to search by the :title
. It should also search by partially matching titles.
So "My"
would find "My Title"
Hint
You could filter using the Enum
module, however, it will be more performant to rely on Ecto.Query
..
TodoList.all_items(%{"My"})
{:ok, [%TodoList.Item{__meta__: #Ecto.Schema.Metadata<:loaded>, title: "My Title", id: 1}])
Commit Your Progress
Run the following in your command line from the project folder to track and save your progress in a Git commit.
$ git add .
$ git commit -m "finish todo list exercise"