Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Context with schema for Events CRUD #27

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/nudge/accounts/site.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ defmodule Nudge.Accounts.Site do
use Ecto.Schema
import Ecto.Changeset
alias Nudge.Accounts.User
alias Nudge.Sites.Event

schema "sites" do
field :active, :boolean, default: false
field :tz, :string, default: "UTC"
field :url, :string

belongs_to :user, User
has_many :events, Event
sreecodeslayer marked this conversation as resolved.
Show resolved Hide resolved

timestamps(type: :utc_datetime)
end
Expand Down
113 changes: 113 additions & 0 deletions lib/nudge/sites.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
defmodule Nudge.Sites do
@moduledoc """
The Sites context.
"""

import Ecto.Query, warn: false
alias Nudge.Repo

alias Nudge.Sites.Event

@doc """
Returns the list of events.

## Examples

iex> list_events()
[%Event{}, ...]

"""
def list_events do
Repo.all(Event)
end

def list_site_events(site_id) do
query =
from site in Nudge.Accounts.Site,
where: site.id == ^site_id,
order_by: site.inserted_at
Comment on lines +25 to +28
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this the right query for filtering events?


Repo.all(query)
end

@doc """
Gets a single event.

Raises `Ecto.NoResultsError` if the Event does not exist.

## Examples

iex> get_event!(123)
%Event{}

iex> get_event!(456)
** (Ecto.NoResultsError)

"""
def get_event!(id), do: Repo.get!(Event, id)

@doc """
Creates a event.

## Examples

iex> create_event(%{field: value})
{:ok, %Event{}}

iex> create_event(%{field: bad_value})
{:error, %Ecto.Changeset{}}

"""
def create_event(attrs \\ %{}) do
%Event{}
|> Event.changeset(attrs)
|> Repo.insert()
end

@doc """
Updates a event.

## Examples

iex> update_event(event, %{field: new_value})
{:ok, %Event{}}

iex> update_event(event, %{field: bad_value})
{:error, %Ecto.Changeset{}}

"""
def update_event(%Event{} = event, attrs) do
event
|> Event.changeset(attrs)
|> Repo.update()
end

@doc """
Deletes a event.

## Examples

iex> delete_event(event)
{:ok, %Event{}}

iex> delete_event(event)
{:error, %Ecto.Changeset{}}

"""
def delete_event(%Event{} = event) do
Repo.delete(event)
end

@doc """
Returns an `%Ecto.Changeset{}` for tracking event changes.

## Examples

iex> change_event(event)
%Ecto.Changeset{data: %Event{}}

"""
def change_event(%Event{} = event, attrs \\ %{}) do
Event.changeset(event, attrs)
end
end
25 changes: 25 additions & 0 deletions lib/nudge/sites/event.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
defmodule Nudge.Sites.Event do
use Ecto.Schema
import Ecto.Changeset
alias Nudge.Accounts.Site

schema "events" do
field :body, :string
field :end_date, :string
field :start_date, :string
Comment on lines +8 to +9
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these two should be utc datetime type, not strings

field :title, :string
sreecodeslayer marked this conversation as resolved.
Show resolved Hide resolved
field :kind, :string, default: "banner"
field :respect_dnd, :boolean, default: false

belongs_to :site, Site

timestamps()
end

@doc false
def changeset(event, attrs) do
event
|> cast(attrs, [:title, :body, :start_date, :end_date, :kind, :respect_dnd, :site_id])
|> validate_required([:title, :body, :start_date, :end_date, :kind, :respect_dnd, :site_id])
end
end
50 changes: 50 additions & 0 deletions lib/nudge_web/controllers/event_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
defmodule NudgeWeb.EventController do
use NudgeWeb, :controller
alias Nudge.Sites
alias Nudge.Sites.Event

def new(conn, _params) do
changeset = Sites.change_event(%Event{})
render(conn, "new.html", changeset: changeset)
end

