Skip to content

Commit

Permalink
wip use put_assoc for WorkUploads
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Mar 19, 2024
1 parent a39b0dc commit c4e39ee
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 44 deletions.
12 changes: 4 additions & 8 deletions lib/banchan/works/work.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ defmodule Banchan.Works.Work do
field :tags, {:array, :string}
field :mature, :boolean, default: false
field :private, :boolean, default: false
field :upload_count, :integer, virtual: true
belongs_to :studio, Banchan.Studios.Studio
belongs_to :client, Banchan.Accounts.User, on_replace: :nilify
belongs_to :offering, Banchan.Offerings.Offering, on_replace: :nilify
Expand All @@ -38,17 +37,14 @@ defmodule Banchan.Works.Work do
end

work
|> cast(attrs, [:title, :description, :tags, :upload_count, :private, :mature])
|> validate_required([:title, :upload_count])
|> cast(attrs, [:title, :description, :tags, :private, :mature])
|> validate_required([:title])
|> validate_length(:title, min: 3, max: 50)
|> validate_number(:upload_count,
greater_than_or_equal_to: 1,
less_than_or_equal_to: 10,
message: "must include between 1 and 10 uploads"
)
|> validate_rich_text_length(:description, max: 500)
|> validate_tags()
|> validate_length(:tags, max: 5)
|> put_assoc(:uploads, attrs["uploads"], required: true)
|> validate_length(:uploads, min: 1, max: 10)
end

def rand_id() do
Expand Down
3 changes: 2 additions & 1 deletion lib/banchan/works/work_upload.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule Banchan.Works.WorkUpload do
schema "work_uploads" do
field :index, :integer
field :comment, :string
field :ref, :string, virtual: true
belongs_to :work, Banchan.Works.Work
belongs_to :upload, Banchan.Uploads.Upload, type: :binary_id
belongs_to :preview, Banchan.Uploads.Upload, on_replace: :nilify, type: :binary_id
Expand All @@ -22,7 +23,7 @@ defmodule Banchan.Works.WorkUpload do
def changeset(work_upload, attrs) do
work_upload
|> cast(attrs, [:index, :comment])
|> validate_required([:index, :comment])
|> validate_required([:index])
|> validate_rich_text_length(:comment, max: 200)
end
end
12 changes: 5 additions & 7 deletions lib/banchan/works/works.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ defmodule Banchan.Works do
def new_work(%User{} = actor, %Studio{} = studio, attrs, uploads, opts \\ []) do
{:ok, ret} =
Repo.transaction(fn ->
with {:ok, uploads} <-
if(Enum.empty?(uploads), do: {:error, :uploads_required}, else: {:ok, uploads}),
{:ok, _actor} <- Studios.check_studio_member(studio, actor) do
with {:ok, _actor} <- Studios.check_studio_member(studio, actor) do
work_uploads =
uploads
|> Enum.with_index()
Expand All @@ -52,11 +50,11 @@ defmodule Banchan.Works do
end

%WorkUpload{
index: index,
comment: "",
upload_id: upload.id,
preview_id: preview_id
}
|> WorkUpload.changeset(%{"index" => index})
end)

