Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…to feat/render-session-with-session-occurrences
  • Loading branch information
Bahugunajii committed Jul 22, 2024
2 parents f6f28ef + 1423a19 commit c381579
Show file tree
Hide file tree
Showing 38 changed files with 1,098 additions and 53 deletions.
18 changes: 18 additions & 0 deletions lib/dbservice/chapters.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@ defmodule Dbservice.Chapters do
"""
def get_chapter!(id), do: Repo.get!(Chapter, id)

@doc """
Gets a chapter by code.
Raises `Ecto.NoResultsError` if the School does not exist.
## Examples
iex> get_chapter_by_code(12)
%School{}
iex> get_chapter_by_code(12)
** (Ecto.NoResultsError)
"""
def get_chapter_by_code(code) do
Repo.get_by(Chapter, code: code)
end

@doc """
Creates a chapter.
## Examples
Expand Down
18 changes: 18 additions & 0 deletions lib/dbservice/curriculums.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@ defmodule Dbservice.Curriculums do
"""
def get_curriculum!(id), do: Repo.get!(Curriculum, id)

@doc """
Gets a curriculum by name.
Raises `Ecto.NoResultsError` if the School does not exist.
## Examples
iex> get_curriculum_by_name(Sankalp)
%School{}
iex> get_curriculum_by_name(Sankalp)
** (Ecto.NoResultsError)
"""
def get_curriculum_by_name(name) do
Repo.get_by(Curriculum, name: name)
end

@doc """
Creates a curriculum.
## Examples
Expand Down
12 changes: 12 additions & 0 deletions lib/dbservice/enrollment_records.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule Dbservice.EnrollmentRecords do
"""

import Ecto.Query, warn: false
alias Dbservice.Utils.Util
alias Dbservice.Repo

alias Dbservice.EnrollmentRecords.EnrollmentRecord
Expand Down Expand Up @@ -131,4 +132,15 @@ defmodule Dbservice.EnrollmentRecords do
def change_enrollment_record(%EnrollmentRecord{} = enrollment_record, attrs \\ %{}) do
EnrollmentRecord.changeset(enrollment_record, attrs)
end

@doc """
Gets a list of Enrollment Record based on the given parameters.
Returns empty list - [] if no Enrollment record with the given parameters is found.
"""

def get_enrollment_record_by_params(params) when is_map(params) do
query = from er in EnrollmentRecord, where: ^Util.build_conditions(params), select: er

Repo.all(query)
end
end
29 changes: 29 additions & 0 deletions lib/dbservice/grades.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule Dbservice.Grades do
"""

import Ecto.Query, warn: false
alias Dbservice.Utils.Util
alias Dbservice.Repo

alias Dbservice.Grades.Grade
Expand All @@ -29,6 +30,24 @@ defmodule Dbservice.Grades do
"""
def get_grade!(id), do: Repo.get!(Grade, id)

@doc """
Gets a grade by number.
Raises `Ecto.NoResultsError` if the School does not exist.
## Examples
iex> get_grade_by_number(12)
%School{}
iex> get_grade_by_number(12)
** (Ecto.NoResultsError)
"""
def get_grade_by_number(number) do
Repo.get_by(Grade, number: number)
end

@doc """
Creates a grade.
## Examples
Expand Down Expand Up @@ -78,4 +97,14 @@ defmodule Dbservice.Grades do
def change_grade(%Grade{} = grade, attrs \\ %{}) do
Grade.changeset(grade, attrs)
end

@doc """
Gets a grade based on the given parameters.
Returns `nil` if no grade with the given parameters is found.
"""
def get_grade_by_params(params) when is_map(params) do
query = from g in Grade, where: ^Util.build_conditions(params), select: g

Repo.one(query)
end
end
18 changes: 18 additions & 0 deletions lib/dbservice/purposes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@ defmodule Dbservice.Purposes do
"""
def get_purpose!(id), do: Repo.get!(Purpose, id)

@doc """
Gets a purpose by name.
Raises `Ecto.NoResultsError` if the School does not exist.
## Examples
iex> get_purpose_by_name(12)
%School{}
iex> get_purpose_by_name(12)
** (Ecto.NoResultsError)
"""
def get_purpose_by_name(name) do
Repo.get_by(Purpose, name: name)
end

@doc """
Creates a purpose.
## Examples
Expand Down
18 changes: 18 additions & 0 deletions lib/dbservice/resources.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@ defmodule Dbservice.Resources do
"""
def get_resource!(id), do: Repo.get!(Resource, id)

@doc """
Gets a resource by name and sourceId.
Raises `Ecto.NoResultsError` if the School does not exist.
## Examples
iex> get_resource_by_name_and_source_id(12)
%School{}
iex> get_resource_by_name_and_source_id(12)
** (Ecto.NoResultsError)
"""
def get_resource_by_name_and_source_id(name, source_id) do
Repo.get_by(Resource, name: name, source_id: source_id)
end

@doc """
Creates a resource.
## Examples
Expand Down
117 changes: 117 additions & 0 deletions lib/dbservice/school_batches.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
defmodule Dbservice.SchoolBatches do
@moduledoc """
The SchoolBatches context.
"""

