Blog: Posts
Mix.install([
{:jason, "~> 1.4"},
{:kino, "~> 0.8.0", override: true},
{:youtube, github: "brooklinjazz/youtube"},
{:hidden_cell, github: "brooklinjazz/hidden_cell"}
])
Navigation
Blog: Posts
You’re going to create the Posts resource in your Blog project.
Blog posts should have a title, subtitle, and content. It should also have relevant timestamp information.
classDiagram
class Post {
title: :string
subtitle: :string
content: :text
}
You may use Phoenix Generators or create the Controller, Context, and other infrastructure for the post resource or you may create it all from scratch.
Example Solution
Generate the resource.
$ mix phx.gen.html Posts Post posts title:string subtitle:string content:text
$ mix ecto.migrate
Then add the resource to router.ex.
scope "/", BlogWeb do
pipe_through :browser
get "/", PageController, :index
resources "/posts", PostController
end
Alternatively we can define every route individually.
scope "/", BlogWeb do
pipe_through :browser
get "/", PageController, :index
get "/posts", PostController, :index
get "/posts/new", PostController, :new
post "/posts", PostController, :create
get "/posts/:id", PostController, :show
get "/posts/:id/edit", PostController, :edit
put "/posts/:id", PostController, :update
patch "/posts/:id", PostController, :update
delete "/posts/:id", PostController, :delete
end
Mark As Completed
file_name = Path.basename(Regex.replace(~r/#.+/, __ENV__.file, ""), ".livemd")
save_name =
case Path.basename(__DIR__) do
"reading" -> "blog_posts_reading"
"exercises" -> "blog_posts_exercise"
end
progress_path = __DIR__ <> "/../progress.json"
existing_progress = File.read!(progress_path) |> Jason.decode!()
default = Map.get(existing_progress, save_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, save_name, completed), pretty: true)
)
end
end)
form
Commit Your Progress
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 -b blog-posts-exercise
$ git add .
$ git commit -m "finish blog posts exercise"
$ git push origin blog-posts-exercise
Create a pull request from your blog-posts-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 teacher by including @BrooklinJazz in your PR description to get feedback.
You (or your teacher) 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.
Up Next
| Previous | Next |
|---|---|
| Drills: SQL Queries | BookSearch: Authors |