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

Removes current batch/grade/group #172

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
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
39 changes: 34 additions & 5 deletions fetch-data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,22 @@
# Install PostgreSQL client tools (only needed if not already installed)
# sudo apt-get install postgresql-client

# Production database credentials
# Environment (production or staging)
environment="production" # Change this to "staging" if needed

# Database credentials for production
production_db_host="xxx.rds.amazonaws.com"
production_db_name="xxx"
production_db_user="postgres"
production_db_password="xxx"
production_db_port="1357"

# Database credentials for staging
staging_db_host="xxx-staging.rds.amazonaws.com"
staging_db_name="xxx_staging"
staging_db_user="postgres"
staging_db_password="xxx"
staging_db_port="1357"

# Local database credentials
local_db_host="localhost"
Expand All @@ -18,6 +29,24 @@ local_db_password="postgres"
# Dump file name
dump_file="dump.sql"

# Set database credentials based on the environment
if [ "$environment" == "production" ]; then
db_host=$production_db_host
db_name=$production_db_name
db_user=$production_db_user
db_password=$production_db_password
db_port=$production_db_port
elif [ "$environment" == "staging" ]; then
db_host=$staging_db_host
db_name=$staging_db_name
db_user=$staging_db_user
db_password=$staging_db_password
db_port=$staging_db_port
else
echo "Error: Invalid environment specified. Use 'production' or 'staging'."
exit 1
fi

# Find the location of pg_dump and psql executables
pg_dump_path=$(which pg_dump)
psql_path=$(which psql)
Expand All @@ -26,11 +55,11 @@ psql_path=$(which psql)
echo "Removing existing tables from local database..."
PGPASSWORD="$local_db_password" "$psql_path" --host="$local_db_host" --port=5432 --username="$local_db_user" --dbname="$local_db_name" --command="DROP SCHEMA public CASCADE; CREATE SCHEMA public;"

# Fetch the data dump from the production database
echo "Fetching data dump from production database..."
PGPASSWORD="$production_db_password" "$pg_dump_path" --host="$production_db_host" --port=5432 --username="$production_db_user" --dbname="$production_db_name" --file="$dump_file"
# Fetch the data dump from the specified environment database
echo "Fetching data dump from $environment database..."
PGPASSWORD="$db_password" "$pg_dump_path" --host="$db_host" --port="$db_port" --username="$db_user" --dbname="$db_name" --file="$dump_file"
if [ $? -ne 0 ]; then
echo "Error: Failed to fetch data dump from production database"
echo "Error: Failed to fetch data dump from $environment database"
exit 1
fi

Expand Down
6 changes: 0 additions & 6 deletions lib/dbservice/profile/user_profile.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ defmodule Dbservice.Profiles.UserProfile do
alias Dbservice.Profiles.TeacherProfile

schema "user_profile" do
field(:current_grade, :string)
field(:current_program, :string)
field(:current_batch, :string)
field(:logged_in_atleast_once, :boolean)
field(:latest_session_accessed, :string)

Expand All @@ -26,9 +23,6 @@ defmodule Dbservice.Profiles.UserProfile do
user_profile
|> cast(attrs, [
:user_id,
:current_grade,
:current_program,
:current_batch,
:logged_in_atleast_once,
:latest_session_accessed
])
Expand Down
39 changes: 39 additions & 0 deletions lib/dbservice/user_profiles.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ defmodule Dbservice.Profiles do
"""
def get_user_profile!(id), do: Repo.get!(UserProfile, id)

@doc """
Gets a user profile by user ID.
Raises `Ecto.NoResultsError` if the UserProfile does not exist.
## Examples
iex> get_profile_by_user_id(1234)
%UserProfile{}
iex> get_profile_by_user_id(abc)
** (Ecto.NoResultsError)
"""
def get_profile_by_user_id(user_id) do
Repo.get_by(UserProfile, user_id: user_id)
end

@doc """
Creates a user profile.

Expand Down Expand Up @@ -133,6 +146,19 @@ defmodule Dbservice.Profiles do
"""
def get_student_profile!(id), do: Repo.get!(StudentProfile, id)

@doc """
Gets a student profile by student ID.
Raises `Ecto.NoResultsError` if the StudentProfile does not exist.
## Examples
iex> get_profile_by_student_id(1234)
%StudentProfile{}
iex> get_profile_by_student_id(abc)
** (Ecto.NoResultsError)
"""
def get_profile_by_student_id(student_id) do
Repo.get_by(StudentProfile, student_id: student_id)
end

@doc """
Creates a student profile.

