-
Notifications
You must be signed in to change notification settings - Fork 0
feat: guardian and guardian_ninja schemas #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
GuilhermePSF
wants to merge
33
commits into
main
Choose a base branch
from
gui/guardian-schemas
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
8edf113
chore: merge ninja and seeding modules
GuilhermePSF fb61fba
fix: ninja context
GuilhermePSF e1fe312
feat: add guardians context
GuilhermePSF 3cc247f
feat: add guardian schema
GuilhermePSF c3b16fc
feat: guardian migration
GuilhermePSF 9c6eb3a
test: add guardian tests and fixtures
GuilhermePSF 9f07efd
chore: add faker
GuilhermePSF 09dac51
chore: format
GuilhermePSF fbb6be1
feat: highly improve seeding to better represent users
GuilhermePSF 8fccd78
feat: guardian seeding
GuilhermePSF f6316ad
fix: typo
GuilhermePSF f44fef1
feat: guardian ninja relation schema
GuilhermePSF a90916f
feat: guardian ninja relation migration
GuilhermePSF 3eba2ca
feat: guardian ninja relation context
GuilhermePSF cad5cff
feat: guardian ninja relation test and fixtures
GuilhermePSF 3f9da8d
feat: create index and define references
GuilhermePSF 8a82228
feat: guardian ninja seeding
GuilhermePSF 09dfbdb
chore: format
GuilhermePSF a815a22
refactor: improve odds
GuilhermePSF 8c025b0
feat: get guardians by ninja id and ninjas by guardian id.
GuilhermePSF e992cb8
feat: add guardian_id field to users and fix migration order
GuilhermePSF 803c1c3
feat: remake account seeding to better suit the desired db structure
GuilhermePSF a1ecf67
Merge branch 'main' into gui/guardian-schemas
GuilhermePSF 6da2e3c
fix: remove fields from ninja
GuilhermePSF f02866e
fix: merge guardian and guardian_ninjas contexts
GuilhermePSF 8ff74de
fix: guardian_ninja tests and fixtures
GuilhermePSF c538736
refactor: make a guardian_ninja table to e created when creating a ni…
GuilhermePSF f71d846
feat: add one-to-one association between User and Guardian
GuilhermePSF 5e0978a
fix: make guardian id optional in user
GuilhermePSF 8d6a428
chore: merge main
GuilhermePSF 6d8e056
chore: format
GuilhermePSF 4dd57e0
fix: file directory accordingly to its context
GuilhermePSF b11ab15
Merge branch 'main' into gui/guardian-schemas
GuilhermePSF File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| defmodule Katana.Guardians do | ||
| @moduledoc """ | ||
| The Guardians context, including links between Guardians and Ninjas. | ||
| """ | ||
|
|
||
| import Ecto.Query, warn: false | ||
| alias Katana.Repo | ||
|
|
||
| alias Katana.Guardians.Guardian | ||
| alias Katana.GuardiansNinjas.GuardianNinja | ||
| alias Katana.Ninjas.Ninja | ||
|
|
||
| # -------------------- | ||
| # Guardians CRUD | ||
| # -------------------- | ||
|
|
||
| @doc """ | ||
| Returns the list of guardians. | ||
| """ | ||
| def list_guardians do | ||
| Repo.all(Guardian) | ||
| end | ||
|
|
||
| @doc """ | ||
| Gets a single guardian. | ||
| """ | ||
| def get_guardian!(id), do: Repo.get!(Guardian, id) | ||
|
|
||
| @doc """ | ||
| Creates a guardian. | ||
| """ | ||
| def create_guardian(attrs \\ %{}) do | ||
| %Guardian{} | ||
| |> Guardian.changeset(attrs) | ||
| |> Repo.insert() | ||
| end | ||
|
|
||
| @doc """ | ||
| Updates a guardian. | ||
| """ | ||
| def update_guardian(%Guardian{} = guardian, attrs) do | ||
| guardian | ||
| |> Guardian.changeset(attrs) | ||
| |> Repo.update() | ||
| end | ||
|
|
||
| @doc """ | ||
| Deletes a guardian. | ||
| """ | ||
| def delete_guardian(%Guardian{} = guardian) do | ||
| Repo.delete(guardian) | ||
| end | ||
|
|
||
| @doc """ | ||
| Returns an `%Ecto.Changeset{}` for tracking guardian changes. | ||
| """ | ||
| def change_guardian(%Guardian{} = guardian, attrs \\ %{}) do | ||
| Guardian.changeset(guardian, attrs) | ||
| end | ||
|
|
||
| # -------------------- | ||
| # GuardianNinja join table CRUD | ||
| # -------------------- | ||
|
|
||
| @doc """ | ||
| Returns the list of guardian_ninjas. | ||
| """ | ||
| def list_guardian_ninjas do | ||
| Repo.all(GuardianNinja) | ||
| end | ||
|
|
||
| @doc """ | ||
| Gets a single guardian_ninja. | ||
| """ | ||
| def get_guardian_ninja!(id), do: Repo.get!(GuardianNinja, id) | ||
|
|
||
| @doc """ | ||
| Creates a guardian_ninja link. | ||
| """ | ||
| def create_guardian_ninja(attrs \\ %{}) do | ||
| %GuardianNinja{} | ||
| |> GuardianNinja.changeset(attrs) | ||
| |> Repo.insert() | ||
| end | ||
|
|
||
| @doc """ | ||
| Updates a guardian_ninja link. | ||
| """ | ||
| def update_guardian_ninja(%GuardianNinja{} = guardian_ninja, attrs) do | ||
| guardian_ninja | ||
| |> GuardianNinja.changeset(attrs) | ||
| |> Repo.update() | ||
| end | ||
|
|
||
| @doc """ | ||
| Deletes a guardian_ninja link. | ||
| """ | ||
| def delete_guardian_ninja(%GuardianNinja{} = guardian_ninja) do | ||
| Repo.delete(guardian_ninja) | ||
| end | ||
|
|
||
| @doc """ | ||
| Returns an `%Ecto.Changeset{}` for tracking guardian_ninja changes. | ||
| """ | ||
| def change_guardian_ninja(%GuardianNinja{} = guardian_ninja, attrs \\ %{}) do | ||
| GuardianNinja.changeset(guardian_ninja, attrs) | ||
| end | ||
|
|
||
| # -------------------- | ||
| # Custom Queries | ||
| # -------------------- | ||
|
|
||
| @doc """ | ||
| Returns all Guardians linked to a given Ninja id. | ||
| """ | ||
| def get_guardians_for_ninja(ninja_id) do | ||
| query = | ||
| from gn in GuardianNinja, | ||
| where: gn.ninja_id == ^ninja_id, | ||
| join: g in Guardian, | ||
| on: g.id == gn.guardian_id, | ||
| select: g | ||
|
|
||
| Repo.all(query) | ||
| end | ||
|
|
||
| @doc """ | ||
| Returns all Ninjas linked to a given Guardian id. | ||
| """ | ||
| def get_ninjas_for_guardian(guardian_id) do | ||
| query = | ||
| from gn in GuardianNinja, | ||
| where: gn.guardian_id == ^guardian_id, | ||
| join: n in Ninja, | ||
| on: n.id == gn.ninja_id, | ||
| select: n | ||
|
|
||
| Repo.all(query) | ||
| end | ||
|
|
||
| @doc """ | ||
| Creates a link between a Guardian and a Ninja. | ||
| """ | ||
| def link_guardian_and_ninja(guardian_id, ninja_id) do | ||
| %GuardianNinja{} | ||
| |> GuardianNinja.changeset(%{guardian_id: guardian_id, ninja_id: ninja_id}) | ||
| |> Repo.insert() | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| defmodule Katana.Guardians.Guardian do | ||
| use Ecto.Schema | ||
| import Ecto.Changeset | ||
|
|
||
| @primary_key {:id, :binary_id, autogenerate: true} | ||
| @foreign_key_type :binary_id | ||
| schema "guardians" do | ||
| field :phone, :string | ||
|
|
||
| belongs_to :user, Katana.Accounts.User, type: :binary_id | ||
|
|
||
| timestamps(type: :utc_datetime) | ||
| end | ||
|
|
||
| def changeset(guardian, attrs) do | ||
| guardian | ||
| |> cast(attrs, [:phone, :user_id]) | ||
| |> validate_required([:phone, :user_id]) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| defmodule Katana.GuardiansNinjas.GuardianNinja do | ||
| use Ecto.Schema | ||
| import Ecto.Changeset | ||
|
|
||
| @primary_key {:id, :binary_id, autogenerate: true} | ||
| @foreign_key_type :binary_id | ||
| schema "guardian_ninja" do | ||
| field :ninja_id, :binary_id | ||
| field :guardian_id, :binary_id | ||
|
|
||
| timestamps(type: :utc_datetime) | ||
| end | ||
|
|
||
| @doc false | ||
| def changeset(guardian_ninja, attrs) do | ||
| guardian_ninja | ||
| |> cast(attrs, [:ninja_id, :guardian_id]) | ||
| |> validate_required([:ninja_id, :guardian_id]) | ||
| end | ||
| end | ||
GuilhermePSF marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| defmodule Katana.GuardiansNinjas.GuardianNinja do | ||
| use Ecto.Schema | ||
| import Ecto.Changeset | ||
|
|
||
| @primary_key {:id, :binary_id, autogenerate: true} | ||
| @foreign_key_type :binary_id | ||
| schema "guardian_ninja" do | ||
GuilhermePSF marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| field :ninja_id, :binary_id | ||
| field :guardian_id, :binary_id | ||
|
|
||
| timestamps(type: :utc_datetime) | ||
| end | ||
|
|
||
| @doc false | ||
| def changeset(guardian_ninja, attrs) do | ||
| guardian_ninja | ||
| |> cast(attrs, [:ninja_id, :guardian_id]) | ||
| |> validate_required([:ninja_id, :guardian_id]) | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| defmodule Katana.Repo.Migrations.CreateGuardians do | ||
| use Ecto.Migration | ||
|
|
||
| def change do | ||
| create table(:guardians, primary_key: false) do | ||
| add :id, :binary_id, primary_key: true | ||
| add :phone, :string | ||
| add :user_id, references(:users, type: :binary_id, on_delete: :delete_all), null: false | ||
|
|
||
| timestamps(type: :utc_datetime) | ||
| end | ||
|
|
||
| create unique_index(:guardians, [:user_id]) | ||
| end | ||
| end |
19 changes: 19 additions & 0 deletions
19
priv/repo/migrations/20250821015859_create_guardian_ninja.exs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| defmodule Katana.Repo.Migrations.CreateGuardianNinja do | ||
| use Ecto.Migration | ||
|
|
||
| def change do | ||
| create table(:guardian_ninja, primary_key: false) do | ||
| add :id, :binary_id, primary_key: true | ||
| add :ninja_id, references(:ninjas, type: :binary_id, on_delete: :delete_all), null: false | ||
|
|
||
| add :guardian_id, references(:guardians, type: :binary_id, on_delete: :delete_all), | ||
| null: false | ||
|
|
||
| timestamps(type: :utc_datetime) | ||
| end | ||
|
|
||
| create index(:guardian_ninja, [:ninja_id]) | ||
| create index(:guardian_ninja, [:guardian_id]) | ||
| create unique_index(:guardian_ninja, [:ninja_id, :guardian_id]) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm confused. This file seems to be duplicated 🤔