Attach upload, room, user to post

This commit is contained in:
Logan Hunt 2022-04-07 15:52:32 -06:00
parent ed22eb20f9
commit 55858eb9ca
Signed by untrusted user who does not match committer: simponic
GPG Key ID: 52B3774857EB24B1
6 changed files with 26 additions and 26 deletions

View File

@ -247,6 +247,7 @@ defmodule Aggiedit.Accounts do
def get_user_by_session_token(token) do
{:ok, query} = UserToken.verify_session_token_query(token)
Repo.one(query)
|> Repo.preload(:room)
end
@doc """

View File

@ -6,6 +6,7 @@ defmodule Aggiedit.Rooms do
import Ecto.Query, warn: false
alias Aggiedit.Repo
alias Aggiedit.Accounts
alias Aggiedit.Rooms.Room
@doc """
@ -140,9 +141,14 @@ defmodule Aggiedit.Rooms do
"""
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{}
|> Repo.preload([:user, :room])
|> Post.changeset(attrs)
|> Ecto.Changeset.put_assoc(:user, user)
|> Ecto.Changeset.put_assoc(:room, user.room)
|> Repo.insert()
|> post_saved(after_save)
end

View File

@ -37,21 +37,11 @@ defmodule Aggiedit.Uploads do
"""
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
def create_upload(attrs, user) do
%Upload{}
|> Repo.preload(:user)
|> Upload.changeset(attrs)
|> Ecto.Changeset.put_assoc(:user, user)
|> Repo.insert()
end

View File

@ -3,17 +3,19 @@ defmodule AggieditWeb.PostLive.FormComponent do
alias Aggiedit.Rooms
alias Aggiedit.Rooms.Post
alias Aggiedit.Uploads
alias Aggiedit.Uploads.Upload
alias Aggiedit.Repo
@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)
{:ok,
socket
|> assign(assigns)
|> assign(:changeset, changeset)
|> assign(:current_user, current_user)
|> assign(:uploaded_files, [])
|> 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 ->
[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)
{: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)
|> Repo.update
IO.puts(inspect(upload))
|> Repo.update()
{:ok, upload}
end)
@ -71,7 +74,7 @@ defmodule AggieditWeb.PostLive.FormComponent do
end
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} ->
{:noreply,
socket

View File

@ -26,10 +26,9 @@ 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

View File

@ -3,6 +3,7 @@
<%= if @live_action in [:new, :edit] do %>
<.modal return_to={Routes.post_index_path(@socket, :index)}>
<.live_component
current_user={@current_user}
module={AggieditWeb.PostLive.FormComponent}
id={@post.id || :new}
title={@page_title}