Add uploads; next up - user authentication on post
This commit is contained in:
parent
61c2c9370a
commit
ed22eb20f9
@ -56,7 +56,7 @@ config :aggiedit, AggieditWeb.Endpoint,
|
|||||||
config :aggiedit, AggieditWeb.Endpoint,
|
config :aggiedit, AggieditWeb.Endpoint,
|
||||||
live_reload: [
|
live_reload: [
|
||||||
patterns: [
|
patterns: [
|
||||||
~r"priv/static/.*(js|css|png|jpeg|jpg|gif|svg)$",
|
~r"priv/static/[^uploads].*(js|css|png|jpeg|jpg|gif|svg)$",
|
||||||
~r"priv/gettext/.*(po)$",
|
~r"priv/gettext/.*(po)$",
|
||||||
~r"lib/aggiedit_web/(live|views)/.*(ex)$",
|
~r"lib/aggiedit_web/(live|views)/.*(ex)$",
|
||||||
~r"lib/aggiedit_web/templates/.*(eex)$"
|
~r"lib/aggiedit_web/templates/.*(eex)$"
|
||||||
|
@ -140,42 +140,25 @@ defmodule Aggiedit.Rooms do
|
|||||||
"""
|
"""
|
||||||
def get_post!(id), do: Repo.get!(Post, id)
|
def get_post!(id), do: Repo.get!(Post, id)
|
||||||
|
|
||||||
@doc """
|
def create_post(attrs, after_save \\ &{:ok, &1}) do
|
||||||
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{}
|
||||||
|> Post.changeset(attrs)
|
|> Post.changeset(attrs)
|
||||||
|> Repo.insert()
|
|> Repo.insert()
|
||||||
|
|> post_saved(after_save)
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
def update_post(%Post{} = post, attrs, after_save \\ &{:ok, &1}) do
|
||||||
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
|
||||||
|> Post.changeset(attrs)
|
|> Post.changeset(attrs)
|
||||||
|> Repo.update()
|
|> Repo.update()
|
||||||
|
|> post_saved(after_save)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp post_saved({:ok, post}, func) do
|
||||||
|
{:ok, _post} = func.(post)
|
||||||
|
end
|
||||||
|
defp post_saved(error, _fun), do: error
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Deletes a post.
|
Deletes a post.
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
defmodule Aggiedit.Rooms.Post do
|
defmodule Aggiedit.Rooms.Post do
|
||||||
use Ecto.Schema
|
use Ecto.Schema
|
||||||
import Ecto.Changeset
|
import Ecto.Changeset
|
||||||
|
alias Aggiedit.Repo
|
||||||
|
|
||||||
schema "posts" do
|
schema "posts" do
|
||||||
field :body, :string
|
field :body, :string
|
||||||
|
@ -18,4 +18,9 @@ defmodule Aggiedit.Uploads.Upload do
|
|||||||
|> cast(attrs, [:file, :mime, :size])
|
|> cast(attrs, [:file, :mime, :size])
|
||||||
|> validate_required([:file, :mime, :size])
|
|> validate_required([:file, :mime, :size])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def change_user(upload, user) do
|
||||||
|
change(upload)
|
||||||
|
|> put_assoc(:user, user)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -20,7 +20,7 @@ defmodule AggieditWeb.Endpoint do
|
|||||||
at: "/",
|
at: "/",
|
||||||
from: :aggiedit,
|
from: :aggiedit,
|
||||||
gzip: false,
|
gzip: false,
|
||||||
only: ~w(assets fonts images favicon.ico robots.txt)
|
only: ~w(assets fonts images favicon.ico robots.txt uploads)
|
||||||
|
|
||||||
# Code reloading can be explicitly enabled under the
|
# Code reloading can be explicitly enabled under the
|
||||||
# :code_reloader configuration of your endpoint.
|
# :code_reloader configuration of your endpoint.
|
||||||
|
@ -2,6 +2,9 @@ defmodule AggieditWeb.PostLive.FormComponent do
|
|||||||
use AggieditWeb, :live_component
|
use AggieditWeb, :live_component
|
||||||
|
|
||||||
alias Aggiedit.Rooms
|
alias Aggiedit.Rooms
|
||||||
|
alias Aggiedit.Rooms.Post
|
||||||
|
alias Aggiedit.Uploads.Upload
|
||||||
|
alias Aggiedit.Repo
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def update(%{post: post} = assigns, socket) do
|
def update(%{post: post} = assigns, socket) do
|
||||||
@ -10,7 +13,10 @@ defmodule AggieditWeb.PostLive.FormComponent do
|
|||||||
{:ok,
|
{:ok,
|
||||||
socket
|
socket
|
||||||
|> assign(assigns)
|
|> assign(assigns)
|
||||||
|> assign(:changeset, changeset)}
|
|> assign(:changeset, changeset)
|
||||||
|
|> assign(:uploaded_files, [])
|
||||||
|
|> allow_upload(:upload, accept: ~w(.jpg .jpeg .png .gif), max_entries: 1)
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
@ -24,11 +30,35 @@ defmodule AggieditWeb.PostLive.FormComponent do
|
|||||||
end
|
end
|
||||||
|
|
||||||
def handle_event("save", %{"post" => post_params}, socket) do
|
def handle_event("save", %{"post" => post_params}, socket) do
|
||||||
save_post(socket, socket.assigns.action, post_params)
|
socket
|
||||||
|
|> save_post(socket.assigns.action, post_params)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp save_upload(socket, %Post{} = post) do
|
||||||
|
consume_uploaded_entries(socket, :upload, fn data, upload ->
|
||||||
|
[extension | _] = MIME.extensions(upload.client_type)
|
||||||
|
filename = "#{upload.uuid}-#{extension}"
|
||||||
|
upload = %Upload{
|
||||||
|
file: filename,
|
||||||
|
size: upload.client_size,
|
||||||
|
mime: upload.client_type
|
||||||
|
}
|
||||||
|
|
||||||
|
dest = Path.join("priv/static/uploads", filename)
|
||||||
|
File.cp!(data.path, dest)
|
||||||
|
|
||||||
|
Repo.preload(post, :upload)
|
||||||
|
|> Post.change_upload(upload)
|
||||||
|
|> Repo.update
|
||||||
|
IO.puts(inspect(upload))
|
||||||
|
|
||||||
|
{:ok, upload}
|
||||||
|
end)
|
||||||
|
{:ok, post}
|
||||||
end
|
end
|
||||||
|
|
||||||
defp save_post(socket, :edit, post_params) do
|
defp save_post(socket, :edit, post_params) do
|
||||||
case Rooms.update_post(socket.assigns.post, post_params) do
|
case Rooms.update_post(socket.assigns.post, post_params, &save_upload(socket, &1)) do
|
||||||
{:ok, _post} ->
|
{:ok, _post} ->
|
||||||
{:noreply,
|
{:noreply,
|
||||||
socket
|
socket
|
||||||
@ -41,7 +71,7 @@ defmodule AggieditWeb.PostLive.FormComponent do
|
|||||||
end
|
end
|
||||||
|
|
||||||
defp save_post(socket, :new, post_params) do
|
defp save_post(socket, :new, post_params) do
|
||||||
case Rooms.create_post(post_params) do
|
case Rooms.create_post(post_params, &save_upload(socket, &1)) do
|
||||||
{:ok, _post} ->
|
{:ok, _post} ->
|
||||||
{:noreply,
|
{:noreply,
|
||||||
socket
|
socket
|
||||||
|
@ -16,6 +16,16 @@
|
|||||||
<%= label f, :body %>
|
<%= label f, :body %>
|
||||||
<%= textarea f, :body %>
|
<%= textarea f, :body %>
|
||||||
<%= error_tag f, :body %>
|
<%= error_tag f, :body %>
|
||||||
|
|
||||||
|
<%= live_file_input @uploads.upload %>
|
||||||
|
|
||||||
|
<%= for upload <- @uploads.upload.entries do %>
|
||||||
|
<div class="row">
|
||||||
|
<div class="column">
|
||||||
|
<%= live_img_preview upload, height: 80 %>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<% end %>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<%= submit "Save", phx_disable_with: "Saving..." %>
|
<%= submit "Save", phx_disable_with: "Saving..." %>
|
||||||
|
@ -3,6 +3,7 @@ defmodule AggieditWeb.PostLive.Index do
|
|||||||
|
|
||||||
alias Aggiedit.Rooms
|
alias Aggiedit.Rooms
|
||||||
alias Aggiedit.Rooms.Post
|
alias Aggiedit.Rooms.Post
|
||||||
|
alias Aggiedit.Repo
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def mount(_params, session, socket) do
|
def mount(_params, session, socket) do
|
||||||
@ -25,9 +26,10 @@ defmodule AggieditWeb.PostLive.Index do
|
|||||||
end
|
end
|
||||||
|
|
||||||
defp apply_action(socket, :new, _params) do
|
defp apply_action(socket, :new, _params) do
|
||||||
|
post = %Post{user_id: socket.assigns[:current_user].id}
|
||||||
socket
|
socket
|
||||||
|> assign(:page_title, "New Post")
|
|> assign(:page_title, "New Post")
|
||||||
|> assign(:post, %Post{})
|
|> assign(:post, post)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp apply_action(socket, :index, _params) do
|
defp apply_action(socket, :index, _params) do
|
||||||
|
@ -6,11 +6,10 @@ defmodule Aggiedit.Repo.Migrations.CreateUploads do
|
|||||||
add :file, :text
|
add :file, :text
|
||||||
add :mime, :text
|
add :mime, :text
|
||||||
add :size, :integer
|
add :size, :integer
|
||||||
add :user_id, references(:users, on_delete: :nothing)
|
|
||||||
|
add :user_id, references(:users, on_delete: :delete_all)
|
||||||
|
|
||||||
timestamps()
|
timestamps()
|
||||||
end
|
end
|
||||||
|
|
||||||
create index(:uploads, [:user_id])
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user