Skip to content

Commit

Permalink
chore: move logic from live view to context
Browse files Browse the repository at this point in the history
  • Loading branch information
jarlah committed Sep 23, 2024
1 parent f636be4 commit b5e92fc
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 38 deletions.
36 changes: 36 additions & 0 deletions lib/journi_plan/itineraries.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ defmodule JourniPlan.Itineraries do
Repo.get_by!(Itinerary, uuid: uuid)
end

def change_itinerary(itinerary, action, params \\ nil)

def change_itinerary(%Itinerary{} = itinerary, :edit, params) do
UpdateItinerary.changeset(
%UpdateItinerary{uuid: itinerary.uuid},
params || Map.from_struct(itinerary)
)
end

def change_itinerary(_itinerary, :new, params) do
CreateItinerary.changeset(
%CreateItinerary{},
params || %{}
)
end

def create_itinerary(attrs \\ %{}) do
uuid = Ecto.UUID.generate()

Expand Down Expand Up @@ -150,10 +166,30 @@ defmodule JourniPlan.Itineraries do
end
end

def list_journal_entries do
Repo.all(JournalEntry)
end

def get_journal_entry!(uuid) do
Repo.get_by!(JournalEntry, uuid: uuid)
end

def change_journal_entry(journal_entry, action, params \\ nil)

def change_journal_entry(%JournalEntry{} = journal_entry, :edit, params) do
UpdateJournalEntry.changeset(
%UpdateJournalEntry{uuid: journal_entry.uuid},
params || Map.from_struct(journal_entry)
)
end

def change_journal_entry(_journal_entry, :new, params) do
CreateJournalEntry.changeset(
%CreateJournalEntry{},
params || %{}
)
end

def create_journal_entry(attrs \\ %{}) do
uuid = Ecto.UUID.generate()

Expand Down
9 changes: 2 additions & 7 deletions lib/journi_plan/itineraries/projectors/activity.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ defmodule JourniPlan.Itineraries.Projectors.Activity do

alias JourniPlan.Repo

import JourniPlan.Utils.UUID

alias JourniPlan.Itineraries.Events.ActivityCreated
alias JourniPlan.Itineraries.Events.ActivityNameUpdated
alias JourniPlan.Itineraries.Events.ActivityDescriptionUpdated
Expand Down Expand Up @@ -66,11 +68,4 @@ defmodule JourniPlan.Itineraries.Projectors.Activity do
activity -> Ecto.Multi.delete(multi, :activity, activity)
end
end)

defp cast_uuid!(uuid) do
case Ecto.UUID.dump(uuid) do
{:ok, casted_uuid} -> casted_uuid
:error -> raise ArgumentError, "Invalid UUID: #{uuid}"
end
end
end
9 changes: 2 additions & 7 deletions lib/journi_plan/itineraries/projectors/journal_entry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ defmodule JourniPlan.Itineraries.Projectors.JournalEntry do

alias JourniPlan.Repo

import JourniPlan.Utils.UUID

alias JourniPlan.Itineraries.Events.JournalEntryCreated
alias JourniPlan.Itineraries.Events.JournalEntryTitleUpdated
alias JourniPlan.Itineraries.Events.JournalEntryBodyUpdated
Expand Down Expand Up @@ -50,11 +52,4 @@ defmodule JourniPlan.Itineraries.Projectors.JournalEntry do
journal_entry -> Ecto.Multi.delete(multi, :journal_entry, journal_entry)
end
end)

defp cast_uuid!(uuid) do
case Ecto.UUID.dump(uuid) do
{:ok, casted_uuid} -> casted_uuid
:error -> raise ArgumentError, "Invalid UUID: #{uuid}"
end
end
end
12 changes: 12 additions & 0 deletions lib/journi_plan/utils/uuid.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
defmodule JourniPlan.Utils.UUID do
def cast_uuid!(uuid) do
if uuid == nil do
nil
else
case Ecto.UUID.dump(uuid) do
{:ok, casted_uuid} -> casted_uuid
:error -> raise ArgumentError, "Invalid UUID: #{uuid}"
end
end
end
end
25 changes: 3 additions & 22 deletions lib/journi_plan_web/live/itinerary_live/form_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ defmodule JourniPlanWeb.ItineraryLive.FormComponent do
use JourniPlanWeb, :live_component

alias JourniPlan.Itineraries
alias JourniPlan.Itineraries.Commands.CreateItinerary
alias JourniPlan.Itineraries.Commands.UpdateItinerary

@impl true
def render(assigns) do
Expand Down Expand Up @@ -32,33 +30,16 @@ defmodule JourniPlanWeb.ItineraryLive.FormComponent do
end

@impl true
def update(%{itinerary: _itinerary, action: :new} = assigns, socket) do
changeset = CreateItinerary.changeset(%CreateItinerary{}, %{})

def update(%{itinerary: itinerary, action: action} = assigns, socket) do
{:ok,
socket
|> assign(assigns)
|> assign(:form, to_form(changeset, as: "itinerary"))}
end

@impl true
def update(%{itinerary: itinerary, action: :edit} = assigns, socket) do
changeset = UpdateItinerary.changeset(%UpdateItinerary{uuid: itinerary.uuid}, Map.from_struct(itinerary))

{:ok,
socket
|> assign(assigns)
|> assign(:form, to_form(changeset, as: "itinerary"))}
|> assign(:form, to_form(Itineraries.change_itinerary(itinerary, action), as: "itinerary"))}
end

@impl true
def handle_event("validate", %{"itinerary" => itinerary_params}, socket) do
changeset =
case socket.assigns.action do
:new -> CreateItinerary.changeset(%CreateItinerary{}, itinerary_params)
:edit -> UpdateItinerary.changeset(%UpdateItinerary{uuid: socket.assigns.itinerary.uuid}, itinerary_params)
end

changeset = Itineraries.change_itinerary(socket.assigns.itinerary, socket.assigns.action, itinerary_params)
{:noreply, assign(socket, form: to_form(changeset, action: :validate, as: "itinerary"))}
end

Expand Down
22 changes: 20 additions & 2 deletions test/support/fixtures/itineraries_fixtures.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,35 @@ defmodule JourniPlan.ItinerariesFixtures do
Generate a itinerary.
"""
def itinerary_fixture(attrs \\ %{}) do
user = AccountsFixtures.user_fixture()
user_id = attrs[:user_id] || AccountsFixtures.user_fixture().id

{:ok, itinerary} =
attrs
|> Enum.into(%{
description: "some description",
name: "some name",
user_id: user.id
user_id: user_id
})
|> JourniPlan.Itineraries.create_itinerary()

itinerary
end

def journal_entry_fixture(attrs \\ %{}) do
user_id = attrs[:user_id] || AccountsFixtures.user_fixture().id
itinerary_id = attrs[:itinerary_id] || itinerary_fixture(%{user_id: user_id}).uuid

{:ok, journal_entry} =
attrs
|> Enum.into(%{
body: "some body",
title: "some title",
user_id: user_id,
entry_date: ~U[2021-01-01 00:00:00Z],
itinerary_id: itinerary_id
})
|> JourniPlan.Itineraries.create_journal_entry()

journal_entry
end
end

0 comments on commit b5e92fc

Please sign in to comment.