Skip to content

Commit

Permalink
add community and meetup
Browse files Browse the repository at this point in the history
  • Loading branch information
ohr486 committed Mar 3, 2022
1 parent 52cdb07 commit 7c9344f
Show file tree
Hide file tree
Showing 15 changed files with 460 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import Config
# Run `mix help test` for more information.
config :elixir_lang_tokyo, ElixirLangTokyo.Repo,
username: "root",
password: "",
hostname: "localhost",
password: "root",
hostname: "mysql",
database: "elixir_lang_tokyo_test#{System.get_env("MIX_TEST_PARTITION")}",
pool: Ecto.Adapters.SQL.Sandbox,
pool_size: 10
Expand Down
104 changes: 104 additions & 0 deletions app/lib/elixir_lang_tokyo/community.ex
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
19 changes: 19 additions & 0 deletions app/lib/elixir_lang_tokyo/community/meetup.ex
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 app/lib/elixir_lang_tokyo_web/controllers/meetup_controller.ex
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
2 changes: 2 additions & 0 deletions app/lib/elixir_lang_tokyo_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ defmodule ElixirLangTokyoWeb.Router do
pipe_through :browser

get "/", PageController, :index

resources "/meetups", MeetupController
end

# Other scopes may use custom stacks.
Expand Down
5 changes: 5 additions & 0 deletions app/lib/elixir_lang_tokyo_web/templates/meetup/edit.html.heex
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 app/lib/elixir_lang_tokyo_web/templates/meetup/form.html.heex
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 app/lib/elixir_lang_tokyo_web/templates/meetup/index.html.heex
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>
5 changes: 5 additions & 0 deletions app/lib/elixir_lang_tokyo_web/templates/meetup/new.html.heex
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 app/lib/elixir_lang_tokyo_web/templates/meetup/show.html.heex
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>
3 changes: 3 additions & 0 deletions app/lib/elixir_lang_tokyo_web/views/meetup_view.ex
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 app/priv/repo/migrations/20220303132359_create_meetups.exs
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
63 changes: 63 additions & 0 deletions app/test/elixir_lang_tokyo/community_test.exs
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
Loading

0 comments on commit 7c9344f

Please sign in to comment.