Skip to content

Commit

Permalink
Merge pull request #194 from avantifellows/feat/add-user-id-to-school
Browse files Browse the repository at this point in the history
Feat: Add user_id to school
  • Loading branch information
Bahugunajii authored Jul 11, 2024
2 parents 995679d + c4eebac commit cb532c6
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 3 deletions.
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
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
3 changes: 2 additions & 1 deletion lib/dbservice_web/views/school_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ 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
}
end
end
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

0 comments on commit cb532c6

Please sign in to comment.