Attach upload, room, user to post
This commit is contained in:
parent
ed22eb20f9
commit
55858eb9ca
@ -247,6 +247,7 @@ defmodule Aggiedit.Accounts do
|
|||||||
def get_user_by_session_token(token) do
|
def get_user_by_session_token(token) do
|
||||||
{:ok, query} = UserToken.verify_session_token_query(token)
|
{:ok, query} = UserToken.verify_session_token_query(token)
|
||||||
Repo.one(query)
|
Repo.one(query)
|
||||||
|
|> Repo.preload(:room)
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
|
@ -6,6 +6,7 @@ defmodule Aggiedit.Rooms do
|
|||||||
import Ecto.Query, warn: false
|
import Ecto.Query, warn: false
|
||||||
alias Aggiedit.Repo
|
alias Aggiedit.Repo
|
||||||
|
|
||||||
|
alias Aggiedit.Accounts
|
||||||
alias Aggiedit.Rooms.Room
|
alias Aggiedit.Rooms.Room
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
@ -140,9 +141,14 @@ defmodule Aggiedit.Rooms do
|
|||||||
"""
|
"""
|
||||||
def get_post!(id), do: Repo.get!(Post, id)
|
def get_post!(id), do: Repo.get!(Post, id)
|
||||||
|
|
||||||
def create_post(attrs, after_save \\ &{:ok, &1}) do
|
def create_post(attrs, user, after_save \\ &{:ok, &1}) do
|
||||||
|
user = Repo.preload(user, :room)
|
||||||
|
|
||||||
%Post{}
|
%Post{}
|
||||||
|
|> Repo.preload([:user, :room])
|
||||||
|> Post.changeset(attrs)
|
|> Post.changeset(attrs)
|
||||||
|
|> Ecto.Changeset.put_assoc(:user, user)
|
||||||
|
|> Ecto.Changeset.put_assoc(:room, user.room)
|
||||||
|> Repo.insert()
|
|> Repo.insert()
|
||||||
|> post_saved(after_save)
|
|> post_saved(after_save)
|
||||||
end
|
end
|
||||||
|
@ -37,21 +37,11 @@ defmodule Aggiedit.Uploads do
|
|||||||
"""
|
"""
|
||||||
def get_upload!(id), do: Repo.get!(Upload, id)
|
def get_upload!(id), do: Repo.get!(Upload, id)
|
||||||
|
|
||||||
@doc """
|
def create_upload(attrs, user) do
|
||||||
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{}
|
||||||
|
|> Repo.preload(:user)
|
||||||
|> Upload.changeset(attrs)
|
|> Upload.changeset(attrs)
|
||||||
|
|> Ecto.Changeset.put_assoc(:user, user)
|
||||||
|> Repo.insert()
|
|> Repo.insert()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -3,17 +3,19 @@ defmodule AggieditWeb.PostLive.FormComponent do
|
|||||||
|
|
||||||
alias Aggiedit.Rooms
|
alias Aggiedit.Rooms
|
||||||
alias Aggiedit.Rooms.Post
|
alias Aggiedit.Rooms.Post
|
||||||
|
alias Aggiedit.Uploads
|
||||||
alias Aggiedit.Uploads.Upload
|
alias Aggiedit.Uploads.Upload
|
||||||
alias Aggiedit.Repo
|
alias Aggiedit.Repo
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def update(%{post: post} = assigns, socket) do
|
def update(%{current_user: current_user, post: post} = assigns, socket) do
|
||||||
changeset = Rooms.change_post(post)
|
changeset = Rooms.change_post(post)
|
||||||
|
|
||||||
{:ok,
|
{:ok,
|
||||||
socket
|
socket
|
||||||
|> assign(assigns)
|
|> assign(assigns)
|
||||||
|> assign(:changeset, changeset)
|
|> assign(:changeset, changeset)
|
||||||
|
|> assign(:current_user, current_user)
|
||||||
|> assign(:uploaded_files, [])
|
|> assign(:uploaded_files, [])
|
||||||
|> allow_upload(:upload, accept: ~w(.jpg .jpeg .png .gif), max_entries: 1)
|
|> allow_upload(:upload, accept: ~w(.jpg .jpeg .png .gif), max_entries: 1)
|
||||||
}
|
}
|
||||||
@ -38,19 +40,20 @@ defmodule AggieditWeb.PostLive.FormComponent do
|
|||||||
consume_uploaded_entries(socket, :upload, fn data, upload ->
|
consume_uploaded_entries(socket, :upload, fn data, upload ->
|
||||||
[extension | _] = MIME.extensions(upload.client_type)
|
[extension | _] = MIME.extensions(upload.client_type)
|
||||||
filename = "#{upload.uuid}-#{extension}"
|
filename = "#{upload.uuid}-#{extension}"
|
||||||
upload = %Upload{
|
|
||||||
file: filename,
|
|
||||||
size: upload.client_size,
|
|
||||||
mime: upload.client_type
|
|
||||||
}
|
|
||||||
|
|
||||||
dest = Path.join("priv/static/uploads", filename)
|
dest = Path.join("priv/static/uploads", filename)
|
||||||
File.cp!(data.path, dest)
|
File.cp!(data.path, dest)
|
||||||
|
|
||||||
Repo.preload(post, :upload)
|
{:ok, upload} = Uploads.create_upload(%{
|
||||||
|
file: filename,
|
||||||
|
size: upload.client_size,
|
||||||
|
mime: upload.client_type,
|
||||||
|
}, socket.assigns.current_user)
|
||||||
|
|
||||||
|
post
|
||||||
|
|> Repo.preload(:upload)
|
||||||
|> Post.change_upload(upload)
|
|> Post.change_upload(upload)
|
||||||
|> Repo.update
|
|> Repo.update()
|
||||||
IO.puts(inspect(upload))
|
|
||||||
|
|
||||||
{:ok, upload}
|
{:ok, upload}
|
||||||
end)
|
end)
|
||||||
@ -71,7 +74,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, &save_upload(socket, &1)) do
|
case Rooms.create_post(post_params, socket.assigns.current_user, &save_upload(socket, &1)) do
|
||||||
{:ok, _post} ->
|
{:ok, _post} ->
|
||||||
{:noreply,
|
{:noreply,
|
||||||
socket
|
socket
|
||||||
|
@ -26,10 +26,9 @@ 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
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
<%= if @live_action in [:new, :edit] do %>
|
<%= if @live_action in [:new, :edit] do %>
|
||||||
<.modal return_to={Routes.post_index_path(@socket, :index)}>
|
<.modal return_to={Routes.post_index_path(@socket, :index)}>
|
||||||
<.live_component
|
<.live_component
|
||||||
|
current_user={@current_user}
|
||||||
module={AggieditWeb.PostLive.FormComponent}
|
module={AggieditWeb.PostLive.FormComponent}
|
||||||
id={@post.id || :new}
|
id={@post.id || :new}
|
||||||
title={@page_title}
|
title={@page_title}
|
||||||
|
Loading…
Reference in New Issue
Block a user