Skip to content

Commit

Permalink
format code
Browse files Browse the repository at this point in the history
  • Loading branch information
jarlah committed Oct 3, 2024
1 parent cddc452 commit f1d119a
Show file tree
Hide file tree
Showing 36 changed files with 370 additions and 152 deletions.
8 changes: 4 additions & 4 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ if config_env() == :prod do
socket_options: maybe_ipv6

database_url_without_query_string =
database_events_url
|> URI.new!()
|> then(&%URI{&1 | query: nil, path: "#{&1.path}_events"})
|> URI.to_string()
database_events_url
|> URI.new!()
|> then(&%URI{&1 | query: nil, path: "#{&1.path}_events"})
|> URI.to_string()

config :journi_plan, JourniPlan.EventStore,
serializer: Commanded.Serialization.JsonSerializer,
Expand Down
9 changes: 7 additions & 2 deletions lib/journi_plan/itineraries.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
defmodule JourniPlan.Itineraries do

import Ecto.Query, warn: false
alias JourniPlan.App
alias JourniPlan.Repo

alias JourniPlan.Itineraries.Commands.{
CreateItinerary,
UpdateItinerary,
Expand All @@ -14,6 +14,7 @@ defmodule JourniPlan.Itineraries do
UpdateJournalEntry,
DeleteJournalEntry
}

alias JourniPlan.Itineraries.Projections.Itinerary
alias JourniPlan.Itineraries.Projections.Activity
alias JourniPlan.Itineraries.Projections.JournalEntry
Expand Down Expand Up @@ -103,24 +104,28 @@ defmodule JourniPlan.Itineraries do

def get_itineraries_by_user_id(user_id) do
query = from(i in Itinerary, where: i.user_id == ^user_id)

Repo.all(query)
|> Repo.preload([:activities, :journal_entries])
end

def get_activities_by_itinerary_id(itinerary_uuid) do
query = from(a in Activity, where: a.itinerary_uuid == ^itinerary_uuid)

Repo.all(query)
|> Repo.preload(:itinerary)
end

def get_journal_entries_by_itinerary_id(itinerary_uuid) do
query = from(j in JournalEntry, where: j.itinerary_uuid == ^itinerary_uuid)

Repo.all(query)
|> Repo.preload([:activity, :itinerary])
end

def get_journal_entries_by_activity_id(activity_uuid) do
query = from(j in JournalEntry, where: j.activity_uuid == ^activity_uuid)

Repo.all(query)
|> Repo.preload([:activity, :itinerary])
end
Expand Down Expand Up @@ -182,6 +187,7 @@ defmodule JourniPlan.Itineraries do

def list_user_activities(user_id) do
query = from(i in Activity, where: i.user_id == ^user_id)

Repo.all(query)
|> Repo.preload(:itinerary)
end
Expand Down Expand Up @@ -277,5 +283,4 @@ defmodule JourniPlan.Itineraries do
reply -> reply
end
end

end
3 changes: 2 additions & 1 deletion lib/journi_plan/itineraries/aggregates/activity.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ defmodule JourniPlan.Itineraries.Aggregates.Activity do
if activity.end_time != event.end_time and not is_nil(event.end_time),
do: %ActivityEndTimeUpdated{uuid: activity.uuid, end_time: event.end_time}

[name_command, description_command, start_time_command, end_time_command] |> Enum.filter(&Function.identity/1)
[name_command, description_command, start_time_command, end_time_command]
|> Enum.filter(&Function.identity/1)
end

def execute(%Activity{}, %DeleteActivity{} = event) do
Expand Down
17 changes: 8 additions & 9 deletions lib/journi_plan/itineraries/aggregates/journal_entry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,20 @@ defmodule JourniPlan.Itineraries.Aggregates.JournalEntry do
[title_command, body_command] |> Enum.filter(&Function.identity/1)
end


def execute(%JournalEntry{}, %DeleteJournalEntry{uuid: uuid}) do
%JournalEntryDeleted{uuid: uuid}
end

def apply(%JournalEntry{} = journal_entry, %JournalEntryCreated{} = event) do
%JournalEntry{
journal_entry |
uuid: event.uuid,
title: event.title,
body: event.body,
entry_date: event.entry_date,
itinerary_uuid: event.itinerary_uuid,
activity_uuid: event.activity_uuid,
user_id: event.user_id
journal_entry
| uuid: event.uuid,
title: event.title,
body: event.body,
entry_date: event.entry_date,
itinerary_uuid: event.itinerary_uuid,
activity_uuid: event.activity_uuid,
user_id: event.user_id
}
end