import Ecto.Query, warn: false
alias Dbservice.Repo

alias Dbservice.SchoolBatches.SchoolBatch

@doc """
Returns the list of school_batch.
## Examples
iex> list_school_batch()
[%SchoolBatch{}, ...]
"""
def list_school_batch do
Repo.all(SchoolBatch)
end

@doc """
Gets a single school_batch.
Raises `Ecto.NoResultsError` if the SchoolBatch does not exist.
## Examples
iex> get_school_batch!(123)
%SchoolBatch{}
iex> get_school_batch!(456)
** (Ecto.NoResultsError)
"""
def get_school_batch!(id), do: Repo.get!(SchoolBatch, id)

@doc """
Gets a school_batch based on school_id and batch_id.
Raises `Ecto.NoResultsError` if the SchoolBatch does not exist.
## Examples
iex> get_school_batch_by_school_id_and_batch_id(1, 2)
%SchoolBatch{}
iex> get_school_batch_by_school_id_and_batch_id(abc)
** (Ecto.NoResultsError)
"""
def get_school_batch_by_school_id_and_batch_id(school_id, batch_id) do
Repo.get_by(SchoolBatch, school_id: school_id, batch_id: batch_id)
end

@doc """
Creates a school_batch.
## Examples
iex> create_school_batch(%{field: value})
{:ok, %SchoolBatch{}}
iex> create_school_batch(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_school_batch(attrs \\ %{}) do
%SchoolBatch{}
|> SchoolBatch.changeset(attrs)
|> Repo.insert()
end

@doc """
Updates a school_batch.
## Examples
iex> update_school_batch(school_batch, %{field: new_value})
{:ok, %SchoolBatch{}}
iex> update_school_batch(school_batch, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_school_batch(%SchoolBatch{} = school_batch, attrs) do
school_batch
|> SchoolBatch.changeset(attrs)
|> Repo.update()
end

@doc """
Deletes a school_batch.
## Examples
iex> delete_school_batch(school_batch)
{:ok, %SchoolBatch{}}
iex> delete_school_batch(school_batch)
{:error, %Ecto.Changeset{}}
"""
def delete_school_batch(%SchoolBatch{} = school_batch) do
Repo.delete(school_batch)
end

@doc """
Returns an `%Ecto.Changeset{}` for tracking school_batch changes.
## Examples
iex> change_school_batch(school_batch)
%Ecto.Changeset{data: %SchoolBatch{}}
"""
def change_school_batch(%SchoolBatch{} = school_batch, attrs \\ %{}) do
SchoolBatch.changeset(school_batch, attrs)
end
end
25 changes: 25 additions & 0 deletions lib/dbservice/school_batches/school_batch.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
defmodule Dbservice.SchoolBatches.SchoolBatch do
@moduledoc false

use Ecto.Schema
alias Dbservice.Batches.Batch
alias Dbservice.Schools.School
import Ecto.Changeset

schema "school_batch" do
belongs_to :school, School
belongs_to :batch, Batch

timestamps()
end

@doc false
def changeset(school_batch, attrs) do
school_batch
|> cast(attrs, [
:school_id,
:batch_id
])
|> validate_required([:school_id, :batch_id])
end
end
57 changes: 57 additions & 0 deletions lib/dbservice/schools.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ defmodule Dbservice.Schools do
"""

import Ecto.Query, warn: false
alias Dbservice.Utils.Util
alias Dbservice.Repo

alias Dbservice.Groups.Group
alias Dbservice.Schools.School
alias Dbservice.Users.User
alias Dbservice.Schools

@doc """
Returns the list of school.
Expand Down Expand Up @@ -123,4 +126,58 @@ defmodule Dbservice.Schools do
def change_school(%School{} = school, attrs \\ %{}) do
School.changeset(school, attrs)
end

@doc """
Gets a list of schools based on the given parameters.
Returns empty list - [] if no school with the given parameters is found.
"""
def get_school_by_params(params) when is_map(params) do
query = from s in School, where: ^Util.build_conditions(params), select: s

Repo.all(query)
end

@doc """
Creates a user first and then the school.
## Examples
iex> create_school_with_user(%{field: value})
{:ok, %School{}}
iex> create_school_with_user(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_school_with_user(attrs \\ %{}) do
alias Dbservice.Users

with {:ok, %User{} = user} <- Users.create_user(attrs),
{:ok, %School{} = school} <-
Schools.create_school(Map.merge(attrs, %{"user_id" => user.id})) do
{:ok, school}
end
end

@doc """
Updates a user first and then the school.
## Examples
iex> update_school_with_user(%{field: value})
{:ok, %School{}}
iex> update_school_with_user(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_school_with_user(school, user, attrs \\ %{}) do
alias Dbservice.Users

with {:ok, %User{} = user} <- Users.update_user(user, attrs),
{:ok, %School{} = school} <-
Schools.update_school(school, Map.merge(attrs, %{"user_id" => user.id})) do
{:ok, school}
end
end
end
Loading

0 comments on commit c381579

Please sign in to comment.