diff --git a/lib/nudge/accounts/site.ex b/lib/nudge/accounts/site.ex index 85bd2dc..ae495f8 100644 --- a/lib/nudge/accounts/site.ex +++ b/lib/nudge/accounts/site.ex @@ -2,6 +2,7 @@ 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 @@ -9,6 +10,7 @@ defmodule Nudge.Accounts.Site do field :url, :string belongs_to :user, User + has_many :events, Event timestamps(type: :utc_datetime) end diff --git a/lib/nudge/sites.ex b/lib/nudge/sites.ex new file mode 100644 index 0000000..5a10939 --- /dev/null +++ b/lib/nudge/sites.ex @@ -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 + + 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 diff --git a/lib/nudge/sites/event.ex b/lib/nudge/sites/event.ex new file mode 100644 index 0000000..f4f39eb --- /dev/null +++ b/lib/nudge/sites/event.ex @@ -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 + field :title, :string + 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 diff --git a/lib/nudge_web/controllers/event_controller.ex b/lib/nudge_web/controllers/event_controller.ex new file mode 100644 index 0000000..f56f891 --- /dev/null +++ b/lib/nudge_web/controllers/event_controller.ex @@ -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)) + + {: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) + render(conn, "index.html", sites: events) + end +end diff --git a/lib/nudge_web/router.ex b/lib/nudge_web/router.ex index 2e43c43..593ae8b 100644 --- a/lib/nudge_web/router.ex +++ b/lib/nudge_web/router.ex @@ -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 diff --git a/lib/nudge_web/templates/event/index.html.eex b/lib/nudge_web/templates/event/index.html.eex new file mode 100644 index 0000000..77f5d6a --- /dev/null +++ b/lib/nudge_web/templates/event/index.html.eex @@ -0,0 +1,28 @@ +

Your events

+ + + + + + + + + + + + + + + +<%= for event <- @sites do %> + + + + + + + + +<% end %> + +
title body start date end date kind dnd
<%= event.title %><%= event.body %><%= event.start_date %><%= event.end_date %><%= event.kind %><%= event.respect_dnd %>
diff --git a/lib/nudge_web/templates/event/new.html.eex b/lib/nudge_web/templates/event/new.html.eex new file mode 100644 index 0000000..0abbde8 --- /dev/null +++ b/lib/nudge_web/templates/event/new.html.eex @@ -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 %> +
+ <%= submit "Save" %> +
+<% end %> diff --git a/lib/nudge_web/views/event_view.ex b/lib/nudge_web/views/event_view.ex new file mode 100644 index 0000000..7c4f33a --- /dev/null +++ b/lib/nudge_web/views/event_view.ex @@ -0,0 +1,3 @@ +defmodule NudgeWeb.EventView do + use NudgeWeb, :view +end diff --git a/priv/repo/migrations/20210909132511_create_events.exs b/priv/repo/migrations/20210909132511_create_events.exs new file mode 100644 index 0000000..e8b4079 --- /dev/null +++ b/priv/repo/migrations/20210909132511_create_events.exs @@ -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