Expand Down
18 changes: 16 additions & 2 deletions lib/journi_plan/itineraries/commands/create_activity.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,22 @@ defmodule JourniPlan.Itineraries.Commands.CreateActivity do
@doc false
def changeset(command, params \\ %{}) do
{command, @types}
|> Changeset.cast(params, [:name, :description, :start_time, :end_time, :itinerary_uuid, :user_id])
|> Changeset.validate_required([:name, :description, :start_time, :end_time, :itinerary_uuid, :user_id])
|> Changeset.cast(params, [
:name,
:description,
:start_time,
:end_time,
:itinerary_uuid,
:user_id
])
|> Changeset.validate_required([
:name,
:description,
:start_time,
:end_time,
:itinerary_uuid,
:user_id
])
|> foreign_key_exists(Itinerary, :uuid, :itinerary_uuid)
|> foreign_key_exists(User, :id, :user_id)
end
Expand Down
9 changes: 8 additions & 1 deletion lib/journi_plan/itineraries/commands/create_journal_entry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ defmodule JourniPlan.Itineraries.Commands.CreateJournalEntry do
@doc false
def changeset(command, params \\ %{}) do
{command, @types}
|> Changeset.cast(params, [:title, :body, :entry_date, :activity_uuid, :itinerary_uuid, :user_id])
|> Changeset.cast(params, [
:title,
:body,
:entry_date,
:activity_uuid,
:itinerary_uuid,
:user_id
])
|> Changeset.validate_required([:title, :body, :entry_date, :itinerary_uuid, :user_id])
|> foreign_key_exists(Activity, :uuid, :activity_uuid)
|> foreign_key_exists(Itinerary, :uuid, :itinerary_uuid)
Expand Down
6 changes: 5 additions & 1 deletion lib/journi_plan/itineraries/projections/activity.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ defmodule JourniPlan.Itineraries.Projections.Activity do
field :start_time, :utc_datetime
field :end_time, :utc_datetime

belongs_to :itinerary, JourniPlan.Itineraries.Projections.Itinerary, references: :uuid, foreign_key: :itinerary_uuid, type: :binary_id
belongs_to :itinerary, JourniPlan.Itineraries.Projections.Itinerary,
references: :uuid,
foreign_key: :itinerary_uuid,
type: :binary_id

belongs_to :user, JourniPlan.Accounts.User

timestamps(type: :utc_datetime)
Expand Down
8 changes: 6 additions & 2 deletions lib/journi_plan/itineraries/projections/itinerary.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ defmodule JourniPlan.Itineraries.Projections.Itinerary do
field :description, :string
field :name, :string

has_many :activities, JourniPlan.Itineraries.Projections.Activity, foreign_key: :itinerary_uuid
has_many :journal_entries, JourniPlan.Itineraries.Projections.JournalEntry, foreign_key: :itinerary_uuid
has_many :activities, JourniPlan.Itineraries.Projections.Activity,
foreign_key: :itinerary_uuid

has_many :journal_entries, JourniPlan.Itineraries.Projections.JournalEntry,
foreign_key: :itinerary_uuid

belongs_to :user, JourniPlan.Accounts.User

timestamps(type: :utc_datetime)
Expand Down
12 changes: 10 additions & 2 deletions lib/journi_plan/itineraries/projections/journal_entry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,16 @@ defmodule JourniPlan.Itineraries.Projections.JournalEntry do
field :entry_date, :utc_datetime
field :title, :string

belongs_to :activity, JourniPlan.Itineraries.Projections.Activity, references: :uuid, foreign_key: :activity_uuid, type: :binary_id
belongs_to :itinerary, JourniPlan.Itineraries.Projections.Itinerary, references: :uuid, foreign_key: :itinerary_uuid, type: :binary_id
belongs_to :activity, JourniPlan.Itineraries.Projections.Activity,
references: :uuid,
foreign_key: :activity_uuid,
type: :binary_id

belongs_to :itinerary, JourniPlan.Itineraries.Projections.Itinerary,
references: :uuid,
foreign_key: :itinerary_uuid,
type: :binary_id

belongs_to :user, JourniPlan.Accounts.User

timestamps(type: :utc_datetime)
Expand Down
31 changes: 24 additions & 7 deletions lib/journi_plan/itineraries/projectors/activity.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule JourniPlan.Itineraries.Projectors.Activity do

alias JourniPlan.Repo

import JourniPlan.Utils.UUID
import JourniPlan.Utils.UUID

