diff options
author | Logan Hunt <loganhunt@simponic.xyz> | 2022-04-06 12:55:12 -0600 |
---|---|---|
committer | Logan Hunt <loganhunt@simponic.xyz> | 2022-04-06 12:55:12 -0600 |
commit | 4067339e8cf9dbd624a8fa0183d7a29c73b2e762 (patch) | |
tree | 4f995e706c10bf5b9a58c078cfb843c76e31c8a0 /lib | |
parent | 66d871e5461814dad58872eb832a58f2c3c5111b (diff) | |
download | aggiedit-4067339e8cf9dbd624a8fa0183d7a29c73b2e762.tar.gz aggiedit-4067339e8cf9dbd624a8fa0183d7a29c73b2e762.zip |
Models for upload and post; generated liveview controller for posts
Diffstat (limited to 'lib')
-rw-r--r-- | lib/aggiedit/rooms.ex | 96 | ||||
-rw-r--r-- | lib/aggiedit/rooms/post.ex | 21 | ||||
-rw-r--r-- | lib/aggiedit/uploads.ex | 104 | ||||
-rw-r--r-- | lib/aggiedit/uploads/upload.ex | 21 | ||||
-rw-r--r-- | lib/aggiedit_web.ex | 1 | ||||
-rw-r--r-- | lib/aggiedit_web/live/live_helpers.ex | 60 | ||||
-rw-r--r-- | lib/aggiedit_web/live/post_live/form_component.ex | 55 | ||||
-rw-r--r-- | lib/aggiedit_web/live/post_live/form_component.html.heex | 24 | ||||
-rw-r--r-- | lib/aggiedit_web/live/post_live/index.ex | 46 | ||||
-rw-r--r-- | lib/aggiedit_web/live/post_live/index.html.heex | 41 | ||||
-rw-r--r-- | lib/aggiedit_web/live/post_live/show.ex | 21 | ||||
-rw-r--r-- | lib/aggiedit_web/live/post_live/show.html.heex | 31 | ||||
-rw-r--r-- | lib/aggiedit_web/router.ex | 7 |
13 files changed, 528 insertions, 0 deletions
diff --git a/lib/aggiedit/rooms.ex b/lib/aggiedit/rooms.ex index eac2b65..fb10c4b 100644 --- a/lib/aggiedit/rooms.ex +++ b/lib/aggiedit/rooms.ex @@ -108,4 +108,100 @@ defmodule Aggiedit.Rooms do nil -> create_room(%{domain: domain}) end end + + alias Aggiedit.Rooms.Post + + @doc """ + Returns the list of posts. + + ## Examples + + iex> list_posts() + [%Post{}, ...] + + """ + def list_posts do + Repo.all(Post) + end + + @doc """ + Gets a single post. + + Raises `Ecto.NoResultsError` if the Post does not exist. + + ## Examples + + iex> get_post!(123) + %Post{} + + iex> get_post!(456) + ** (Ecto.NoResultsError) + + """ + def get_post!(id), do: Repo.get!(Post, id) + + @doc """ + Creates a post. + + ## Examples + + iex> create_post(%{field: value}) + {:ok, %Post{}} + + iex> create_post(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def create_post(attrs \\ %{}) do + %Post{} + |> Post.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a post. + + ## Examples + + iex> update_post(post, %{field: new_value}) + {:ok, %Post{}} + + iex> update_post(post, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def update_post(%Post{} = post, attrs) do + post + |> Post.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a post. + + ## Examples + + iex> delete_post(post) + {:ok, %Post{}} + + iex> delete_post(post) + {:error, %Ecto.Changeset{}} + + """ + def delete_post(%Post{} = post) do + Repo.delete(post) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking post changes. + + ## Examples + + iex> change_post(post) + %Ecto.Changeset{data: %Post{}} + + """ + def change_post(%Post{} = post, attrs \\ %{}) do + Post.changeset(post, attrs) + end end diff --git a/lib/aggiedit/rooms/post.ex b/lib/aggiedit/rooms/post.ex new file mode 100644 index 0000000..c6ca24a --- /dev/null +++ b/lib/aggiedit/rooms/post.ex @@ -0,0 +1,21 @@ +defmodule Aggiedit.Rooms.Post do + use Ecto.Schema + import Ecto.Changeset + + schema "posts" do + field :body, :string + field :title, :string + field :user_id, :id + field :upload_id, :id + field :room_id, :id + + timestamps() + end + + @doc false + def changeset(post, attrs) do + post + |> cast(attrs, [:title, :body]) + |> validate_required([:title, :body]) + end +end diff --git a/lib/aggiedit/uploads.ex b/lib/aggiedit/uploads.ex new file mode 100644 index 0000000..2dcdcc3 --- /dev/null +++ b/lib/aggiedit/uploads.ex @@ -0,0 +1,104 @@ +defmodule Aggiedit.Uploads do + @moduledoc """ + The Uploads context. + """ + + import Ecto.Query, warn: false + alias Aggiedit.Repo + + alias Aggiedit.Uploads.Upload + + @doc """ + Returns the list of uploads. + + ## Examples + + iex> list_uploads() + [%Upload{}, ...] + + """ + def list_uploads do + Repo.all(Upload) + end + + @doc """ + Gets a single upload. + + Raises `Ecto.NoResultsError` if the Upload does not exist. + + ## Examples + + iex> get_upload!(123) + %Upload{} + + iex> get_upload!(456) + ** (Ecto.NoResultsError) + + """ + def get_upload!(id), do: Repo.get!(Upload, id) + + @doc """ + Creates a upload. + + ## Examples + + iex> create_upload(%{field: value}) + {:ok, %Upload{}} + + iex> create_upload(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def create_upload(attrs \\ %{}) do + %Upload{} + |> Upload.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a upload. + + ## Examples + + iex> update_upload(upload, %{field: new_value}) + {:ok, %Upload{}} + + iex> update_upload(upload, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def update_upload(%Upload{} = upload, attrs) do + upload + |> Upload.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a upload. + + ## Examples + + iex> delete_upload(upload) + {:ok, %Upload{}} + + iex> delete_upload(upload) + {:error, %Ecto.Changeset{}} + + """ + def delete_upload(%Upload{} = upload) do + Repo.delete(upload) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking upload changes. + + ## Examples + + iex> change_upload(upload) + %Ecto.Changeset{data: %Upload{}} + + """ + def change_upload(%Upload{} = upload, attrs \\ %{}) do + Upload.changeset(upload, attrs) + end +end diff --git a/lib/aggiedit/uploads/upload.ex b/lib/aggiedit/uploads/upload.ex new file mode 100644 index 0000000..f5f028d --- /dev/null +++ b/lib/aggiedit/uploads/upload.ex @@ -0,0 +1,21 @@ +defmodule Aggiedit.Uploads.Upload do + use Ecto.Schema + import Ecto.Changeset + + schema "uploads" do + field :file, :string + field :mime, :string + field :size, :integer + + belongs_to :user, Aggiedit.Accounts.User + + timestamps() + end + + @doc false + def changeset(upload, attrs) do + upload + |> cast(attrs, [:file, :mime, :size]) + |> validate_required([:file, :mime, :size]) + end +end diff --git a/lib/aggiedit_web.ex b/lib/aggiedit_web.ex index 0b7717c..c6b6841 100644 --- a/lib/aggiedit_web.ex +++ b/lib/aggiedit_web.ex @@ -91,6 +91,7 @@ defmodule AggieditWeb do # Import LiveView and .heex helpers (live_render, live_patch, <.form>, etc) import Phoenix.LiveView.Helpers + import AggieditWeb.LiveHelpers # Import basic rendering functionality (render, render_layout, etc) import Phoenix.View diff --git a/lib/aggiedit_web/live/live_helpers.ex b/lib/aggiedit_web/live/live_helpers.ex new file mode 100644 index 0000000..aa6b28a --- /dev/null +++ b/lib/aggiedit_web/live/live_helpers.ex @@ -0,0 +1,60 @@ +defmodule AggieditWeb.LiveHelpers do + import Phoenix.LiveView + import Phoenix.LiveView.Helpers + + alias Phoenix.LiveView.JS + + @doc """ + Renders a live component inside a modal. + + The rendered modal receives a `:return_to` option to properly update + the URL when the modal is closed. + + ## Examples + + <.modal return_to={Routes.post_index_path(@socket, :index)}> + <.live_component + module={AggieditWeb.PostLive.FormComponent} + id={@post.id || :new} + title={@page_title} + action={@live_action} + return_to={Routes.post_index_path(@socket, :index)} + post: @post + /> + </.modal> + """ + def modal(assigns) do + assigns = assign_new(assigns, :return_to, fn -> nil end) + + ~H""" + <div id="modal" class="phx-modal fade-in" phx-remove={hide_modal()}> + <div + id="modal-content" + class="phx-modal-content fade-in-scale" + phx-click-away={JS.dispatch("click", to: "#close")} + phx-window-keydown={JS.dispatch("click", to: "#close")} + phx-key="escape" + > + <%= if @return_to do %> + <%= live_patch "✖", + to: @return_to, + id: "close", + class: "phx-modal-close", + phx_click: hide_modal() + %> + <% else %> + <a id="close" href="#" class="phx-modal-close" phx-click={hide_modal()}>✖</a> + <% end %> + + <%= render_slot(@inner_block) %> + </div> + </div> + """ + end + + defp hide_modal(js \\ %JS{}) do + js + |> JS.hide(to: "#modal", transition: "fade-out") + |> JS.hide(to: "#modal-content", transition: "fade-out-scale") + end +end diff --git a/lib/aggiedit_web/live/post_live/form_component.ex b/lib/aggiedit_web/live/post_live/form_component.ex new file mode 100644 index 0000000..11ad4a1 --- /dev/null +++ b/lib/aggiedit_web/live/post_live/form_component.ex @@ -0,0 +1,55 @@ +defmodule AggieditWeb.PostLive.FormComponent do + use AggieditWeb, :live_component + + alias Aggiedit.Rooms + + @impl true + def update(%{post: post} = assigns, socket) do + changeset = Rooms.change_post(post) + + {:ok, + socket + |> assign(assigns) + |> assign(:changeset, changeset)} + end + + @impl true + def handle_event("validate", %{"post" => post_params}, socket) do + changeset = + socket.assigns.post + |> Rooms.change_post(post_params) + |> Map.put(:action, :validate) + + {:noreply, assign(socket, :changeset, changeset)} + end + + def handle_event("save", %{"post" => post_params}, socket) do + save_post(socket, socket.assigns.action, post_params) + end + + defp save_post(socket, :edit, post_params) do + case Rooms.update_post(socket.assigns.post, post_params) do + {:ok, _post} -> + {:noreply, + socket + |> put_flash(:info, "Post updated successfully") + |> push_redirect(to: socket.assigns.return_to)} + + {:error, %Ecto.Changeset{} = changeset} -> + {:noreply, assign(socket, :changeset, changeset)} + end + end + + defp save_post(socket, :new, post_params) do + case Rooms.create_post(post_params) do + {:ok, _post} -> + {:noreply, + socket + |> put_flash(:info, "Post created successfully") + |> push_redirect(to: socket.assigns.return_to)} + + {:error, %Ecto.Changeset{} = changeset} -> + {:noreply, assign(socket, changeset: changeset)} + end + end +end diff --git a/lib/aggiedit_web/live/post_live/form_component.html.heex b/lib/aggiedit_web/live/post_live/form_component.html.heex new file mode 100644 index 0000000..682ec78 --- /dev/null +++ b/lib/aggiedit_web/live/post_live/form_component.html.heex @@ -0,0 +1,24 @@ +<div> + <h2><%= @title %></h2> + + <.form + let={f} + for={@changeset} + id="post-form" + phx-target={@myself} + phx-change="validate" + phx-submit="save"> + + <%= label f, :title %> + <%= textarea f, :title %> + <%= error_tag f, :title %> + + <%= label f, :body %> + <%= textarea f, :body %> + <%= error_tag f, :body %> + + <div> + <%= submit "Save", phx_disable_with: "Saving..." %> + </div> + </.form> +</div> diff --git a/lib/aggiedit_web/live/post_live/index.ex b/lib/aggiedit_web/live/post_live/index.ex new file mode 100644 index 0000000..0b12ed0 --- /dev/null +++ b/lib/aggiedit_web/live/post_live/index.ex @@ -0,0 +1,46 @@ +defmodule AggieditWeb.PostLive.Index do + use AggieditWeb, :live_view + + alias Aggiedit.Rooms + alias Aggiedit.Rooms.Post + + @impl true + def mount(_params, _session, socket) do + {:ok, assign(socket, :posts, list_posts())} + end + + @impl true + def handle_params(params, _url, socket) do + {:noreply, apply_action(socket, socket.assigns.live_action, params)} + end + + defp apply_action(socket, :edit, %{"id" => id}) do + socket + |> assign(:page_title, "Edit Post") + |> assign(:post, Rooms.get_post!(id)) + end + + defp apply_action(socket, :new, _params) do + socket + |> assign(:page_title, "New Post") + |> assign(:post, %Post{}) + end + + defp apply_action(socket, :index, _params) do + socket + |> assign(:page_title, "Listing Posts") + |> assign(:post, nil) + end + + @impl true + def handle_event("delete", %{"id" => id}, socket) do + post = Rooms.get_post!(id) + {:ok, _} = Rooms.delete_post(post) + + {:noreply, assign(socket, :posts, list_posts())} + end + + defp list_posts do + Rooms.list_posts() + end +end diff --git a/lib/aggiedit_web/live/post_live/index.html.heex b/lib/aggiedit_web/live/post_live/index.html.heex new file mode 100644 index 0000000..4297d5a --- /dev/null +++ b/lib/aggiedit_web/live/post_live/index.html.heex @@ -0,0 +1,41 @@ +<h1>Listing Posts</h1> + +<%= if @live_action in [:new, :edit] do %> + <.modal return_to={Routes.post_index_path(@socket, :index)}> + <.live_component + module={AggieditWeb.PostLive.FormComponent} + id={@post.id || :new} + title={@page_title} + action={@live_action} + post={@post} + return_to={Routes.post_index_path(@socket, :index)} + /> + </.modal> +<% end %> + +<table> + <thead> + <tr> + <th>Title</th> + <th>Body</th> + + <th></th> + </tr> + </thead> + <tbody id="posts"> + <%= for post <- @posts do %> + <tr id={"post-#{post.id}"}> + <td><%= post.title %></td> + <td><%= post.body %></td> + + <td> + <span><%= live_redirect "Show", to: Routes.post_show_path(@socket, :show, post) %></span> + <span><%= live_patch "Edit", to: Routes.post_index_path(@socket, :edit, post) %></span> + <span><%= link "Delete", to: "#", phx_click: "delete", phx_value_id: post.id, data: [confirm: "Are you sure?"] %></span> + </td> + </tr> + <% end %> + </tbody> +</table> + +<span><%= live_patch "New Post", to: Routes.post_index_path(@socket, :new) %></span> diff --git a/lib/aggiedit_web/live/post_live/show.ex b/lib/aggiedit_web/live/post_live/show.ex new file mode 100644 index 0000000..2416156 --- /dev/null +++ b/lib/aggiedit_web/live/post_live/show.ex @@ -0,0 +1,21 @@ +defmodule AggieditWeb.PostLive.Show do + use AggieditWeb, :live_view + + alias Aggiedit.Rooms + + @impl true + def mount(_params, _session, socket) do + {:ok, socket} + end + + @impl true + def handle_params(%{"id" => id}, _, socket) do + {:noreply, + socket + |> assign(:page_title, page_title(socket.assigns.live_action)) + |> assign(:post, Rooms.get_post!(id))} + end + + defp page_title(:show), do: "Show Post" + defp page_title(:edit), do: "Edit Post" +end diff --git a/lib/aggiedit_web/live/post_live/show.html.heex b/lib/aggiedit_web/live/post_live/show.html.heex new file mode 100644 index 0000000..e6eaebe --- /dev/null +++ b/lib/aggiedit_web/live/post_live/show.html.heex @@ -0,0 +1,31 @@ +<h1>Show Post</h1> + +<%= if @live_action in [:edit] do %> + <.modal return_to={Routes.post_show_path(@socket, :show, @post)}> + <.live_component + module={AggieditWeb.PostLive.FormComponent} + id={@post.id} + title={@page_title} + action={@live_action} + post={@post} + return_to={Routes.post_show_path(@socket, :show, @post)} + /> + </.modal> +<% end %> + +<ul> + + <li> + <strong>Title:</strong> + <%= @post.title %> + </li> + + <li> + <strong>Body:</strong> + <%= @post.body %> + </li> + +</ul> + +<span><%= live_patch "Edit", to: Routes.post_show_path(@socket, :edit, @post), class: "button" %></span> | +<span><%= live_redirect "Back", to: Routes.post_index_path(@socket, :index) %></span> diff --git a/lib/aggiedit_web/router.ex b/lib/aggiedit_web/router.ex index e400391..dec730a 100644 --- a/lib/aggiedit_web/router.ex +++ b/lib/aggiedit_web/router.ex @@ -21,6 +21,13 @@ defmodule AggieditWeb.Router do pipe_through :browser get "/", PageController, :index + + live "/posts", PostLive.Index, :index + live "/posts/new", PostLive.Index, :new + live "/posts/:id/edit", PostLive.Index, :edit + + live "/posts/:id", PostLive.Show, :show + live "/posts/:id/show/edit", PostLive.Show, :edit end # Other scopes may use custom stacks. |