diff --git a/lib/journi_plan/itineraries.ex b/lib/journi_plan/itineraries.ex index 8a5fd5a..a5ae7a6 100644 --- a/lib/journi_plan/itineraries.ex +++ b/lib/journi_plan/itineraries.ex @@ -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() @@ -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() diff --git a/lib/journi_plan/itineraries/projectors/activity.ex b/lib/journi_plan/itineraries/projectors/activity.ex index 7c6fd01..f5ed640 100644 --- a/lib/journi_plan/itineraries/projectors/activity.ex +++ b/lib/journi_plan/itineraries/projectors/activity.ex @@ -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 @@ -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 diff --git a/lib/journi_plan/itineraries/projectors/journal_entry.ex b/lib/journi_plan/itineraries/projectors/journal_entry.ex index 3b4590f..8331677 100644 --- a/lib/journi_plan/itineraries/projectors/journal_entry.ex +++ b/lib/journi_plan/itineraries/projectors/journal_entry.ex @@ -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 @@ -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 diff --git a/lib/journi_plan/utils/uuid.ex b/lib/journi_plan/utils/uuid.ex new file mode 100644 index 0000000..71ea4d4 --- /dev/null +++ b/lib/journi_plan/utils/uuid.ex @@ -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 diff --git a/lib/journi_plan_web/live/itinerary_live/form_component.ex b/lib/journi_plan_web/live/itinerary_live/form_component.ex index 7c2cffa..bb689a5 100644 --- a/lib/journi_plan_web/live/itinerary_live/form_component.ex +++ b/lib/journi_plan_web/live/itinerary_live/form_component.ex @@ -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 @@ -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 diff --git a/test/support/fixtures/itineraries_fixtures.ex b/test/support/fixtures/itineraries_fixtures.ex index 2853327..fcbb451 100644 --- a/test/support/fixtures/itineraries_fixtures.ex +++ b/test/support/fixtures/itineraries_fixtures.ex @@ -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