alias JourniPlan.Itineraries.Events.{
ActivityCreated,
Expand Down Expand Up @@ -47,22 +47,39 @@ defmodule JourniPlan.Itineraries.Projectors.Activity do

project(%ActivityDescriptionUpdated{uuid: uuid, description: description}, _, fn multi ->
case Repo.get(Activity, uuid) do
nil -> multi
activity -> Ecto.Multi.update(multi, :activity, Activity.changeset(activity, %{description: description}))
nil ->
multi

activity ->
Ecto.Multi.update(
multi,
:activity,
Activity.changeset(activity, %{description: description})
)
end
end)

project(%ActivityStartTimeUpdated{uuid: uuid, start_time: start_time}, _, fn multi ->
case Repo.get(Activity, uuid) do
nil -> multi
activity -> Ecto.Multi.update(multi, :activity, Activity.changeset(activity, %{start_time: start_time}))
nil ->
multi

activity ->
Ecto.Multi.update(
multi,
:activity,
Activity.changeset(activity, %{start_time: start_time})
)
end
end)

project(%ActivityEndTimeUpdated{uuid: uuid, end_time: end_time}, _, fn multi ->
case Repo.get(Activity, uuid) do
nil -> multi
activity -> Ecto.Multi.update(multi, :activity, Activity.changeset(activity, %{end_time: end_time}))
nil ->
multi

activity ->
Ecto.Multi.update(multi, :activity, Activity.changeset(activity, %{end_time: end_time}))
end
end)

Expand Down
19 changes: 14 additions & 5 deletions lib/journi_plan/itineraries/projectors/itinerary.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,25 @@ defmodule JourniPlan.Itineraries.Projectors.Itinerary do

project(%ItineraryNameUpdated{uuid: uuid, name: name}, _, fn multi ->
case Repo.get(Itinerary, uuid) do
nil -> multi
itinerary -> Ecto.Multi.update(multi, :itinerary, Itinerary.changeset(itinerary, %{name: name}))
nil ->
multi

itinerary ->
Ecto.Multi.update(multi, :itinerary, Itinerary.changeset(itinerary, %{name: name}))
end
end)

project(%ItineraryDescriptionUpdated{uuid: uuid, description: description}, _, fn multi ->
case Repo.get(Itinerary, uuid) do
nil -> multi
itinerary -> Ecto.Multi.update(multi, :itinerary, Itinerary.changeset(itinerary, %{description: description}))
nil ->
multi

itinerary ->
Ecto.Multi.update(
multi,
:itinerary,
Itinerary.changeset(itinerary, %{description: description})
)
end
end)

Expand All @@ -45,5 +55,4 @@ defmodule JourniPlan.Itineraries.Projectors.Itinerary do
itinerary -> Ecto.Multi.delete(multi, :itinerary, itinerary)
end
end)

end
24 changes: 19 additions & 5 deletions lib/journi_plan/itineraries/projectors/journal_entry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule JourniPlan.Itineraries.Projectors.JournalEntry do

alias JourniPlan.Repo

import JourniPlan.Utils.UUID
import JourniPlan.Utils.UUID

alias JourniPlan.Itineraries.Events.{
JournalEntryCreated,
Expand Down Expand Up @@ -38,15 +38,29 @@ defmodule JourniPlan.Itineraries.Projectors.JournalEntry do

project(%JournalEntryTitleUpdated{uuid: uuid, title: title}, _, fn multi ->
case Repo.get(JournalEntry, uuid) do
nil -> multi
journal_entry -> Ecto.Multi.update(multi, :journal_entry, JournalEntry.changeset(journal_entry, %{title: title}))
nil ->
multi

journal_entry ->
Ecto.Multi.update(
multi,
:journal_entry,
JournalEntry.changeset(journal_entry, %{title: title})
)
end
end)

project(%JournalEntryBodyUpdated{uuid: uuid, body: body}, _, fn multi ->
case Repo.get(JournalEntry, uuid) do
nil -> multi
journal_entry -> Ecto.Multi.update(multi, :journal_entry, JournalEntry.changeset(journal_entry, %{body: body}))
nil ->
multi

journal_entry ->
Ecto.Multi.update(
multi,
:journal_entry,
JournalEntry.changeset(journal_entry, %{body: body})
)
end
end)

Expand Down
1 change: 0 additions & 1 deletion lib/journi_plan/release.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,4 @@ defmodule JourniPlan.Release do
defp load_app do
Application.load(@app)
end

end
6 changes: 5 additions & 1 deletion lib/journi_plan/utils/changeset.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ defmodule JourniPlan.Utils.Changeset do

def foreign_key_exists(changes, projection, projection_id_col, field) do
foreign_key = Changeset.get_field(changes, field)

case foreign_key do
nil ->
changes

_ ->
valid = Repo.exists?(from p in projection, where: field(p, ^projection_id_col) == ^foreign_key)
valid =
Repo.exists?(from p in projection, where: field(p, ^projection_id_col) == ^foreign_key)

case valid do
false -> Changeset.add_error(changes, field, "should exist")
true -> changes
Expand Down
4 changes: 2 additions & 2 deletions lib/journi_plan/utils/timex.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule JourniPlan.TimexUtils do
def parse_to_datetime!(time) when is_binary(time) do
time
|> Timex.parse!("%Y-%m-%dT%H:%M", :strftime)
|> Timex.to_datetime("Etc/UTC")
|> Timex.parse!("%Y-%m-%dT%H:%M", :strftime)
|> Timex.to_datetime("Etc/UTC")
end
end
Loading

0 comments on commit f1d119a

Please sign in to comment.