Mix.install([
{:jason, "~> 1.4"},
{:kino, "~> 0.9", override: true},
{:youtube, github: "brooklinjazz/youtube"},
{:hidden_cell, github: "brooklinjazz/hidden_cell"}
])
Using the command line, create a new project in the projects
folder called candy_store
.
mix new candy_store
In this exercise, you will build a candy store application which searches and filters candy products from existing products.
Each product should be a struct with a :name
, :cost
, and :type
.
The :name
will be a string. The :cost
will be an integer (in cents). The :type
will be an atom.
classDiagram
Product {
name: :string,
cost: :integer,
type: :atom
}
Create a CandyStore.Item
module in a lib/item.ex
file.
It should be a struct with the :name
, :cost
, and :type
fields.
:name
, :cost
, and :type
should be enforced.
Create the following test cases for CandyStore.Item
.
Create a CandyStore.ItemTest
module in tests/item_test.ex
.
Create the following test cases for the following:
- given a
:name
,:cost
, and:type
field it should create an Item struct. - The Item struct should enforce the
:name
and:type
fields.
The CandyStore.search/2
function should take a list of items, and the a search query to filter them by.
For example,
items = [%Item{name: "AAA", type: :candy_bar, cost: 120}, %Item{name: "BBB", type: :candy_bar, cost: 120}, ]
query = %{name: "A", type: :candy_bar, cost: 120}
CandyStore.search(items, query)
%Item{name: "AAA", type: :candy_bar, cost: 120}
Create a CandyStore.search/2
function and the following test cases:
(You may use any test names, so long as your tests cover the following functionality)
- given a list of items, when searching the full name of one item, then it should only find matching items.
- given a list of items, when searching the partial name of one item, then it should only find matching items.
- given a list of items, when searching the a non matching name, it should find no items.
- given a list of items, when searching by type, it should find matching items.
- given a list of items, when searching by a non-matching type, it should find no items.
- given a list of items, when searching by exact cost, it should find matching items.
- given a list of items, when searching by all filters (:type, :cost, and :name) it should find matching items.
Ensure all tests pass.
Add the following functionality and cases to allow users to search by price:
- given a list of items, when searching by
:max_cost
, it should find items below the max cost. - given a list of items, when searching by
:min_cost
, it should find items below the max cost. - given a list of items, when searching by
:cost
with a range, it should find items within the range.
Use Ecto Changesets
validate the Item
struct in an Item.new/1
function.
:name
must be a string.
:type
must be an atom.
:cost
must be an integer.
Then create the following test cases:
Item.new/1
should return a changeset error if:name
is not a string.Item.new/1
should return a changeset error if:type
is not an atom.Item.new/1
should return a changeset error if:cost
is not a integer.Item.new/1
should return a struct if:name
,:type
, and:cost
are all valid.
file_name = Path.basename(Regex.replace(~r/#.+/, __ENV__.file, ""), ".livemd")
progress_path = __DIR__ <> "/../progress.json"
existing_progress = File.read!(progress_path) |> Jason.decode!()
default = Map.get(existing_progress, file_name, false)
form =
Kino.Control.form(
[
completed: input = Kino.Input.checkbox("Mark As Completed", default: default)
],
report_changes: true
)
Task.async(fn ->
for %{data: %{completed: completed}} <- Kino.Control.stream(form) do
File.write!(progress_path, Jason.encode!(Map.put(existing_progress, file_name, completed)))
end
end)
form
Run the following in your command line from the curriculum folder to track and save your progress in a Git commit.
Ensure that you do not already have undesired or unrelated changes by running git status
or by checking the source control tab in Visual Studio Code.
$ git checkout solutions
$ git checkout -b candy-store-exercise
$ git add .
$ git commit -m "finish candy store exercise"
$ git push origin candy-store-exercise
Create a pull request from your candy-store-exercise
branch to your solutions
branch.
Please do not create a pull request to the DockYard Academy repository as this will spam our PR tracker.
DockYard Academy Students Only:
Notify your instructor by including @BrooklinJazz
in your PR description to get feedback.
You (or your instructor) may merge your PR into your solutions branch after review.
If you are interested in joining the next academy cohort, sign up here to receive more news when it is available.