Skip to content

Instantly share code, notes, and snippets.

@vinikira
Created August 3, 2020 13:03
Show Gist options
  • Save vinikira/0df92cb1c32ed15c5742f3c5810e4487 to your computer and use it in GitHub Desktop.
Save vinikira/0df92cb1c32ed15c5742f3c5810e4487 to your computer and use it in GitHub Desktop.
Snippets utilizados durante a aprensetação "Introdução ao Phoenix LiveView".
# Filter event
@impl true
def handle_event("filter", %{"director" => director, "rating" => rating}, socket) do
params = [director: director, rating: rating]
movies = Movies.list_movies(params)
socket =
assign(
socket,
params ++ [movies: movies]
)
{:noreply, socket}
end
# Filter function
def list_movies(criteria) when is_list(criteria) do
query = from(g in Movie)
Enum.reduce(criteria, query, fn
{:director, ""}, query ->
query
{:director, director}, query ->
from q in query, where: q.director == ^director
{:rating, ""}, query ->
query
{:rating, rating}, query ->
from q in query, where: q.rating == ^rating
end)
|> Repo.all()
end
# Options for rating and director select
defp rating_options() do
[
"Todos": "",
"1 estrela": 1,
"2 estrelas": 2,
"3 estrelas": 3,
"4 estrelas": 4,
"5 estrelas": 5,
]
end
defp director_options() do
[
"Todos": "",
"Joss Whedon": "Joss Whedon",
"John Lasseter": "John 3",
"Hayao Miyazaki": "Hayao Miyazaki",
"Frank Coraci": "Frank Coraci",
"Edgar Wright": "Edgar Wright",
"Antoine Fuqua": "Antoine Fuqua",
"Pete Docter": "Pete Docter"
]
end
%Movie{
title: "Os Vingadores",
director: "Joss Whedon",
rating: 5
}
|> Repo.insert!()
%Movie{
title: "Vingadores: A Era de Ultron",
director: "Joss Whedon",
rating: 4
}
|> Repo.insert!()
%Movie{
title: "Vida de Inseto",
director: "John Lasseter",
rating: 4
}
|> Repo.insert!()
%Movie{
title: "A Viagem de Chihiro",
director: "Hayao Miyazaki",
rating: 5
}
|> Repo.insert!()
%Movie{
title: "O Meu Amigo Totoro",
director: "Hayao Miyazaki",
rating: 3
}
|> Repo.insert!()
%Movie{
title: "Click",
director: "Frank Coraci",
rating: 3
}
|> Repo.insert!()
%Movie{
title: "Volta ao mundo em 80 dias",
director: "Frank Coraci",
rating: 2
}
|> Repo.insert!()
%Movie{
title: "Baby Driver",
director: "Edgar Wright",
rating: 5
}
|> Repo.insert!()
%Movie{
title: "Os Sete Magníficos",
director: "Antoine Fuqua",
rating: 4
}
|> Repo.insert!()
%Movie{
title: "Up - Altamente!",
director: "Pete Docter",
rating: 4
}
|> Repo.insert!()
# Postgres on docker
docker run --name guilda_elixir\
-e "POSTGRES_PASSWORD=postgres"\
-p 5432:5432\
-v teste-postgres:/var/lib/postgresql/data\
-d postgres:12-alpine
# Create LiveView Project
mix phx.new live_view_intro --live
# Start Phoenix server
iex -S mix phx.server
# Movie context generator
mix phx.gen.context Movies Movie movies title:string director:string rating:integer
#
.form-movies > * {
display: inline-block
}
<h1>Lista de Filmes</h1>
<form class="form-movies" phx-change="filter">
<label for="director">Diretor</label>
<select id="director" name="director">
<%= options_for_select(director_options(), @director) %>
</select>
<label for="rating">Avaliação</label>
<select id="rating" name="rating">
<%= options_for_select(rating_options(), @rating) %>
</select>
</form>
<table>
<thead>
<tr>
<th>Título</th>
<th>Diretor</th>
<th>Avaliação</th>
</tr>
</thead>
<tbody>
<%= for movie <- @movies do %>
<tr>
<td><%= movie.title %></td>
<td><%= movie.director %></td>
<td><%= movie.rating %></td>
</tr>
<% end %>
</tbody>
</table>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment