Skip to content
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

Prod deployment #195

Merged
merged 6 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions lib/dbservice/schools.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ defmodule Dbservice.Schools do

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 @@ -134,4 +136,48 @@ defmodule Dbservice.Schools do

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
6 changes: 5 additions & 1 deletion lib/dbservice/schools/school.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule Dbservice.Schools.School do

alias Dbservice.Groups.Group
alias Dbservice.EnrollmentRecords.EnrollmentRecord
alias Dbservice.Users.User

schema "school" do
field :code, :string
Expand All @@ -29,6 +30,8 @@ defmodule Dbservice.Schools.School do
foreign_key: :group_id,
where: [group_type: "school"]

belongs_to(:user, User)

timestamps()
end

Expand All @@ -49,7 +52,8 @@ defmodule Dbservice.Schools.School do
:block_code,
:block_name,
:board,
:board_medium
:board_medium,
:user_id
])
|> validate_required([:code, :name])
end
Expand Down
2 changes: 2 additions & 0 deletions lib/dbservice/users/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule Dbservice.Users.User do
alias Dbservice.Profiles.UserProfile
alias Dbservice.Groups.Group
alias Dbservice.EnrollmentRecords.EnrollmentRecord
alias Dbservice.Schools.School

schema "user" do
field(:first_name, :string)
Expand All @@ -36,6 +37,7 @@ defmodule Dbservice.Users.User do
has_one(:user_profile, UserProfile)
has_many(:enrollment_record, EnrollmentRecord)
many_to_many(:group, Group, join_through: "group_user", on_replace: :delete)
has_one(:school, School)
end

@doc false
Expand Down
30 changes: 30 additions & 0 deletions lib/dbservice_web/controllers/school_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule DbserviceWeb.SchoolController do
alias Dbservice.Repo
alias Dbservice.Schools
alias Dbservice.Schools.School
alias Dbservice.Users

action_fallback DbserviceWeb.FallbackController

Expand Down Expand Up @@ -148,4 +149,33 @@ defmodule DbserviceWeb.SchoolController do
|> render("show.json", school: school)
end
end

def create_school_with_user(conn, params) do
case Schools.get_school_by_code(params["code"]) do
nil ->
create_school_and_user(conn, params)

existing_school ->
update_existing_school_with_user(conn, existing_school, params)
end
end

defp create_school_and_user(conn, params) do
with {:ok, %School{} = school} <- Schools.create_school_with_user(params) do
conn
|> put_status(:created)
|> render("show.json", school: school)
end
end

defp update_existing_school_with_user(conn, existing_school, params) do
user = Users.get_user!(existing_school.user_id)

with {:ok, %School{} = school} <-
Schools.update_school_with_user(existing_school, user, params) do
conn
|> put_status(:ok)
|> render("show.json", school: school)
end
end
end
5 changes: 4 additions & 1 deletion lib/dbservice_web/controllers/teacher_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,10 @@ defmodule DbserviceWeb.TeacherController do
end

defp update_existing_teacher_with_user(conn, existing_teacher, params) do
with {:ok, %Teacher{} = teacher} <- Users.update_teacher_with_user(existing_teacher, params) do
user = Users.get_user!(existing_teacher.user_id)

with {:ok, %Teacher{} = teacher} <-
Users.update_teacher_with_user(existing_teacher, user, params) do
conn
|> put_status(:ok)
|> render("show.json", teacher: teacher)
Expand Down
1 change: 1 addition & 0 deletions lib/dbservice_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ defmodule DbserviceWeb.Router do
resources("/status", StatusController, except: [:new, :edit])
patch("/enrolled", StudentController, :enrolled)
resources("/school-batch", SchoolBatchController, except: [:new, :edit])
post("/school-with-user", SchoolController, :create_school_with_user)

def swagger_info do
source(["config/.env", "config/.env"])
Expand Down
4 changes: 3 additions & 1 deletion lib/dbservice_web/swagger_schemas/school.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ defmodule DbserviceWeb.SwaggerSchema.School do
block_name(:string, "Block Name")
board(:string, "Board")
board_medium(:string, "Medium")
user_id(:integer, "User ID of a school")
end

example(%{
Expand All @@ -40,7 +41,8 @@ defmodule DbserviceWeb.SwaggerSchema.School do
district: "NORTH WEST DELHI",
block_code: "DOEAIDED",
board: "CBSE",
board_medium: "en"
board_medium: "en",
user_id: 1
})
end
}
Expand Down
8 changes: 7 additions & 1 deletion lib/dbservice_web/views/school_view.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
defmodule DbserviceWeb.SchoolView do
use DbserviceWeb, :view
alias DbserviceWeb.SchoolView
alias DbserviceWeb.UserView
alias Dbservice.Repo

def render("index.json", %{school: school}) do
render_many(school, SchoolView, "school.json")
Expand All @@ -11,6 +13,8 @@ defmodule DbserviceWeb.SchoolView do
end

def render("school.json", %{school: school}) do
school = Repo.preload(school, :user)

%{
id: school.id,
code: school.code,
Expand All @@ -26,7 +30,9 @@ defmodule DbserviceWeb.SchoolView do
block_code: school.block_code,
block_name: school.block_name,
board: school.board,
board_medium: school.board_medium
board_medium: school.board_medium,
user_id: school.user_id,
user: render_one(school.user, UserView, "user.json")
}
end
end
5 changes: 2 additions & 3 deletions lib/dbservice_web/views/teacher_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ defmodule DbserviceWeb.TeacherView do
use DbserviceWeb, :view
alias DbserviceWeb.TeacherView
alias DbserviceWeb.UserView
alias DbserviceWeb.SubjectView
alias Dbservice.Repo

def render("index.json", %{teacher: teacher}) do
Expand All @@ -24,7 +23,7 @@ defmodule DbserviceWeb.TeacherView do
id: teacher.id,
designation: teacher.designation,
teacher_id: teacher.teacher_id,
subject: render_one(teacher.subject, SubjectView, "subject.json"),
subject_id: teacher.subject_id,
user: render_one(teacher.user, UserView, "user.json")
}
end
Expand All @@ -34,7 +33,7 @@ defmodule DbserviceWeb.TeacherView do
id: teacher.id,
designation: teacher.designation,
teacher_id: teacher.teacher_id,
subject: render_one(teacher.subject, SubjectView, "subject.json"),
subject_id: teacher.subject_id,
user: render_one(teacher.user, UserView, "user.json")
}
end
Expand Down
9 changes: 9 additions & 0 deletions priv/repo/migrations/20240711052952_add_user_id_to_school.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule Dbservice.Repo.Migrations.AddUserIdToSchool do
use Ecto.Migration

def change do
alter table(:school) do
add :user_id, references(:user, on_delete: :nothing)
end
end
end
Loading