-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: created
StudentCycleInfo
schema in new StudentsCycleInfo
c…
…ontext
- Loading branch information
Showing
5 changed files
with
353 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,104 @@ | ||
defmodule Lanttern.StudentsCycleInfo do | ||
@moduledoc """ | ||
The StudentsCycleInfo context. | ||
""" | ||
|
||
import Ecto.Query, warn: false | ||
alias Lanttern.Repo | ||
|
||
alias Lanttern.StudentsCycleInfo.StudentCycleInfo | ||
|
||
@doc """ | ||
Returns the list of students_cycle_info. | ||
## Examples | ||
iex> list_students_cycle_info() | ||
[%StudentCycleInfo{}, ...] | ||
""" | ||
def list_students_cycle_info do | ||
Repo.all(StudentCycleInfo) | ||
end | ||
|
||
@doc """ | ||
Gets a single student_cycle_info. | ||
Raises `Ecto.NoResultsError` if the Student cycle info does not exist. | ||
## Examples | ||
iex> get_student_cycle_info!(123) | ||
%StudentCycleInfo{} | ||
iex> get_student_cycle_info!(456) | ||
** (Ecto.NoResultsError) | ||
""" | ||
def get_student_cycle_info!(id), do: Repo.get!(StudentCycleInfo, id) | ||
|
||
@doc """ | ||
Creates a student_cycle_info. | ||
## Examples | ||
iex> create_student_cycle_info(%{field: value}) | ||
{:ok, %StudentCycleInfo{}} | ||
iex> create_student_cycle_info(%{field: bad_value}) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def create_student_cycle_info(attrs \\ %{}) do | ||
%StudentCycleInfo{} | ||
|> StudentCycleInfo.changeset(attrs) | ||
|> Repo.insert() | ||
end | ||
|
||
@doc """ | ||
Updates a student_cycle_info. | ||
## Examples | ||
iex> update_student_cycle_info(student_cycle_info, %{field: new_value}) | ||
{:ok, %StudentCycleInfo{}} | ||
iex> update_student_cycle_info(student_cycle_info, %{field: bad_value}) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def update_student_cycle_info(%StudentCycleInfo{} = student_cycle_info, attrs) do | ||
student_cycle_info | ||
|> StudentCycleInfo.changeset(attrs) | ||
|> Repo.update() | ||
end | ||
|
||
@doc """ | ||
Deletes a student_cycle_info. | ||
## Examples | ||
iex> delete_student_cycle_info(student_cycle_info) | ||
{:ok, %StudentCycleInfo{}} | ||
iex> delete_student_cycle_info(student_cycle_info) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def delete_student_cycle_info(%StudentCycleInfo{} = student_cycle_info) do | ||
Repo.delete(student_cycle_info) | ||
end | ||
|
||
@doc """ | ||
Returns an `%Ecto.Changeset{}` for tracking student_cycle_info changes. | ||
## Examples | ||
iex> change_student_cycle_info(student_cycle_info) | ||
%Ecto.Changeset{data: %StudentCycleInfo{}} | ||
""" | ||
def change_student_cycle_info(%StudentCycleInfo{} = student_cycle_info, attrs \\ %{}) do | ||
StudentCycleInfo.changeset(student_cycle_info, attrs) | ||
end | ||
end |
This file contains 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,65 @@ | ||
defmodule Lanttern.StudentsCycleInfo.StudentCycleInfo do | ||
@moduledoc """ | ||
The `StudentCycleInfo` schema | ||
""" | ||
|
||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
import LantternWeb.Gettext | ||
|
||
alias Lanttern.Schools.Cycle | ||
alias Lanttern.Schools.School | ||
alias Lanttern.Schools.Student | ||
|
||
@type t :: %__MODULE__{ | ||
id: pos_integer(), | ||
school_info: String.t(), | ||
family_info: String.t(), | ||
profile_picture_url: String.t(), | ||
student: Student.t(), | ||
student_id: pos_integer(), | ||
cycle: Cycle.t(), | ||
cycle_id: pos_integer(), | ||
school: School.t(), | ||
school_id: pos_integer(), | ||
inserted_at: DateTime.t(), | ||
updated_at: DateTime.t() | ||
} | ||
|
||
schema "students_cycle_info" do | ||
field :school_info, :string | ||
field :family_info, :string | ||
field :profile_picture_url, :string | ||
|
||
belongs_to :student, Student | ||
belongs_to :cycle, Cycle | ||
belongs_to :school, School | ||
|
||
timestamps() | ||
end | ||
|
||
@doc false | ||
def changeset(student_cycle_info, attrs) do | ||
student_cycle_info | ||
|> cast(attrs, [ | ||
:school_info, | ||
:family_info, | ||
:profile_picture_url, | ||
:student_id, | ||
:cycle_id, | ||
:school_id | ||
]) | ||
|> validate_required([:student_id, :cycle_id, :school_id]) | ||
|> foreign_key_constraint( | ||
:student_id, | ||
name: :students_cycle_info_student_id_fkey, | ||
message: gettext("Check the student and school relationship") | ||
) | ||
|> foreign_key_constraint( | ||
:cycle_id, | ||
name: :students_cycle_info_cycle_id_fkey, | ||
message: gettext("Check the cycle and school relationship") | ||
) | ||
end | ||
end |
30 changes: 30 additions & 0 deletions
30
priv/repo/migrations/20241223144343_create_students_cycle_info.exs
This file contains 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,30 @@ | ||
defmodule Lanttern.Repo.Migrations.CreateStudentsCycleInfo do | ||
use Ecto.Migration | ||
|
||
def change do | ||
# we'll use unique constraints to allow composite foreign keys. | ||
# this guarantees, in the database level, that student and cycle | ||
# belong to the same school | ||
|
||
create table(:students_cycle_info) do | ||
add :school_info, :text | ||
add :family_info, :text | ||
add :profile_picture_url, :text | ||
|
||
add :student_id, references(:students, with: [school_id: :school_id], on_delete: :nothing), | ||
required: true | ||
|
||
add :cycle_id, | ||
references(:school_cycles, with: [school_id: :school_id], on_delete: :nothing), | ||
required: true | ||
|
||
add :school_id, references(:schools, on_delete: :nothing), required: true | ||
|
||
timestamps() | ||
end | ||
|
||
create index(:students_cycle_info, [:student_id]) | ||
create unique_index(:students_cycle_info, [:cycle_id, :student_id]) | ||
create index(:students_cycle_info, [:school_id]) | ||
end | ||
end |
This file contains 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,104 @@ | ||
defmodule Lanttern.StudentsCycleInfoTest do | ||
use Lanttern.DataCase | ||
|
||
alias Lanttern.StudentsCycleInfo | ||
|
||
describe "students_cycle_info" do | ||
alias Lanttern.StudentsCycleInfo.StudentCycleInfo | ||
|
||
import Lanttern.StudentsCycleInfoFixtures | ||
alias Lanttern.SchoolsFixtures | ||
|
||
@invalid_attrs %{school_info: nil, family_info: nil, profile_picture_url: nil} | ||
|
||
test "list_students_cycle_info/0 returns all students_cycle_info" do | ||
student_cycle_info = student_cycle_info_fixture() | ||
assert StudentsCycleInfo.list_students_cycle_info() == [student_cycle_info] | ||
end | ||
|
||
test "get_student_cycle_info!/1 returns the student_cycle_info with given id" do | ||
student_cycle_info = student_cycle_info_fixture() | ||
|
||
assert StudentsCycleInfo.get_student_cycle_info!(student_cycle_info.id) == | ||
student_cycle_info | ||
end | ||
|
||
test "create_student_cycle_info/1 with valid data creates a student_cycle_info" do | ||
school = SchoolsFixtures.school_fixture() | ||
student = SchoolsFixtures.student_fixture(%{school_id: school.id}) | ||
cycle = SchoolsFixtures.cycle_fixture(%{school_id: school.id}) | ||
|
||
valid_attrs = %{ | ||
school_info: "some school_info", | ||
family_info: "some family_info", | ||
profile_picture_url: "some profile_picture", | ||
school_id: school.id, | ||
student_id: student.id, | ||
cycle_id: cycle.id | ||
} | ||
|
||
assert {:ok, %StudentCycleInfo{} = student_cycle_info} = | ||
StudentsCycleInfo.create_student_cycle_info(valid_attrs) | ||
|
||
assert student_cycle_info.school_info == "some school_info" | ||
assert student_cycle_info.family_info == "some family_info" | ||
assert student_cycle_info.profile_picture_url == "some profile_picture" | ||
assert student_cycle_info.school_id == school.id | ||
assert student_cycle_info.student_id == student.id | ||
assert student_cycle_info.cycle_id == cycle.id | ||
end | ||
|
||
test "create_student_cycle_info/1 with invalid data returns error changeset" do | ||
assert {:error, %Ecto.Changeset{}} = | ||
StudentsCycleInfo.create_student_cycle_info(@invalid_attrs) | ||
end | ||
|
||
test "update_student_cycle_info/2 with valid data updates the student_cycle_info" do | ||
student_cycle_info = student_cycle_info_fixture() | ||
|
||
update_attrs = %{ | ||
school_info: "some updated school_info", | ||
family_info: "some updated family_info", | ||
profile_picture_url: "some updated profile_picture" | ||
} | ||
|
||
assert {:ok, %StudentCycleInfo{} = student_cycle_info} = | ||
StudentsCycleInfo.update_student_cycle_info(student_cycle_info, update_attrs) | ||
|
||
assert student_cycle_info.school_info == "some updated school_info" | ||
assert student_cycle_info.family_info == "some updated family_info" | ||
assert student_cycle_info.profile_picture_url == "some updated profile_picture" | ||
end | ||
|
||
test "update_student_cycle_info/2 with invalid data returns error changeset" do | ||
student_cycle_info = student_cycle_info_fixture() | ||
invalid_student = SchoolsFixtures.student_fixture() | ||
invalid_cycle = SchoolsFixtures.cycle_fixture() | ||
|
||
assert {:error, %Ecto.Changeset{}} = | ||
StudentsCycleInfo.update_student_cycle_info(student_cycle_info, %{ | ||
student_id: invalid_student.id, | ||
cycle_id: invalid_cycle.id | ||
}) | ||
|
||
assert student_cycle_info == | ||
StudentsCycleInfo.get_student_cycle_info!(student_cycle_info.id) | ||
end | ||
|
||
test "delete_student_cycle_info/1 deletes the student_cycle_info" do | ||
student_cycle_info = student_cycle_info_fixture() | ||
|
||
assert {:ok, %StudentCycleInfo{}} = | ||
StudentsCycleInfo.delete_student_cycle_info(student_cycle_info) | ||
|
||
assert_raise Ecto.NoResultsError, fn -> | ||
StudentsCycleInfo.get_student_cycle_info!(student_cycle_info.id) | ||
end | ||
end | ||
|
||
test "change_student_cycle_info/1 returns a student_cycle_info changeset" do | ||
student_cycle_info = student_cycle_info_fixture() | ||
assert %Ecto.Changeset{} = StudentsCycleInfo.change_student_cycle_info(student_cycle_info) | ||
end | ||
end | ||
end |
This file contains 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,50 @@ | ||
defmodule Lanttern.StudentsCycleInfoFixtures do | ||
@moduledoc """ | ||
This module defines test helpers for creating | ||
entities via the `Lanttern.StudentsCycleInfo` context. | ||
""" | ||
|
||
alias Lanttern.SchoolsFixtures | ||
|
||
@doc """ | ||
Generate a student_cycle_info. | ||
""" | ||
def student_cycle_info_fixture(attrs \\ %{}) do | ||
{:ok, student_cycle_info} = | ||
attrs | ||
|> maybe_inject_school_id() | ||
|> maybe_inject_student_id() | ||
|> maybe_inject_cycle_id() | ||
|> Enum.into(%{ | ||
family_info: "some family_info", | ||
profile_picture: "some profile_picture", | ||
school_info: "some school_info" | ||
}) | ||
|> Lanttern.StudentsCycleInfo.create_student_cycle_info() | ||
|
||
student_cycle_info | ||
end | ||
|
||
# helpers | ||
|
||
defp maybe_inject_school_id(%{school_id: _} = attrs), do: attrs | ||
|
||
defp maybe_inject_school_id(attrs) do | ||
attrs | ||
|> Map.put(:school_id, SchoolsFixtures.school_fixture().id) | ||
end | ||
|
||
defp maybe_inject_student_id(%{student_id: _} = attrs), do: attrs | ||
|
||
defp maybe_inject_student_id(%{school_id: school_id} = attrs) do | ||
attrs | ||
|> Map.put(:student_id, SchoolsFixtures.student_fixture(%{school_id: school_id}).id) | ||
end | ||
|
||
defp maybe_inject_cycle_id(%{cycle_id: _} = attrs), do: attrs | ||
|
||
defp maybe_inject_cycle_id(%{school_id: school_id} = attrs) do | ||
attrs | ||
|> Map.put(:cycle_id, SchoolsFixtures.cycle_fixture(%{school_id: school_id}).id) | ||
end | ||
end |