-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
460 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
defmodule ElixirLangTokyo.Community do | ||
@moduledoc """ | ||
The Community context. | ||
""" | ||
|
||
import Ecto.Query, warn: false | ||
alias ElixirLangTokyo.Repo | ||
|
||
alias ElixirLangTokyo.Community.Meetup | ||
|
||
@doc """ | ||
Returns the list of meetups. | ||
## Examples | ||
iex> list_meetups() | ||
[%Meetup{}, ...] | ||
""" | ||
def list_meetups do | ||
Repo.all(Meetup) | ||
end | ||
|
||
@doc """ | ||
Gets a single meetup. | ||
Raises `Ecto.NoResultsError` if the Meetup does not exist. | ||
## Examples | ||
iex> get_meetup!(123) | ||
%Meetup{} | ||
iex> get_meetup!(456) | ||
** (Ecto.NoResultsError) | ||
""" | ||
def get_meetup!(id), do: Repo.get!(Meetup, id) | ||
|
||
@doc """ | ||
Creates a meetup. | ||
## Examples | ||
iex> create_meetup(%{field: value}) | ||
{:ok, %Meetup{}} | ||
iex> create_meetup(%{field: bad_value}) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def create_meetup(attrs \\ %{}) do | ||
%Meetup{} | ||
|> Meetup.changeset(attrs) | ||
|> Repo.insert() | ||
end | ||
|
||
@doc """ | ||
Updates a meetup. | ||
## Examples | ||
iex> update_meetup(meetup, %{field: new_value}) | ||
{:ok, %Meetup{}} | ||
iex> update_meetup(meetup, %{field: bad_value}) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def update_meetup(%Meetup{} = meetup, attrs) do | ||
meetup | ||
|> Meetup.changeset(attrs) | ||
|> Repo.update() | ||
end | ||
|
||
@doc """ | ||
Deletes a meetup. | ||
## Examples | ||
iex> delete_meetup(meetup) | ||
{:ok, %Meetup{}} | ||
iex> delete_meetup(meetup) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def delete_meetup(%Meetup{} = meetup) do | ||
Repo.delete(meetup) | ||
end | ||
|
||
@doc """ | ||
Returns an `%Ecto.Changeset{}` for tracking meetup changes. | ||
## Examples | ||
iex> change_meetup(meetup) | ||
%Ecto.Changeset{data: %Meetup{}} | ||
""" | ||
def change_meetup(%Meetup{} = meetup, attrs \\ %{}) do | ||
Meetup.changeset(meetup, attrs) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
defmodule ElixirLangTokyo.Community.Meetup do | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
schema "meetups" do | ||
field :hashtag, :string | ||
field :name, :string | ||
field :site, :string | ||
|
||
timestamps() | ||
end | ||
|
||
@doc false | ||
def changeset(meetup, attrs) do | ||
meetup | ||
|> cast(attrs, [:name, :site, :hashtag]) | ||
|> validate_required([:name, :site, :hashtag]) | ||
end | ||
end |
62 changes: 62 additions & 0 deletions
62
app/lib/elixir_lang_tokyo_web/controllers/meetup_controller.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
defmodule ElixirLangTokyoWeb.MeetupController do | ||
use ElixirLangTokyoWeb, :controller | ||
|
||
alias ElixirLangTokyo.Community | ||
alias ElixirLangTokyo.Community.Meetup | ||
|
||
def index(conn, _params) do | ||
meetups = Community.list_meetups() | ||
render(conn, "index.html", meetups: meetups) | ||
end | ||
|
||
def new(conn, _params) do | ||
changeset = Community.change_meetup(%Meetup{}) | ||
render(conn, "new.html", changeset: changeset) | ||
end | ||
|
||
def create(conn, %{"meetup" => meetup_params}) do | ||
case Community.create_meetup(meetup_params) do | ||
{:ok, meetup} -> | ||
conn | ||
|> put_flash(:info, "Meetup created successfully.") | ||
|> redirect(to: Routes.meetup_path(conn, :show, meetup)) | ||
|
||
{:error, %Ecto.Changeset{} = changeset} -> | ||
render(conn, "new.html", changeset: changeset) | ||
end | ||
end | ||
|
||
def show(conn, %{"id" => id}) do | ||
meetup = Community.get_meetup!(id) | ||
render(conn, "show.html", meetup: meetup) | ||
end | ||
|
||
def edit(conn, %{"id" => id}) do | ||
meetup = Community.get_meetup!(id) | ||
changeset = Community.change_meetup(meetup) | ||
render(conn, "edit.html", meetup: meetup, changeset: changeset) | ||
end | ||
|
||
def update(conn, %{"id" => id, "meetup" => meetup_params}) do | ||
meetup = Community.get_meetup!(id) | ||
|
||
case Community.update_meetup(meetup, meetup_params) do | ||
{:ok, meetup} -> | ||
conn | ||
|> put_flash(:info, "Meetup updated successfully.") | ||
|> redirect(to: Routes.meetup_path(conn, :show, meetup)) | ||
|
||
{:error, %Ecto.Changeset{} = changeset} -> | ||
render(conn, "edit.html", meetup: meetup, changeset: changeset) | ||
end | ||
end | ||
|
||
def delete(conn, %{"id" => id}) do | ||
meetup = Community.get_meetup!(id) | ||
{:ok, _meetup} = Community.delete_meetup(meetup) | ||
|
||
conn | ||
|> put_flash(:info, "Meetup deleted successfully.") | ||
|> redirect(to: Routes.meetup_path(conn, :index)) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
app/lib/elixir_lang_tokyo_web/templates/meetup/edit.html.heex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<h1>Edit Meetup</h1> | ||
|
||
<%= render "form.html", Map.put(assigns, :action, Routes.meetup_path(@conn, :update, @meetup)) %> | ||
|
||
<span><%= link "Back", to: Routes.meetup_path(@conn, :index) %></span> |
23 changes: 23 additions & 0 deletions
23
app/lib/elixir_lang_tokyo_web/templates/meetup/form.html.heex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<.form let={f} for={@changeset} action={@action}> | ||
<%= if @changeset.action do %> | ||
<div class="alert alert-danger"> | ||
<p>Oops, something went wrong! Please check the errors below.</p> | ||
</div> | ||
<% end %> | ||
|
||
<%= label f, :name %> | ||
<%= text_input f, :name %> | ||
<%= error_tag f, :name %> | ||
|
||
<%= label f, :site %> | ||
<%= text_input f, :site %> | ||
<%= error_tag f, :site %> | ||
|
||
<%= label f, :hashtag %> | ||
<%= text_input f, :hashtag %> | ||
<%= error_tag f, :hashtag %> | ||
|
||
<div> | ||
<%= submit "Save" %> | ||
</div> | ||
</.form> |
30 changes: 30 additions & 0 deletions
30
app/lib/elixir_lang_tokyo_web/templates/meetup/index.html.heex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<h1>Listing Meetups</h1> | ||
|
||
<table> | ||
<thead> | ||
<tr> | ||
<th>Name</th> | ||
<th>Site</th> | ||
<th>Hashtag</th> | ||
|
||
<th></th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<%= for meetup <- @meetups do %> | ||
<tr> | ||
<td><%= meetup.name %></td> | ||
<td><%= meetup.site %></td> | ||
<td><%= meetup.hashtag %></td> | ||
|
||
<td> | ||
<span><%= link "Show", to: Routes.meetup_path(@conn, :show, meetup) %></span> | ||
<span><%= link "Edit", to: Routes.meetup_path(@conn, :edit, meetup) %></span> | ||
<span><%= link "Delete", to: Routes.meetup_path(@conn, :delete, meetup), method: :delete, data: [confirm: "Are you sure?"] %></span> | ||
</td> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> | ||
|
||
<span><%= link "New Meetup", to: Routes.meetup_path(@conn, :new) %></span> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<h1>New Meetup</h1> | ||
|
||
<%= render "form.html", Map.put(assigns, :action, Routes.meetup_path(@conn, :create)) %> | ||
|
||
<span><%= link "Back", to: Routes.meetup_path(@conn, :index) %></span> |
23 changes: 23 additions & 0 deletions
23
app/lib/elixir_lang_tokyo_web/templates/meetup/show.html.heex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<h1>Show Meetup</h1> | ||
|
||
<ul> | ||
|
||
<li> | ||
<strong>Name:</strong> | ||
<%= @meetup.name %> | ||
</li> | ||
|
||
<li> | ||
<strong>Site:</strong> | ||
<%= @meetup.site %> | ||
</li> | ||
|
||
<li> | ||
<strong>Hashtag:</strong> | ||
<%= @meetup.hashtag %> | ||
</li> | ||
|
||
</ul> | ||
|
||
<span><%= link "Edit", to: Routes.meetup_path(@conn, :edit, @meetup) %></span> | | ||
<span><%= link "Back", to: Routes.meetup_path(@conn, :index) %></span> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
defmodule ElixirLangTokyoWeb.MeetupView do | ||
use ElixirLangTokyoWeb, :view | ||
end |
13 changes: 13 additions & 0 deletions
13
app/priv/repo/migrations/20220303132359_create_meetups.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
defmodule ElixirLangTokyo.Repo.Migrations.CreateMeetups do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:meetups) do | ||
add :name, :string | ||
add :site, :string | ||
add :hashtag, :string | ||
|
||
timestamps() | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
defmodule ElixirLangTokyo.CommunityTest do | ||
use ElixirLangTokyo.DataCase | ||
|
||
alias ElixirLangTokyo.Community | ||
|
||
describe "meetups" do | ||
alias ElixirLangTokyo.Community.Meetup | ||
|
||
import ElixirLangTokyo.CommunityFixtures | ||
|
||
@invalid_attrs %{hashtag: nil, name: nil, site: nil} | ||
|
||
test "list_meetups/0 returns all meetups" do | ||
meetup = meetup_fixture() | ||
assert Community.list_meetups() == [meetup] | ||
end | ||
|
||
test "get_meetup!/1 returns the meetup with given id" do | ||
meetup = meetup_fixture() | ||
assert Community.get_meetup!(meetup.id) == meetup | ||
end | ||
|
||
test "create_meetup/1 with valid data creates a meetup" do | ||
valid_attrs = %{hashtag: "some hashtag", name: "some name", site: "some site"} | ||
|
||
assert {:ok, %Meetup{} = meetup} = Community.create_meetup(valid_attrs) | ||
assert meetup.hashtag == "some hashtag" | ||
assert meetup.name == "some name" | ||
assert meetup.site == "some site" | ||
end | ||
|
||
test "create_meetup/1 with invalid data returns error changeset" do | ||
assert {:error, %Ecto.Changeset{}} = Community.create_meetup(@invalid_attrs) | ||
end | ||
|
||
test "update_meetup/2 with valid data updates the meetup" do | ||
meetup = meetup_fixture() | ||
update_attrs = %{hashtag: "some updated hashtag", name: "some updated name", site: "some updated site"} | ||
|
||
assert {:ok, %Meetup{} = meetup} = Community.update_meetup(meetup, update_attrs) | ||
assert meetup.hashtag == "some updated hashtag" | ||
assert meetup.name == "some updated name" | ||
assert meetup.site == "some updated site" | ||
end | ||
|
||
test "update_meetup/2 with invalid data returns error changeset" do | ||
meetup = meetup_fixture() | ||
assert {:error, %Ecto.Changeset{}} = Community.update_meetup(meetup, @invalid_attrs) | ||
assert meetup == Community.get_meetup!(meetup.id) | ||
end | ||
|
||
test "delete_meetup/1 deletes the meetup" do | ||
meetup = meetup_fixture() | ||
assert {:ok, %Meetup{}} = Community.delete_meetup(meetup) | ||
assert_raise Ecto.NoResultsError, fn -> Community.get_meetup!(meetup.id) end | ||
end | ||
|
||
test "change_meetup/1 returns a meetup changeset" do | ||
meetup = meetup_fixture() | ||
assert %Ecto.Changeset{} = Community.change_meetup(meetup) | ||
end | ||
end | ||
end |
Oops, something went wrong.