Expand Down Expand Up @@ -279,6 +305,19 @@ defmodule Dbservice.Profiles do
"""
def get_teacher_profile!(id), do: Repo.get!(TeacherProfile, id)

@doc """
Gets a teacher profile by teacher ID.
Raises `Ecto.NoResultsError` if the TeacherProfile does not exist.
## Examples
iex> get_profile_by_teacher_id(1234)
%TeacherProfile{}
iex> get_profile_by_teacher_id(abc)
** (Ecto.NoResultsError)
"""
def get_profile_by_teacher_id(teacher_id) do
Repo.get_by(TeacherProfile, teacher_id: teacher_id)
end

@doc """
Creates a teacher profile.

Expand Down
28 changes: 27 additions & 1 deletion lib/dbservice_web/controllers/student_profile_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,36 @@ defmodule DbserviceWeb.StudentProfileController do
|> Map.put_new("user_id", user_id)
|> Map.put_new("student_fk", student_fk)

case Profiles.get_profile_by_student_id(updated_params["student_id"]) do
nil ->
create_new_profile(conn, updated_params)

existing_profile ->
update_existing_profile(conn, existing_profile, updated_params)
end
end

defp create_new_profile(conn, params) do
with {:ok, %StudentProfile{} = student_profile} <-
Profiles.create_student_profile_with_user_profile(updated_params) do
Profiles.create_student_profile_with_user_profile(params) do
conn
|> put_status(:created)
|> put_resp_header("location", Routes.student_profile_path(conn, :show, student_profile))
|> render("show.json", student_profile: student_profile)
end
end

defp update_existing_profile(conn, existing_profile, params) do
user_profile = Profiles.get_user_profile!(existing_profile.user_profile_id)

with {:ok, %StudentProfile{} = student_profile} <-
Profiles.update_student_profile_with_user_profile(
existing_profile,
user_profile,
params
) do
conn
|> put_status(:ok)
|> render("show.json", student_profile: student_profile)
end
end
Expand Down
28 changes: 27 additions & 1 deletion lib/dbservice_web/controllers/teacher_profile_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,36 @@ defmodule DbserviceWeb.TeacherProfileController do
|> Map.put_new("user_id", user_id)
|> Map.put_new("teacher_fk", teacher_fk)

case Profiles.get_profile_by_teacher_id(updated_params["teacher_id"]) do
nil ->
create_new_profile(conn, updated_params)

existing_profile ->
update_existing_profile(conn, existing_profile, updated_params)
end
end

defp create_new_profile(conn, params) do
with {:ok, %TeacherProfile{} = teacher_profile} <-
Profiles.create_teacher_profile_with_user_profile(updated_params) do
Profiles.create_teacher_profile_with_user_profile(params) do
conn
|> put_status(:created)
|> put_resp_header("location", Routes.teacher_profile_path(conn, :show, teacher_profile))
|> render("show.json", teacher_profile: teacher_profile)
end
end

defp update_existing_profile(conn, existing_profile, params) do
user_profile = Profiles.get_user_profile!(existing_profile.user_profile_id)

with {:ok, %TeacherProfile{} = teacher_profile} <-
Profiles.update_teacher_profile_with_user_profile(
existing_profile,
user_profile,
params
) do
conn
|> put_status(:ok)
|> render("show.json", teacher_profile: teacher_profile)
end
end
Expand Down
29 changes: 24 additions & 5 deletions lib/dbservice_web/controllers/user_profile_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,12 @@ defmodule DbserviceWeb.UserProfileController do
end

def create(conn, params) do
with {:ok, %UserProfile{} = user_profile} <- Profiles.create_user_profile(params) do
conn
|> put_status(:created)
|> put_resp_header("location", Routes.user_path(conn, :show, user_profile))
|> render("show.json", user_profile: user_profile)
case Profiles.get_profile_by_user_id(params["user_id"]) do
nil ->
create_new_profile(conn, params)

existing_profile ->
update_existing_profile(conn, existing_profile, params)
end
end

Expand Down Expand Up @@ -128,4 +129,22 @@ defmodule DbserviceWeb.UserProfileController do
send_resp(conn, :no_content, "")
end
end

