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,
|
||||
live_reload: [
|
||||
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"lib/aggiedit_web/(live|views)/.*(ex)$",
|
||||
~r"lib/aggiedit_web/templates/.*(eex)$"
|
||||
|
@ -140,42 +140,25 @@ defmodule Aggiedit.Rooms do
|
||||
"""
|
||||
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
|
||||
def create_post(attrs, after_save \\ &{:ok, &1}) do
|
||||
%Post{}
|
||||
|> Post.changeset(attrs)
|
||||
|> Repo.insert()
|
||||
|> post_saved(after_save)
|
||||
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
|
||||
def update_post(%Post{} = post, attrs, after_save \\ &{:ok, &1}) do
|
||||
post
|
||||
|> Post.changeset(attrs)
|
||||
|> Repo.update()
|
||||
|> post_saved(after_save)
|
||||
end
|
||||
|
||||
defp post_saved({:ok, post}, func) do
|
||||
{:ok, _post} = func.(post)
|
||||
end
|
||||
defp post_saved(error, _fun), do: error
|
||||
|
||||
@doc """
|
||||
Deletes a post.
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
defmodule Aggiedit.Rooms.Post do
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
alias Aggiedit.Repo
|
||||
|
||||
schema "posts" do
|
||||
field :body, :string
|
||||
|
@ -18,4 +18,9 @@ defmodule Aggiedit.Uploads.Upload do
|
||||
|> cast(attrs, [:file, :mime, :size])
|
||||
|> validate_required([:file, :mime, :size])
|
||||
end
|
||||
|
||||
def change_user(upload, user) do
|
||||
change(upload)
|
||||
|> put_assoc(:user, user)
|
||||
end
|
||||
end
|
||||
|
@ -20,7 +20,7 @@ defmodule AggieditWeb.Endpoint do
|
||||
at: "/",
|
||||
from: :aggiedit,
|
||||
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_reloader configuration of your endpoint.
|
||||
|
@ -2,6 +2,9 @@ defmodule AggieditWeb.PostLive.FormComponent do
|
||||
use AggieditWeb, :live_component
|
||||
|
||||
alias Aggiedit.Rooms
|
||||
alias Aggiedit.Rooms.Post
|
||||
alias Aggiedit.Uploads.Upload
|
||||
alias Aggiedit.Repo
|
||||
|
||||
@impl true
|
||||
def update(%{post: post} = assigns, socket) do
|
||||
@ -10,7 +13,10 @@ defmodule AggieditWeb.PostLive.FormComponent do
|
||||
{:ok,
|
||||
socket
|
||||
|> assign(assigns)
|
||||
|> assign(:changeset, changeset)}
|
||||
|> assign(:changeset, changeset)
|
||||
|> assign(:uploaded_files, [])
|
||||
|> allow_upload(:upload, accept: ~w(.jpg .jpeg .png .gif), max_entries: 1)
|
||||
}
|
||||
end
|
||||
|
||||
@impl true
|
||||
@ -24,11 +30,35 @@ defmodule AggieditWeb.PostLive.FormComponent do
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
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} ->
|
||||
{:noreply,
|
||||
socket
|
||||
@ -41,7 +71,7 @@ defmodule AggieditWeb.PostLive.FormComponent do
|
||||
end
|
||||
|
||||
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} ->
|
||||
{:noreply,
|
||||
socket
|
||||
|
@ -16,6 +16,16 @@
|
||||
<%= label f, :body %>
|
||||
<%= textarea 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>
|
||||
<%= submit "Save", phx_disable_with: "Saving..." %>
|
||||
|
@ -3,6 +3,7 @@ defmodule AggieditWeb.PostLive.Index do
|
||||
|
||||
alias Aggiedit.Rooms
|
||||
alias Aggiedit.Rooms.Post
|
||||
alias Aggiedit.Repo
|
||||
|
||||
@impl true
|
||||
def mount(_params, session, socket) do
|
||||
@ -25,9 +26,10 @@ defmodule AggieditWeb.PostLive.Index do
|
||||
end
|
||||
|
||||
defp apply_action(socket, :new, _params) do
|
||||
post = %Post{user_id: socket.assigns[:current_user].id}
|
||||
socket
|
||||
|> assign(:page_title, "New Post")
|
||||
|> assign(:post, %Post{})
|
||||
|> assign(:post, post)
|
||||
end
|
||||
|
||||
defp apply_action(socket, :index, _params) do
|
||||
|
@ -6,11 +6,10 @@ defmodule Aggiedit.Repo.Migrations.CreateUploads do
|
||||
add :file, :text
|
||||
add :mime, :text
|
||||
add :size, :integer
|
||||
add :user_id, references(:users, on_delete: :nothing)
|
||||
|
||||
add :user_id, references(:users, on_delete: :delete_all)
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
||||
create index(:uploads, [:user_id])
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user