def create(conn, %{
"event" => %{
"title" => title,
"body" => body,
"start_date" => start_date,
"end_date" => end_date,
"kind" => kind,
"respect_dnd" => dnd
}
}) do
IO.inspect(conn.assigns)

case Sites.create_event(%{
"title" => title,
"body" => body,
"start_date" => start_date,
"end_date" => end_date,
"kind" => kind,
"respect_dnd" => dnd,
"site_id" => 7
}) do
{:ok, event} ->
conn
|> put_flash(:info, "Site created successfully.")
|> redirect(to: Routes.site_event_path(conn, :index, :id))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redirect(to: Routes.site_event_path(conn, :index, :id))

Will this line work? Shouldn't we be passing the id of the site?


{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "new.html", changeset: changeset)
end
end

def index(conn, %{"site_id" => id}) do
id = 7
site_id = Nudge.Accounts.get_site!(id)

IO.inspect(site_id)
events = Sites.list_site_events(site_id)
Comment on lines +44 to +47
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does get_site!/1 return just id or the whole site struct with data?

This is why we end up having type casting error in the query. The query expects and id, but you are passing the whole result to the list_site_events/1

render(conn, "index.html", sites: events)
end
end
6 changes: 5 additions & 1 deletion lib/nudge_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ defmodule NudgeWeb.Router do

scope "/", NudgeWeb do
pipe_through [:browser, :with_current_user]
resources "/sites", SiteController, only: [:new, :show, :create, :index]

resources "/sites", SiteController, only: [:new, :show, :create, :index] do
resources "events", EventController, only: [:new, :create, :index]
end

post "/sites/:id/toggle", SiteController, :toggle
get "/logout", SessionController, :logout
get "/welcome", PageController, :welcome
Expand Down
28 changes: 28 additions & 0 deletions lib/nudge_web/templates/event/index.html.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<h1>Your events</h1>

<table>
<thead>
<tr>
<th> title</th>
<th> body</th>
<th> start date</th>
<th> end date</th>
<th> kind</th>
<th> dnd</th>

<th></th>
</tr>
</thead>
<tbody>
<%= for event <- @sites do %>
<tr>
<td><%= event.title %></td>
<td><%= event.body %></td>
<td><%= event.start_date %></td>
<td><%= event.end_date %></td>
<td><%= event.kind %></td>
<td><%= event.respect_dnd %></td>
</tr>
<% end %>
</tbody>
</table>
30 changes: 30 additions & 0 deletions lib/nudge_web/templates/event/new.html.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<%= form_for @changeset, Routes.site_event_path(@conn, :create, :id), fn f -> %>


<%= label f, :title %>
<%= text_input f, :title %>
<%= error_tag f, :title %>

<%= label f, :body %>
<%= text_input f, :body %>
<%= error_tag f, :body %>

<%= label f, :start_date %>
<%= text_input f, :start_date %>
<%= error_tag f, :start_date %>

<%= label f, :end_date %>
<%= text_input f, :end_date %>
<%= error_tag f, :end_date %>

<%= label f, :kind %>
<%= text_input f, :kind %>
<%= error_tag f, :kind %>

<%= label f, :respect_dnd %>
<%= checkbox f, :respect_dnd %>
<%= error_tag f, :respect_dnd %>
<div>
<%= submit "Save" %>
</div>
<% end %>
3 changes: 3 additions & 0 deletions lib/nudge_web/views/event_view.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
defmodule NudgeWeb.EventView do
use NudgeWeb, :view
end
17 changes: 17 additions & 0 deletions priv/repo/migrations/20210909132511_create_events.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule Nudge.Repo.Migrations.CreateEvents do
use Ecto.Migration

def change do
create table(:events) do
add :title, :string
add :body, :string
add :start_date, :string
add :end_date, :string
add :kind, :string
add :respect_dnd, :boolean, default: false, null: false
add :site_id, references(:sites, on_delete: :nothing)

timestamps()
end
end
end