commission = Keyword.get(opts, :commission)
Expand All @@ -73,10 +71,9 @@ defmodule Banchan.Works do
studio_id: studio.id,
commission_id: commission && commission.id,
client_id: client_id,
offering_id: offering_id,
uploads: work_uploads
offering_id: offering_id
}
|> Work.changeset(attrs)
|> Work.changeset(Map.put(attrs, "uploads", work_uploads))
|> Repo.insert()
end
end)
Expand Down Expand Up @@ -373,6 +370,7 @@ defmodule Banchan.Works do
Fetches a `WorkUpload`, but only if the given `User` is allowed access.
"""
def get_work_upload_if_allowed!(work, upload_id, actor)

def get_work_upload_if_allowed!(%Work{} = work, upload_id, nil) do
from(
work_upload in WorkUpload,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ defmodule BanchanWeb.WorkLive.Components.WorkUploads do
/>
</Lightbox.Item>
{#elseif type == :existing}
<Icon name="file-up" class="non-media-file" size={32} label={wupload.name}>
<span class="upload-name">{wupload.name}</span>
<Icon name="file-up" class="non-media-file" size={32} label={wupload.upload.name}>
<span class="upload-name">{wupload.upload.name}</span>
</Icon>
{#elseif type == :live && Uploads.media?(wupload.client_type)}
<Lightbox.Item>
Expand Down
90 changes: 64 additions & 26 deletions lib/banchan_web/live/work_live/work.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,7 @@ defmodule BanchanWeb.WorkLive.Work do
alias Banchan.Repo
alias Banchan.Uploads
alias Banchan.Works
alias Banchan.Works.Work

alias BanchanWeb.Components.{
Icon,
Layout,
RichText,
Tag
}

alias BanchanWeb.WorkLive.Components.WorkUploads
alias Banchan.Works.{Work, WorkUpload}

alias Surface.Components.{
Form,
Expand All @@ -30,27 +21,33 @@ defmodule BanchanWeb.WorkLive.Work do
LiveRedirect
}

alias Surface.Components.Form.{Field, ErrorTag}

alias BanchanWeb.Components.{
Icon,
Layout,
RichText,
Tag
}

alias BanchanWeb.Components.Form.{
Checkbox,
HiddenInput,
QuillInput,
Submit,
TagsInput,
TextInput
}

alias BanchanWeb.WorkLive.Components.WorkUploads

@impl true
def mount(_params, _session, socket) do
{:ok,
socket
|> allow_upload(:uploads,
accept: :any,
max_entries: 10,
max_file_size: Uploads.max_upload_size(),
progress: fn :uploads, entry, socket ->
dbg(entry)
{:noreply, socket}
end
max_file_size: Uploads.max_upload_size()
)}
end

Expand Down Expand Up @@ -118,8 +115,21 @@ defmodule BanchanWeb.WorkLive.Work do

@impl true
def handle_event("change", %{"work" => work}, socket) do
uploads =
socket.assigns.work_uploads
|> Enum.with_index()
|> Enum.map(fn {{ty, data}, index} ->
if ty == :live do
%WorkUpload{}
|> WorkUpload.changeset(%{"index" => index, "ref" => data.ref, "comment" => ""})
else
data
|> WorkUpload.changeset(%{"index" => index})
end
end)

changeset =
Work.changeset(socket.assigns.work, work)
Work.changeset(socket.assigns.work, Map.put(work, "uploads", uploads))
|> Map.put(:action, if(is_nil(socket.assigns.work.id), do: :insert, else: :update))

{:noreply, assign(socket, changeset: changeset)}
Expand All @@ -130,19 +140,29 @@ defmodule BanchanWeb.WorkLive.Work do
uploads =
consume_uploaded_entries(socket, :uploads, fn %{path: path}, entry ->
{:ok,
Uploads.save_file!(
socket.assigns.current_user,
path,
entry.client_type,
entry.client_name
)}
{entry.ref,
Uploads.save_file!(
socket.assigns.current_user,
path,
entry.client_type,
entry.client_name
)}}
end)

Works.new_work(
socket.assigns.current_user,
socket.assigns.studio,
work,
uploads,
socket.assigns.work_uploads
|> Enum.map(fn {ty, data} ->
if ty == :live do
Enum.find_value(uploads, fn {ref, upload} ->
if ref == data.ref, do: upload
end)
else
data.upload
end
end),
commission: socket.assigns.commission,
offering: socket.assigns.offering
)
Expand Down Expand Up @@ -171,10 +191,23 @@ defmodule BanchanWeb.WorkLive.Work do
end

def handle_info({:updated_uploads, _, uploads}, socket) do
uploads_param =
uploads
|> Enum.with_index()
|> Enum.map(fn {{ty, data}, index} ->
if ty == :live do
%WorkUpload{}
|> WorkUpload.changeset(%{"index" => index, "ref" => data.ref, "comment" => ""})
else
data
|> WorkUpload.changeset(%{"index" => index})
end
end)

changeset =
Work.changeset(
socket.assigns.work,
socket.assigns.changeset.params |> Map.put("upload_count", Enum.count(uploads))
socket.assigns.changeset.params |> Map.put("uploads", uploads_param)
)
|> Map.put(:action, if(is_nil(socket.assigns.work.id), do: :insert, else: :update))

Expand Down Expand Up @@ -342,6 +375,9 @@ defmodule BanchanWeb.WorkLive.Work do
</div>
<LiveFileInput upload={@uploads.uploads} />
</div>
<Field name={:uploads}>
<ErrorTag class="help text-error" />
</Field>
{/if}
</div>
{#if is_nil(@changeset)}
Expand Down Expand Up @@ -370,7 +406,6 @@ defmodule BanchanWeb.WorkLive.Work do
{/if}
{#else}
<div class="work-form">
<HiddenInput name={:upload_count} value={Enum.count(@work_uploads)} />
<QuillInput id="work-description" label="Description" name={:description} />
<TagsInput id="work-tags" label="Tags" name={:tags} />
<div class="flags-form">
Expand Down Expand Up @@ -409,6 +444,9 @@ defmodule BanchanWeb.WorkLive.Work do
</div>
<LiveFileInput upload={@uploads.uploads} />
</div>
<Field name={:uploads}>
<ErrorTag class="help text-error" />
</Field>
{/if}
</div>
</Form>
Expand Down

0 comments on commit c4e39ee

Please sign in to comment.