Skip to content

Commit

Permalink
remove commanded library (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
jarlah authored Nov 18, 2024
1 parent 179d607 commit 5779945
Show file tree
Hide file tree
Showing 52 changed files with 171 additions and 1,090 deletions.
10 changes: 0 additions & 10 deletions lib/journi_plan/app.ex

This file was deleted.

2 changes: 0 additions & 2 deletions lib/journi_plan/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ defmodule JourniPlan.Application do
@impl true
def start(_type, _args) do
children = [
JourniPlan.App,
JourniPlan.Itineraries.Supervisor,
JourniPlanWeb.Telemetry,
JourniPlan.Repo,
{DNSCluster, query: Application.get_env(:journi_plan, :dns_cluster_query) || :ignore},
Expand Down
3 changes: 0 additions & 3 deletions lib/journi_plan/event_store.ex

This file was deleted.

180 changes: 51 additions & 129 deletions lib/journi_plan/itineraries.ex
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
defmodule JourniPlan.Itineraries do
use Bond
import Ecto.Query, warn: false
alias JourniPlan.App

alias JourniPlan.Repo

alias JourniPlan.Itineraries.Commands.{
CreateItinerary,
UpdateItinerary,
DeleteItinerary,
CreateActivity,
UpdateActivity,
DeleteActivity,
CreateJournalEntry,
UpdateJournalEntry,
DeleteJournalEntry
}

alias JourniPlan.Itineraries.Projections.Itinerary
alias JourniPlan.Itineraries.Projections.Activity
alias JourniPlan.Itineraries.Projections.JournalEntry
alias JourniPlan.Itineraries.Itinerary
alias JourniPlan.Itineraries.Activity
alias JourniPlan.Itineraries.JournalEntry

def list_user_itineraries(user_id) do
from(i in Itinerary, where: i.user_id == ^user_id)
Expand All @@ -36,70 +25,49 @@ defmodule JourniPlan.Itineraries do
|> Repo.preload([:activities, :journal_entries])
end

def change_itinerary(itinerary, action, params \\ nil)
def change_itinerary(itinerary, action, params \\ %{})

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

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

@pre attrs_doesnt_have_id: not Map.has_key?(attrs, "uuid")
def create_itinerary(attrs \\ %{}) do
uuid = Ecto.UUID.generate()
attrs = Map.put(attrs, "uuid", uuid)

command =
attrs
|> CreateItinerary.new()
|> CreateItinerary.assign_uuid(uuid)

changeset = CreateItinerary.changeset(command, attrs)
changeset = Itinerary.changeset(%Itinerary{}, attrs)

if changeset.valid? do
with :ok <- App.dispatch(command, consistency: :strong) do
{:ok, get_itinerary!(uuid)}
else
reply -> reply
case Repo.insert(changeset) do
{:ok, itinerary} -> {:ok, itinerary |> Repo.preload([:activities, :journal_entries])}
{:error, changeset} -> {:error, changeset}
end
else
{:error, changeset}
end
end

def update_itinerary(%Itinerary{uuid: uuid}, attrs) do
command =
attrs
|> UpdateItinerary.new()
|> UpdateItinerary.assign_uuid(uuid)

changeset = UpdateItinerary.changeset(command, attrs)
itinerary = get_itinerary!(uuid)
changeset = Itinerary.changeset(itinerary, attrs)

if changeset.valid? do
with :ok <- App.dispatch(command, consistency: :strong) do
{:ok, get_itinerary!(uuid)}
else
reply -> reply
case Repo.update(changeset) do
{:ok, itinerary} -> {:ok, itinerary}
{:error, changeset} -> {:error, changeset}
end
else
{:error, changeset}
end
end

def delete_itinerary(%Itinerary{uuid: uuid} = itinerary) do
command = %DeleteItinerary{uuid: uuid}

with :ok <- App.dispatch(command, consistency: :strong) do
{:ok, itinerary}
else
reply -> reply
end
def delete_itinerary(%Itinerary{uuid: uuid}) do
%Itinerary{uuid: uuid} |> Repo.delete()
end

def get_itineraries_by_user_id(user_id) do
Expand Down Expand Up @@ -137,52 +105,35 @@ defmodule JourniPlan.Itineraries do

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

command =
attrs
|> CreateActivity.new()
|> CreateActivity.assign_uuid(uuid)

changeset = CreateActivity.changeset(command, attrs)
attrs = Map.put(attrs, "uuid", uuid)
changeset = Activity.changeset(%Activity{}, attrs)

if changeset.valid? do
with :ok <- App.dispatch(command, consistency: :strong) do
{:ok, get_activity!(uuid)}
else
reply -> reply
case Repo.insert(changeset) do
{:ok, activity} -> {:ok, activity}
{:error, changeset} -> {:error, changeset}
end
else
{:error, changeset}
end
end

def update_activity(%Activity{uuid: uuid}, attrs) do
command =
attrs
|> UpdateActivity.new()
|> UpdateActivity.assign_uuid(uuid)

changeset = UpdateActivity.changeset(command, attrs)
activity = get_activity!(uuid)
changeset = Activity.changeset(activity, attrs)

if changeset.valid? do
with :ok <- App.dispatch(command, consistency: :strong) do
{:ok, get_activity!(uuid)}
else
reply -> reply
case Repo.update(changeset) do
{:ok, updated_entry} -> {:ok, updated_entry}
{:error, changeset} -> {:error, changeset}
end
else
{:error, changeset}
end
end

def delete_activity(%Activity{uuid: uuid} = activity) do
command = %DeleteActivity{uuid: uuid}

with :ok <- App.dispatch(command, consistency: :strong) do
{:ok, activity}
else
reply -> reply
end
def delete_activity(%Activity{uuid: uuid}) do
%Activity{uuid: uuid} |> Repo.delete()
end

def list_user_activities(user_id) do
Expand All @@ -202,85 +153,56 @@ defmodule JourniPlan.Itineraries do
|> Repo.preload([:activity, :itinerary])
end

def change_activity(activity, action, params \\ nil)
def change_activity(activity, action, params \\ %{})

def change_activity(%Activity{} = activity, :edit, params) do
UpdateActivity.changeset(
%UpdateActivity{uuid: activity.uuid},
params || Map.from_struct(activity)
)
activity |> Activity.changeset(params)
end

def change_activity(_activity, :new, params) do
CreateActivity.changeset(
%CreateActivity{},
params || %{}
)
%Activity{} |> Activity.changeset(params)
end

def change_journal_entry(journal_entry, action, params \\ nil)
def change_journal_entry(journal_entry, action, params \\ %{})

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

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

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

command =
attrs
|> CreateJournalEntry.new()
|> CreateJournalEntry.assign_uuid(uuid)

changeset = CreateJournalEntry.changeset(command, attrs)
attrs = Map.put(attrs, "uuid", uuid)
changeset = JournalEntry.changeset(%JournalEntry{}, attrs)

if changeset.valid? do
with :ok <- App.dispatch(command, consistency: :strong) do
{:ok, get_journal_entry!(uuid)}
else
reply -> reply
case Repo.insert(changeset) do
{:ok, journal_entry} -> {:ok, journal_entry}
{:error, changeset} -> {:error, changeset}
end
else
{:error, changeset}
end
end

def update_journal_entry(%JournalEntry{uuid: uuid}, attrs) do
command =
attrs
|> UpdateJournalEntry.new()
|> UpdateJournalEntry.assign_uuid(uuid)

changeset = UpdateJournalEntry.changeset(command, attrs)
journal_entry = get_journal_entry!(uuid)
changeset = JournalEntry.changeset(journal_entry, attrs)

if changeset.valid? do
with :ok <- App.dispatch(command, consistency: :strong) do
{:ok, get_journal_entry!(uuid)}
else
reply -> reply
case Repo.update(changeset) do
{:ok, updated_entry} -> {:ok, updated_entry}
{:error, changeset} -> {:error, changeset}
end
else
{:error, changeset}
end
end

def delete_journal_entry(%JournalEntry{uuid: uuid} = journal_entry) do
command = %DeleteJournalEntry{uuid: uuid}

with :ok <- App.dispatch(command, consistency: :strong) do
{:ok, journal_entry}
else
reply -> reply
end
def delete_journal_entry(%JournalEntry{uuid: uuid}) do
%JournalEntry{uuid: uuid} |> Repo.delete()
end
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule JourniPlan.Itineraries.Projections.Activity do
defmodule JourniPlan.Itineraries.Activity do
use Ecto.Schema
import Ecto.Changeset

Expand All @@ -11,7 +11,7 @@ defmodule JourniPlan.Itineraries.Projections.Activity do
field :start_time, :utc_datetime
field :end_time, :utc_datetime

belongs_to :itinerary, JourniPlan.Itineraries.Projections.Itinerary,
belongs_to :itinerary, JourniPlan.Itineraries.Itinerary,
references: :uuid,
foreign_key: :itinerary_uuid,
type: :binary_id
Expand All @@ -21,8 +21,9 @@ defmodule JourniPlan.Itineraries.Projections.Activity do
timestamps(type: :utc_datetime)
end

def changeset(activity, attrs) do
def changeset(activity, attrs \\ %{}) do
activity
|> cast(attrs, [:name, :description, :start_time, :end_time])
|> cast(attrs, [:uuid, :user_id, :itinerary_uuid, :name, :description, :start_time, :end_time])
|> validate_required([:user_id, :name, :description, :start_time, :end_time])
end
end
Loading

0 comments on commit 5779945

Please sign in to comment.