defp create_new_profile(conn, params) do
with {:ok, %UserProfile{} = user_profile} <- Profiles.create_user_profile(params) do
conn
|> put_status(:created)
|> put_resp_header("location", Routes.user_profile_path(conn, :show, user_profile))
|> render("show.json", user_profile: user_profile)
end
end

defp update_existing_profile(conn, existing_profile, params) do
with {:ok, %UserProfile{} = user_profile} <-
Profiles.update_user_profile(existing_profile, params) do
conn
|> put_status(:ok)
|> render("show.json", user_profile: user_profile)
end
end
end
9 changes: 0 additions & 9 deletions lib/dbservice_web/swagger_schemas/student_profile.ex
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,6 @@ defmodule DbserviceWeb.SwaggerSchema.StudentProfile do
"Total Number of skipped questions in tests"
)

current_grade(:string, "Current Grade")
current_program(:string, "Current Program")
current_batch(:string, "Current Batch")
logged_in_atleast_once(:boolean, "Has user logged in atleast once?")
latest_session_accessed(:string, "Name of the latest session accessed")
end
Expand Down Expand Up @@ -217,9 +214,6 @@ defmodule DbserviceWeb.SwaggerSchema.StudentProfile do
tests_number_of_correct_questions: 75,
tests_number_of_wrong_questions: 10,
tests_number_of_skipped_questions: 15,
current_grade: "11",
current_program: "Science",
current_batch: "Batch A",
logged_in_atleast_once: true,
latest_session_accessed: "LiveClass_10"
})
Expand Down Expand Up @@ -335,9 +329,6 @@ defmodule DbserviceWeb.SwaggerSchema.StudentProfile do
student_fk: 1,
user_profile: %{
user_id: 1,
current_grade: "11",
current_program: "Science",
current_batch: "Batch A",
logged_in_atleast_once: true,
latest_session_accessed: "LiveClass_10"
}
Expand Down
9 changes: 0 additions & 9 deletions lib/dbservice_web/swagger_schemas/teacher_profile.ex
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ defmodule DbserviceWeb.SwaggerSchema.TeacherProfile do
avg_rating(:decimal, "Average rating of the teacher")
teacher_id(:string, "Teacher ID associated with the teacher's profile")

current_grade(:string, "Current Grade")
current_program(:string, "Current Program")
current_batch(:string, "Current Batch")
logged_in_atleast_once(:boolean, "Has user logged in atleast once?")
latest_session_accessed(:string, "Name of the latest session accessed")
end
Expand All @@ -72,9 +69,6 @@ defmodule DbserviceWeb.SwaggerSchema.TeacherProfile do
program_manager: "John Doe",
avg_rating: 4.5,
teacher_id: "20202",
current_grade: "11",
current_program: "HaryanaStudents",
current_batch: "Photon",
logged_in_atleast_once: false,
latest_session_accessed: "LiveClass_10"
})
Expand Down Expand Up @@ -110,9 +104,6 @@ defmodule DbserviceWeb.SwaggerSchema.TeacherProfile do
teacher_fk: 2,
user_profile: %{
user_id: 2,
current_grade: "11",
current_program: "HaryanaStudents",
current_batch: "Photon",
logged_in_atleast_once: false,
latest_session_accessed: "LiveClass_10"
}
Expand Down
6 changes: 0 additions & 6 deletions lib/dbservice_web/swagger_schemas/user_profile.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,12 @@ defmodule DbserviceWeb.SwaggerSchema.UserProfile do

properties do
user_id(:integer, "Corresponding user ID of the user")
current_grade(:string, "Current Grade")
current_program(:string, "Current Program")
current_batch(:string, "Current Batch")
logged_in_atleast_once(:boolean, "Has user logged in atleast once?")
latest_session_accessed(:string, "Name of the latest session accessed")
end

example(%{
user_id: 10,
current_grade: "11",
current_program: "HaryanaStudents",
current_batch: "Photon",
logged_in_atleast_once: true,
latest_session_accessed: "LiveClass_10"
})
Expand Down
3 changes: 0 additions & 3 deletions lib/dbservice_web/views/user_profile_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ defmodule DbserviceWeb.UserProfileView do
%{
id: user_profile.id,
user_id: user_profile.user_id,
current_grade: user_profile.current_grade,
current_program: user_profile.current_program,
current_batch: user_profile.current_batch,
logged_in_atleast_once: user_profile.logged_in_atleast_once,
latest_session_accessed: user_profile.latest_session_accessed
}
Expand Down
Loading
Loading