From ea9b0dcbf8272647da23b824dfae52e92191fff6 Mon Sep 17 00:00:00 2001 From: Bahugunajii Date: Fri, 6 Oct 2023 18:26:52 +0530 Subject: [PATCH 1/3] gurukul schema --- lib/dbservice/batches.ex | 20 +- lib/dbservice/chapters.ex | 81 +++++ lib/dbservice/chapters/chapter.ex | 37 ++ lib/dbservice/concepts.ex | 81 +++++ lib/dbservice/concepts/concept.ex | 32 ++ lib/dbservice/curriculums.ex | 81 +++++ lib/dbservice/curriculums/curriculum.ex | 29 ++ lib/dbservice/grades.ex | 81 +++++ lib/dbservice/grades/grade.ex | 29 ++ lib/dbservice/learning_objectives.ex | 81 +++++ .../learning_objectives/learning_objective.ex | 30 ++ lib/dbservice/purposes.ex | 81 +++++ lib/dbservice/purposes/purpose.ex | 29 ++ lib/dbservice/resources.ex | 81 +++++ lib/dbservice/resources/resource.ex | 52 +++ lib/dbservice/sources.ex | 81 +++++ lib/dbservice/sources/source.ex | 29 ++ lib/dbservice/subjects.ex | 81 +++++ lib/dbservice/subjects/subject.ex | 29 ++ lib/dbservice/tags.ex | 81 +++++ lib/dbservice/tags/tag.ex | 44 +++ lib/dbservice/topics.ex | 81 +++++ lib/dbservice/topics/topic.ex | 37 ++ .../controllers/chapter_controller.ex | 131 +++++++ .../controllers/concept_controller.ex | 126 +++++++ .../controllers/curriculum_controller.ex | 128 +++++++ .../controllers/grade_controller.ex | 126 +++++++ .../learning_objective_controller.ex | 126 +++++++ .../controllers/purpose_controller.ex | 126 +++++++ .../controllers/resource_controller.ex | 126 +++++++ .../controllers/source_controller.ex | 131 +++++++ .../controllers/subject_controller.ex | 131 +++++++ .../controllers/tag_controller.ex | 127 +++++++ .../controllers/topic_controller.ex | 132 +++++++ lib/dbservice_web/router.ex | 11 + lib/dbservice_web/swagger_schemas/chapter.ex | 43 +++ lib/dbservice_web/swagger_schemas/concept.ex | 39 +++ .../swagger_schemas/curriculum.ex | 39 +++ lib/dbservice_web/swagger_schemas/grade.ex | 37 ++ .../swagger_schemas/learning_objective.ex | 39 +++ lib/dbservice_web/swagger_schemas/purpose.ex | 39 +++ lib/dbservice_web/swagger_schemas/resource.ex | 57 +++ lib/dbservice_web/swagger_schemas/source.ex | 39 +++ lib/dbservice_web/swagger_schemas/subject.ex | 38 ++ lib/dbservice_web/swagger_schemas/tag.ex | 37 ++ lib/dbservice_web/swagger_schemas/topic.ex | 43 +++ lib/dbservice_web/views/chapter_view.ex | 23 ++ lib/dbservice_web/views/concept_view.ex | 21 ++ lib/dbservice_web/views/curriculum_view.ex | 21 ++ lib/dbservice_web/views/grade_view.ex | 20 ++ .../views/learning_objective_view.ex | 21 ++ lib/dbservice_web/views/purpose_view.ex | 21 ++ lib/dbservice_web/views/resource_view.ex | 30 ++ lib/dbservice_web/views/source_view.ex | 21 ++ lib/dbservice_web/views/subject_view.ex | 21 ++ lib/dbservice_web/views/tag_view.ex | 20 ++ lib/dbservice_web/views/topic_view.ex | 23 ++ .../20231004104045_create_tag_table.exs | 12 + ...20231004104058_create_curriculum_table.exs | 13 + .../20231004104120_create_grade_table.exs | 12 + .../20231004104137_create_subject_table.exs | 13 + .../20231004104148_create_chapter_table.exs | 15 + .../20231004104203_create_topic_table.exs | 15 + .../20231004104222_create_concept_table.exs | 13 + ...104241_create_learning_objective_table.exs | 13 + .../20231004104257_create_source_table.exs | 13 + .../20231004104301_create_purpose_table.exs | 13 + .../20231004104309_create_resource_table.exs | 22 ++ priv/repo/seeds.exs | 325 +++++++++++++++++- 69 files changed, 3866 insertions(+), 14 deletions(-) create mode 100644 lib/dbservice/chapters.ex create mode 100644 lib/dbservice/chapters/chapter.ex create mode 100644 lib/dbservice/concepts.ex create mode 100644 lib/dbservice/concepts/concept.ex create mode 100644 lib/dbservice/curriculums.ex create mode 100644 lib/dbservice/curriculums/curriculum.ex create mode 100644 lib/dbservice/grades.ex create mode 100644 lib/dbservice/grades/grade.ex create mode 100644 lib/dbservice/learning_objectives.ex create mode 100644 lib/dbservice/learning_objectives/learning_objective.ex create mode 100644 lib/dbservice/purposes.ex create mode 100644 lib/dbservice/purposes/purpose.ex create mode 100644 lib/dbservice/resources.ex create mode 100644 lib/dbservice/resources/resource.ex create mode 100644 lib/dbservice/sources.ex create mode 100644 lib/dbservice/sources/source.ex create mode 100644 lib/dbservice/subjects.ex create mode 100644 lib/dbservice/subjects/subject.ex create mode 100644 lib/dbservice/tags.ex create mode 100644 lib/dbservice/tags/tag.ex create mode 100644 lib/dbservice/topics.ex create mode 100644 lib/dbservice/topics/topic.ex create mode 100644 lib/dbservice_web/controllers/chapter_controller.ex create mode 100644 lib/dbservice_web/controllers/concept_controller.ex create mode 100644 lib/dbservice_web/controllers/curriculum_controller.ex create mode 100644 lib/dbservice_web/controllers/grade_controller.ex create mode 100644 lib/dbservice_web/controllers/learning_objective_controller.ex create mode 100644 lib/dbservice_web/controllers/purpose_controller.ex create mode 100644 lib/dbservice_web/controllers/resource_controller.ex create mode 100644 lib/dbservice_web/controllers/source_controller.ex create mode 100644 lib/dbservice_web/controllers/subject_controller.ex create mode 100644 lib/dbservice_web/controllers/tag_controller.ex create mode 100644 lib/dbservice_web/controllers/topic_controller.ex create mode 100644 lib/dbservice_web/swagger_schemas/chapter.ex create mode 100644 lib/dbservice_web/swagger_schemas/concept.ex create mode 100644 lib/dbservice_web/swagger_schemas/curriculum.ex create mode 100644 lib/dbservice_web/swagger_schemas/grade.ex create mode 100644 lib/dbservice_web/swagger_schemas/learning_objective.ex create mode 100644 lib/dbservice_web/swagger_schemas/purpose.ex create mode 100644 lib/dbservice_web/swagger_schemas/resource.ex create mode 100644 lib/dbservice_web/swagger_schemas/source.ex create mode 100644 lib/dbservice_web/swagger_schemas/subject.ex create mode 100644 lib/dbservice_web/swagger_schemas/tag.ex create mode 100644 lib/dbservice_web/swagger_schemas/topic.ex create mode 100644 lib/dbservice_web/views/chapter_view.ex create mode 100644 lib/dbservice_web/views/concept_view.ex create mode 100644 lib/dbservice_web/views/curriculum_view.ex create mode 100644 lib/dbservice_web/views/grade_view.ex create mode 100644 lib/dbservice_web/views/learning_objective_view.ex create mode 100644 lib/dbservice_web/views/purpose_view.ex create mode 100644 lib/dbservice_web/views/resource_view.ex create mode 100644 lib/dbservice_web/views/source_view.ex create mode 100644 lib/dbservice_web/views/subject_view.ex create mode 100644 lib/dbservice_web/views/tag_view.ex create mode 100644 lib/dbservice_web/views/topic_view.ex create mode 100644 priv/repo/migrations/20231004104045_create_tag_table.exs create mode 100644 priv/repo/migrations/20231004104058_create_curriculum_table.exs create mode 100644 priv/repo/migrations/20231004104120_create_grade_table.exs create mode 100644 priv/repo/migrations/20231004104137_create_subject_table.exs create mode 100644 priv/repo/migrations/20231004104148_create_chapter_table.exs create mode 100644 priv/repo/migrations/20231004104203_create_topic_table.exs create mode 100644 priv/repo/migrations/20231004104222_create_concept_table.exs create mode 100644 priv/repo/migrations/20231004104241_create_learning_objective_table.exs create mode 100644 priv/repo/migrations/20231004104257_create_source_table.exs create mode 100644 priv/repo/migrations/20231004104301_create_purpose_table.exs create mode 100644 priv/repo/migrations/20231004104309_create_resource_table.exs diff --git a/lib/dbservice/batches.ex b/lib/dbservice/batches.ex index 16073af8..b053dc26 100644 --- a/lib/dbservice/batches.ex +++ b/lib/dbservice/batches.ex @@ -1,6 +1,6 @@ defmodule Dbservice.Batches do @moduledoc """ - The Groups context. + The Batches context. """ import Ecto.Query, warn: false @@ -12,8 +12,8 @@ defmodule Dbservice.Batches do @doc """ Returns the list of batch. ## Examples - iex> list_group() - [%Group{}, ...] + iex> list_batch() + [%Batch{}, ...] """ def list_batch do Repo.all(Batch) @@ -21,10 +21,10 @@ defmodule Dbservice.Batches do @doc """ Gets a single batch. - Raises `Ecto.NoResultsError` if the Group does not exist. + Raises `Ecto.NoResultsError` if the batch does not exist. ## Examples iex> get_batch!(123) - %Group{} + %Batch{} iex> get_batch!(456) ** (Ecto.NoResultsError) """ @@ -34,7 +34,7 @@ defmodule Dbservice.Batches do Creates a batch. ## Examples iex> create_batch(%{field: value}) - {:ok, %Group{}} + {:ok, %Batch{}} iex> create_batch(%{field: bad_value}) {:error, %Ecto.Changeset{}} """ @@ -49,7 +49,7 @@ defmodule Dbservice.Batches do Updates a batch. ## Examples iex> update_batch(batch, %{field: new_value}) - {:ok, %Group{}} + {:ok, %Batch{}} iex> update_batch(batch, %{field: bad_value}) {:error, %Ecto.Changeset{}} """ @@ -63,7 +63,7 @@ defmodule Dbservice.Batches do Deletes a batch. ## Examples iex> delete_batch(batch) - {:ok, %GroupUser{}} + {:ok, %Batch{}} iex> delete_batch(batch) {:error, %Ecto.Changeset{}} """ @@ -72,10 +72,10 @@ defmodule Dbservice.Batches do end @doc """ - Returns an `%Ecto.Changeset{}` for tracking group changes. + Returns an `%Ecto.Changeset{}` for tracking batch changes. ## Examples iex> change_batch(batch) - %Ecto.Changeset{data: %Groupuser{}} + %Ecto.Changeset{data: %Batch{}} """ def change_batch(%Batch{} = batch, attrs \\ %{}) do Batch.changeset(batch, attrs) diff --git a/lib/dbservice/chapters.ex b/lib/dbservice/chapters.ex new file mode 100644 index 00000000..d4505df9 --- /dev/null +++ b/lib/dbservice/chapters.ex @@ -0,0 +1,81 @@ +defmodule Dbservice.Chapters do + @moduledoc """ + The Chapters context. + """ + + import Ecto.Query, warn: false + alias Dbservice.Repo + + alias Dbservice.Chapters.Chapter + + @doc """ + Returns the list of chapter. + ## Examples + iex> list_chapter() + [%Chapter{}, ...] + """ + def list_chapter do + Repo.all(Chapter) + end + + @doc """ + Gets a single chapter. + Raises `Ecto.NoResultsError` if the chapter does not exist. + ## Examples + iex> get_chapter!(123) + %Chapter{} + iex> get_chapter!(456) + ** (Ecto.NoResultsError) + """ + def get_chapter!(id), do: Repo.get!(Chapter, id) + + @doc """ + Creates a chapter. + ## Examples + iex> create_chapter(%{field: value}) + {:ok, %Chapter{}} + iex> create_chapter(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + """ + def create_chapter(attrs \\ %{}) do + %Chapter{} + |> Chapter.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a chapter. + ## Examples + iex> update_chapter(chapter, %{field: new_value}) + {:ok, %Chapter{}} + iex> update_chapter(chapter, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + """ + def update_chapter(%Chapter{} = chapter, attrs) do + chapter + |> Chapter.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a chapter. + ## Examples + iex> delete_chapter(chapter) + {:ok, %Chapter{}} + iex> delete_chapter(chapter) + {:error, %Ecto.Changeset{}} + """ + def delete_chapter(%Chapter{} = chapter) do + Repo.delete(chapter) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking chapter changes. + ## Examples + iex> change_chapter(chapter) + %Ecto.Changeset{data: %Chapter{}} + """ + def change_chapter(%Chapter{} = chapter, attrs \\ %{}) do + Chapter.changeset(chapter, attrs) + end +end diff --git a/lib/dbservice/chapters/chapter.ex b/lib/dbservice/chapters/chapter.ex new file mode 100644 index 00000000..57206d02 --- /dev/null +++ b/lib/dbservice/chapters/chapter.ex @@ -0,0 +1,37 @@ +defmodule Dbservice.Chapters.Chapter do + @moduledoc false + + use Ecto.Schema + import Ecto.Changeset + + alias Dbservice.Grades.Grade + alias Dbservice.Subjects.Subject + alias Dbservice.Tags.Tag + alias Dbservice.Topics.Topic + alias Dbservice.Resources.Resource + + schema "chapter" do + field(:name, :string) + field(:code, :string) + + timestamps() + + has_many(:topic, Topic) + has_many(:resource, Resource) + belongs_to(:grade, Grade) + belongs_to(:subject, Subject) + belongs_to(:tag, Tag) + end + + @doc false + def changeset(chapter, attrs) do + chapter + |> cast(attrs, [ + :name, + :code, + :grade_id, + :subject_id, + :tag_id + ]) + end +end diff --git a/lib/dbservice/concepts.ex b/lib/dbservice/concepts.ex new file mode 100644 index 00000000..014d61d8 --- /dev/null +++ b/lib/dbservice/concepts.ex @@ -0,0 +1,81 @@ +defmodule Dbservice.Concepts do + @moduledoc """ + The Concepts context. + """ + + import Ecto.Query, warn: false + alias Dbservice.Repo + + alias Dbservice.Concepts.Concept + + @doc """ + Returns the list of concept. + ## Examples + iex> list_concept() + [%Concept{}, ...] + """ + def list_concept do + Repo.all(Concept) + end + + @doc """ + Gets a single concept. + Raises `Ecto.NoResultsError` if the concept does not exist. + ## Examples + iex> get_concept!(123) + %Concept{} + iex> get_concept!(456) + ** (Ecto.NoResultsError) + """ + def get_concept!(id), do: Repo.get!(Concept, id) + + @doc """ + Creates a concept. + ## Examples + iex> create_concept(%{field: value}) + {:ok, %Concept{}} + iex> create_concept(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + """ + def create_concept(attrs \\ %{}) do + %Concept{} + |> Concept.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a concept. + ## Examples + iex> update_concept(concept, %{field: new_value}) + {:ok, %Concept{}} + iex> update_concept(concept, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + """ + def update_concept(%Concept{} = concept, attrs) do + concept + |> Concept.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a concept. + ## Examples + iex> delete_concept(concept) + {:ok, %Concept{}} + iex> delete_concept(concept) + {:error, %Ecto.Changeset{}} + """ + def delete_concept(%Concept{} = concept) do + Repo.delete(concept) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking concept changes. + ## Examples + iex> change_concept(concept) + %Ecto.Changeset{data: %Concept{}} + """ + def change_concept(%Concept{} = concept, attrs \\ %{}) do + Concept.changeset(concept, attrs) + end +end diff --git a/lib/dbservice/concepts/concept.ex b/lib/dbservice/concepts/concept.ex new file mode 100644 index 00000000..cfa5c39a --- /dev/null +++ b/lib/dbservice/concepts/concept.ex @@ -0,0 +1,32 @@ +defmodule Dbservice.Concepts.Concept do + @moduledoc false + + use Ecto.Schema + import Ecto.Changeset + + alias Dbservice.Topics.Topic + alias Dbservice.Tags.Tag + alias Dbservice.LearningObjectives.LearningObjective + alias Dbservice.Resources.Resource + + schema "concept" do + field(:name, :string) + + timestamps() + + has_many(:learning_objective, LearningObjective) + has_many(:resource, Resource) + belongs_to(:topic, Topic) + belongs_to(:tag, Tag) + end + + @doc false + def changeset(concept, attrs) do + concept + |> cast(attrs, [ + :name, + :topic_id, + :tag_id + ]) + end +end diff --git a/lib/dbservice/curriculums.ex b/lib/dbservice/curriculums.ex new file mode 100644 index 00000000..38322d35 --- /dev/null +++ b/lib/dbservice/curriculums.ex @@ -0,0 +1,81 @@ +defmodule Dbservice.Curriculums do + @moduledoc """ + The Curriculums context. + """ + + import Ecto.Query, warn: false + alias Dbservice.Repo + + alias Dbservice.Curriculums.Curriculum + + @doc """ + Returns the list of curriculum. + ## Examples + iex> list_curriculum() + [%Curriculum{}, ...] + """ + def list_curriculum do + Repo.all(Curriculum) + end + + @doc """ + Gets a single curriculum. + Raises `Ecto.NoResultsError` if the curriculum does not exist. + ## Examples + iex> get_curriculum!(123) + %Curriculum{} + iex> get_curriculum!(456) + ** (Ecto.NoResultsError) + """ + def get_curriculum!(id), do: Repo.get!(Curriculum, id) + + @doc """ + Creates a curriculum. + ## Examples + iex> create_curriculum(%{field: value}) + {:ok, %Curriculum{}} + iex> create_curriculum(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + """ + def create_curriculum(attrs \\ %{}) do + %Curriculum{} + |> Curriculum.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a curriculum. + ## Examples + iex> update_curriculum(curriculum, %{field: new_value}) + {:ok, %Curriculum{}} + iex> update_curriculum(curriculum, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + """ + def update_curriculum(%Curriculum{} = curriculum, attrs) do + curriculum + |> Curriculum.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a curriculum. + ## Examples + iex> delete_curriculum(curriculum) + {:ok, %Curriculum{}} + iex> delete_curriculum(curriculum) + {:error, %Ecto.Changeset{}} + """ + def delete_curriculum(%Curriculum{} = curriculum) do + Repo.delete(curriculum) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking curriculum changes. + ## Examples + iex> change_curriculum(curriculum) + %Ecto.Changeset{data: %Curriculum{}} + """ + def change_curriculum(%Curriculum{} = curriculum, attrs \\ %{}) do + Curriculum.changeset(curriculum, attrs) + end +end diff --git a/lib/dbservice/curriculums/curriculum.ex b/lib/dbservice/curriculums/curriculum.ex new file mode 100644 index 00000000..c77dcd9d --- /dev/null +++ b/lib/dbservice/curriculums/curriculum.ex @@ -0,0 +1,29 @@ +defmodule Dbservice.Curriculums.Curriculum do + @moduledoc false + + use Ecto.Schema + import Ecto.Changeset + + alias Dbservice.Tags.Tag + alias Dbservice.Resources.Resource + + schema "curriculum" do + field(:name, :string) + field(:code, :string) + + timestamps() + + has_many(:resource, Resource) + belongs_to(:tag, Tag) + end + + @doc false + def changeset(curriculum, attrs) do + curriculum + |> cast(attrs, [ + :name, + :code, + :tag_id + ]) + end +end diff --git a/lib/dbservice/grades.ex b/lib/dbservice/grades.ex new file mode 100644 index 00000000..53d7d645 --- /dev/null +++ b/lib/dbservice/grades.ex @@ -0,0 +1,81 @@ +defmodule Dbservice.Grades do + @moduledoc """ + The Grades context. + """ + + import Ecto.Query, warn: false + alias Dbservice.Repo + + alias Dbservice.Grades.Grade + + @doc """ + Returns the list of grade. + ## Examples + iex> list_grade() + [%Grade{}, ...] + """ + def list_grade do + Repo.all(Grade) + end + + @doc """ + Gets a single grade. + Raises `Ecto.NoResultsError` if the grade does not exist. + ## Examples + iex> get_grade!(123) + %Grade{} + iex> get_grade!(456) + ** (Ecto.NoResultsError) + """ + def get_grade!(id), do: Repo.get!(Grade, id) + + @doc """ + Creates a grade. + ## Examples + iex> create_grade(%{field: value}) + {:ok, %Grade{}} + iex> create_grade(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + """ + def create_grade(attrs \\ %{}) do + %Grade{} + |> Grade.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a grade. + ## Examples + iex> update_grade(grade, %{field: new_value}) + {:ok, %Grade{}} + iex> update_grade(grade, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + """ + def update_grade(%Grade{} = grade, attrs) do + grade + |> Grade.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a grade. + ## Examples + iex> delete_grade(grade) + {:ok, %Grade{}} + iex> delete_grade(grade) + {:error, %Ecto.Changeset{}} + """ + def delete_grade(%Grade{} = grade) do + Repo.delete(grade) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking grade changes. + ## Examples + iex> change_grade(grade) + %Ecto.Changeset{data: %Grade{}} + """ + def change_grade(%Grade{} = grade, attrs \\ %{}) do + Grade.changeset(grade, attrs) + end +end diff --git a/lib/dbservice/grades/grade.ex b/lib/dbservice/grades/grade.ex new file mode 100644 index 00000000..27a1f39c --- /dev/null +++ b/lib/dbservice/grades/grade.ex @@ -0,0 +1,29 @@ +defmodule Dbservice.Grades.Grade do + @moduledoc false + + use Ecto.Schema + import Ecto.Changeset + + alias Dbservice.Tags.Tag + alias Dbservice.Chapters.Chapter + alias Dbservice.Topics.Topic + + schema "grade" do + field(:number, :integer) + + timestamps() + + has_many(:chapter, Chapter) + has_many(:topic, Topic) + belongs_to(:tag, Tag) + end + + @doc false + def changeset(grade, attrs) do + grade + |> cast(attrs, [ + :number, + :tag_id + ]) + end +end diff --git a/lib/dbservice/learning_objectives.ex b/lib/dbservice/learning_objectives.ex new file mode 100644 index 00000000..92877057 --- /dev/null +++ b/lib/dbservice/learning_objectives.ex @@ -0,0 +1,81 @@ +defmodule Dbservice.LearningObjectives do + @moduledoc """ + The LearningObjectives context. + """ + + import Ecto.Query, warn: false + alias Dbservice.Repo + + alias Dbservice.LearningObjectives.LearningObjective + + @doc """ + Returns the list of learning_objective. + ## Examples + iex> list_learning_objective() + [%LearningObjective{}, ...] + """ + def list_learning_objective do + Repo.all(LearningObjective) + end + + @doc """ + Gets a single learning_objective. + Raises `Ecto.NoResultsError` if the learning_objective does not exist. + ## Examples + iex> get_learning_objective!(123) + %LearningObjective{} + iex> get_learning_objective!(456) + ** (Ecto.NoResultsError) + """ + def get_learning_objective!(id), do: Repo.get!(LearningObjective, id) + + @doc """ + Creates a learning_objective. + ## Examples + iex> create_learning_objective(%{field: value}) + {:ok, %LearningObjective{}} + iex> create_learning_objective(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + """ + def create_learning_objective(attrs \\ %{}) do + %LearningObjective{} + |> LearningObjective.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a learning_objective. + ## Examples + iex> update_learning_objective(learning_objective, %{field: new_value}) + {:ok, %LearningObjective{}} + iex> update_learning_objective(learning_objective, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + """ + def update_learning_objective(%LearningObjective{} = learning_objective, attrs) do + learning_objective + |> LearningObjective.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a learning_objective. + ## Examples + iex> delete_learning_objective(learning_objective) + {:ok, %LearningObjective{}} + iex> delete_learning_objective(learning_objective) + {:error, %Ecto.Changeset{}} + """ + def delete_learning_objective(%LearningObjective{} = learning_objective) do + Repo.delete(learning_objective) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking learning_objective changes. + ## Examples + iex> change_learning_objective(learning_objective) + %Ecto.Changeset{data: %LearningObjective{}} + """ + def change_learning_objective(%LearningObjective{} = learning_objective, attrs \\ %{}) do + LearningObjective.changeset(learning_objective, attrs) + end +end diff --git a/lib/dbservice/learning_objectives/learning_objective.ex b/lib/dbservice/learning_objectives/learning_objective.ex new file mode 100644 index 00000000..af93ff62 --- /dev/null +++ b/lib/dbservice/learning_objectives/learning_objective.ex @@ -0,0 +1,30 @@ +defmodule Dbservice.LearningObjectives.LearningObjective do + @moduledoc false + + use Ecto.Schema + import Ecto.Changeset + + alias Dbservice.Concepts.Concept + alias Dbservice.Tags.Tag + alias Dbservice.Resources.Resource + + schema "learning_objective" do + field(:title, :string) + + timestamps() + + has_many(:resource, Resource) + belongs_to(:concept, Concept) + belongs_to(:tag, Tag) + end + + @doc false + def changeset(learning_objective, attrs) do + learning_objective + |> cast(attrs, [ + :title, + :concept_id, + :tag_id + ]) + end +end diff --git a/lib/dbservice/purposes.ex b/lib/dbservice/purposes.ex new file mode 100644 index 00000000..893b56d4 --- /dev/null +++ b/lib/dbservice/purposes.ex @@ -0,0 +1,81 @@ +defmodule Dbservice.Purposes do + @moduledoc """ + The Purposes context. + """ + + import Ecto.Query, warn: false + alias Dbservice.Repo + + alias Dbservice.Purposes.Purpose + + @doc """ + Returns the list of purpose. + ## Examples + iex> list_purpose() + [%Purpose{}, ...] + """ + def list_purpose do + Repo.all(Purpose) + end + + @doc """ + Gets a single purpose. + Raises `Ecto.NoResultsError` if the purpose does not exist. + ## Examples + iex> get_purpose!(123) + %Purpose{} + iex> get_purpose!(456) + ** (Ecto.NoResultsError) + """ + def get_purpose!(id), do: Repo.get!(Purpose, id) + + @doc """ + Creates a purpose. + ## Examples + iex> create_purpose(%{field: value}) + {:ok, %Purpose{}} + iex> create_purpose(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + """ + def create_purpose(attrs \\ %{}) do + %Purpose{} + |> Purpose.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a purpose. + ## Examples + iex> update_purpose(purpose, %{field: new_value}) + {:ok, %Purpose{}} + iex> update_purpose(purpose, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + """ + def update_purpose(%Purpose{} = purpose, attrs) do + purpose + |> Purpose.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a purpose. + ## Examples + iex> delete_purpose(purpose) + {:ok, %Purpose{}} + iex> delete_purpose(purpose) + {:error, %Ecto.Changeset{}} + """ + def delete_purpose(%Purpose{} = purpose) do + Repo.delete(purpose) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking purpose changes. + ## Examples + iex> change_purpose(purpose) + %Ecto.Changeset{data: %Purpose{}} + """ + def change_purpose(%Purpose{} = purpose, attrs \\ %{}) do + Purpose.changeset(purpose, attrs) + end +end diff --git a/lib/dbservice/purposes/purpose.ex b/lib/dbservice/purposes/purpose.ex new file mode 100644 index 00000000..8bc30366 --- /dev/null +++ b/lib/dbservice/purposes/purpose.ex @@ -0,0 +1,29 @@ +defmodule Dbservice.Purposes.Purpose do + @moduledoc false + + use Ecto.Schema + import Ecto.Changeset + + alias Dbservice.Tags.Tag + alias Dbservice.Resources.Resource + + schema "purpose" do + field(:name, :string) + field(:description, :string) + + timestamps() + + has_many(:resource, Resource) + belongs_to(:tag, Tag) + end + + @doc false + def changeset(purpose, attrs) do + purpose + |> cast(attrs, [ + :name, + :description, + :tag_id + ]) + end +end diff --git a/lib/dbservice/resources.ex b/lib/dbservice/resources.ex new file mode 100644 index 00000000..b0a6fdf5 --- /dev/null +++ b/lib/dbservice/resources.ex @@ -0,0 +1,81 @@ +defmodule Dbservice.Resources do + @moduledoc """ + The Resources context. + """ + + import Ecto.Query, warn: false + alias Dbservice.Repo + + alias Dbservice.Resources.Resource + + @doc """ + Returns the list of resource. + ## Examples + iex> list_resource() + [%Resource{}, ...] + """ + def list_resource do + Repo.all(Resource) + end + + @doc """ + Gets a single resource. + Raises `Ecto.NoResultsError` if the resource does not exist. + ## Examples + iex> get_resource!(123) + %Resource{} + iex> get_resource!(456) + ** (Ecto.NoResultsError) + """ + def get_resource!(id), do: Repo.get!(Resource, id) + + @doc """ + Creates a resource. + ## Examples + iex> create_resource(%{field: value}) + {:ok, %Resource{}} + iex> create_resource(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + """ + def create_resource(attrs \\ %{}) do + %Resource{} + |> Resource.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a resource. + ## Examples + iex> update_resource(resource, %{field: new_value}) + {:ok, %Resource{}} + iex> update_resource(resource, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + """ + def update_resource(%Resource{} = resource, attrs) do + resource + |> Resource.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a resource. + ## Examples + iex> delete_resource(resource) + {:ok, %Resource{}} + iex> delete_resource(resource) + {:error, %Ecto.Changeset{}} + """ + def delete_resource(%Resource{} = resource) do + Repo.delete(resource) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking resource changes. + ## Examples + iex> change_resource(resource) + %Ecto.Changeset{data: %Resource{}} + """ + def change_resource(%Resource{} = resource, attrs \\ %{}) do + Resource.changeset(resource, attrs) + end +end diff --git a/lib/dbservice/resources/resource.ex b/lib/dbservice/resources/resource.ex new file mode 100644 index 00000000..288d4e5c --- /dev/null +++ b/lib/dbservice/resources/resource.ex @@ -0,0 +1,52 @@ +defmodule Dbservice.Resources.Resource do + @moduledoc false + + use Ecto.Schema + import Ecto.Changeset + + alias Dbservice.Curriculums.Curriculum + alias Dbservice.Chapters.Chapter + alias Dbservice.Topics.Topic + alias Dbservice.Sources.Source + alias Dbservice.Purposes.Purpose + alias Dbservice.Concepts.Concept + alias Dbservice.LearningObjectives.LearningObjective + alias Dbservice.Tags.Tag + + schema "resource" do + field(:name, :string) + field(:type, :string) + field(:type_params, :map) + field(:difficulty_level, :string) + + timestamps() + + belongs_to(:curriculum, Curriculum) + belongs_to(:chapter, Chapter) + belongs_to(:topic, Topic) + belongs_to(:source, Source) + belongs_to(:purpose, Purpose) + belongs_to(:concept, Concept) + belongs_to(:learning_objective, LearningObjective) + belongs_to(:tag, Tag) + end + + @doc false + def changeset(purpose, attrs) do + purpose + |> cast(attrs, [ + :name, + :type, + :type_params, + :difficulty_level, + :curriculum_id, + :chapter_id, + :topic_id, + :source_id, + :purpose_id, + :concept_id, + :learning_objective_id, + :tag_id + ]) + end +end diff --git a/lib/dbservice/sources.ex b/lib/dbservice/sources.ex new file mode 100644 index 00000000..216f6246 --- /dev/null +++ b/lib/dbservice/sources.ex @@ -0,0 +1,81 @@ +defmodule Dbservice.Sources do + @moduledoc """ + The Sources context. + """ + + import Ecto.Query, warn: false + alias Dbservice.Repo + + alias Dbservice.Sources.Source + + @doc """ + Returns the list of source. + ## Examples + iex> list_source() + [%Source{}, ...] + """ + def list_source do + Repo.all(Source) + end + + @doc """ + Gets a single source. + Raises `Ecto.NoResultsError` if the source does not exist. + ## Examples + iex> get_source!(123) + %Source{} + iex> get_source!(456) + ** (Ecto.NoResultsError) + """ + def get_source!(id), do: Repo.get!(Source, id) + + @doc """ + Creates a source. + ## Examples + iex> create_source(%{field: value}) + {:ok, %Source{}} + iex> create_source(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + """ + def create_source(attrs \\ %{}) do + %Source{} + |> Source.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a source. + ## Examples + iex> update_source(source, %{field: new_value}) + {:ok, %Source{}} + iex> update_source(source, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + """ + def update_source(%Source{} = source, attrs) do + source + |> Source.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a source. + ## Examples + iex> delete_source(source) + {:ok, %Source{}} + iex> delete_source(source) + {:error, %Ecto.Changeset{}} + """ + def delete_source(%Source{} = source) do + Repo.delete(source) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking source changes. + ## Examples + iex> change_source(source) + %Ecto.Changeset{data: %Source{}} + """ + def change_source(%Source{} = source, attrs \\ %{}) do + Source.changeset(source, attrs) + end +end diff --git a/lib/dbservice/sources/source.ex b/lib/dbservice/sources/source.ex new file mode 100644 index 00000000..4e1177e6 --- /dev/null +++ b/lib/dbservice/sources/source.ex @@ -0,0 +1,29 @@ +defmodule Dbservice.Sources.Source do + @moduledoc false + + use Ecto.Schema + import Ecto.Changeset + + alias Dbservice.Tags.Tag + alias Dbservice.Resources.Resource + + schema "source" do + field(:name, :string) + field(:link, :string) + + timestamps() + + has_many(:resource, Resource) + belongs_to(:tag, Tag) + end + + @doc false + def changeset(source, attrs) do + source + |> cast(attrs, [ + :name, + :link, + :tag_id + ]) + end +end diff --git a/lib/dbservice/subjects.ex b/lib/dbservice/subjects.ex new file mode 100644 index 00000000..84a364fd --- /dev/null +++ b/lib/dbservice/subjects.ex @@ -0,0 +1,81 @@ +defmodule Dbservice.Subjects do + @moduledoc """ + The Subjects context. + """ + + import Ecto.Query, warn: false + alias Dbservice.Repo + + alias Dbservice.Subjects.Subject + + @doc """ + Returns the list of subject. + ## Examples + iex> list_subject() + [%Subject{}, ...] + """ + def list_subject do + Repo.all(Subject) + end + + @doc """ + Gets a single subject. + Raises `Ecto.NoResultsError` if the subject does not exist. + ## Examples + iex> get_subject!(123) + %Subject{} + iex> get_subject!(456) + ** (Ecto.NoResultsError) + """ + def get_subject!(id), do: Repo.get!(Subject, id) + + @doc """ + Creates a subject. + ## Examples + iex> create_subject(%{field: value}) + {:ok, %Subject{}} + iex> create_subject(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + """ + def create_subject(attrs \\ %{}) do + %Subject{} + |> Subject.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a subject. + ## Examples + iex> update_subject(subject, %{field: new_value}) + {:ok, %Subject{}} + iex> update_subject(subject, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + """ + def update_subject(%Subject{} = subject, attrs) do + subject + |> Subject.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a subject. + ## Examples + iex> delete_subject(subject) + {:ok, %Subject{}} + iex> delete_subject(subject) + {:error, %Ecto.Changeset{}} + """ + def delete_subject(%Subject{} = subject) do + Repo.delete(subject) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking subject changes. + ## Examples + iex> change_subject(subject) + %Ecto.Changeset{data: %Subject{}} + """ + def change_subject(%Subject{} = subject, attrs \\ %{}) do + Subject.changeset(subject, attrs) + end +end diff --git a/lib/dbservice/subjects/subject.ex b/lib/dbservice/subjects/subject.ex new file mode 100644 index 00000000..cc1d8afb --- /dev/null +++ b/lib/dbservice/subjects/subject.ex @@ -0,0 +1,29 @@ +defmodule Dbservice.Subjects.Subject do + @moduledoc false + + use Ecto.Schema + import Ecto.Changeset + + alias Dbservice.Tags.Tag + alias Dbservice.Chapters.Chapter + + schema "subject" do + field(:name, :string) + field(:code, :string) + + timestamps() + + has_many(:chapter, Chapter) + belongs_to(:tag, Tag) + end + + @doc false + def changeset(subject, attrs) do + subject + |> cast(attrs, [ + :name, + :code, + :tag_id + ]) + end +end diff --git a/lib/dbservice/tags.ex b/lib/dbservice/tags.ex new file mode 100644 index 00000000..ecff33ee --- /dev/null +++ b/lib/dbservice/tags.ex @@ -0,0 +1,81 @@ +defmodule Dbservice.Tags do + @moduledoc """ + The Tags context. + """ + + import Ecto.Query, warn: false + alias Dbservice.Repo + + alias Dbservice.Tags.Tag + + @doc """ + Returns the list of tag. + ## Examples + iex> list_tag() + [%Tag{}, ...] + """ + def list_tag do + Repo.all(Tag) + end + + @doc """ + Gets a single tag. + Raises `Ecto.NoResultsError` if the tag does not exist. + ## Examples + iex> get_tag!(123) + %Tag{} + iex> get_tag!(456) + ** (Ecto.NoResultsError) + """ + def get_tag!(id), do: Repo.get!(Tag, id) + + @doc """ + Creates a tag. + ## Examples + iex> create_tag(%{field: value}) + {:ok, %Tag{}} + iex> create_tag(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + """ + def create_tag(attrs \\ %{}) do + %Tag{} + |> Tag.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a tag. + ## Examples + iex> update_tag(tag, %{field: new_value}) + {:ok, %Tag{}} + iex> update_tag(tag, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + """ + def update_tag(%Tag{} = tag, attrs) do + tag + |> Tag.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a tag. + ## Examples + iex> delete_tag(tag) + {:ok, %Tag{}} + iex> delete_tag(tag) + {:error, %Ecto.Changeset{}} + """ + def delete_tag(%Tag{} = tag) do + Repo.delete(tag) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking tag changes. + ## Examples + iex> change_tag(tag) + %Ecto.Changeset{data: %Tag{}} + """ + def change_tag(%Tag{} = tag, attrs \\ %{}) do + Tag.changeset(tag, attrs) + end +end diff --git a/lib/dbservice/tags/tag.ex b/lib/dbservice/tags/tag.ex new file mode 100644 index 00000000..47344870 --- /dev/null +++ b/lib/dbservice/tags/tag.ex @@ -0,0 +1,44 @@ +defmodule Dbservice.Tags.Tag do + @moduledoc false + + use Ecto.Schema + import Ecto.Changeset + + alias Dbservice.Curriculums.Curriculum + alias Dbservice.Grades.Grade + alias Dbservice.Subjects.Subject + alias Dbservice.Chapters.Chapter + alias Dbservice.Topics.Topic + alias Dbservice.Concepts.Concept + alias Dbservice.LearningObjectives.LearningObjective + alias Dbservice.Purposes.Purpose + alias Dbservice.Sources.Source + alias Dbservice.Resources.Resource + + schema "tag" do + field(:name, :string) + field(:description, :string) + + timestamps() + + has_one(:curriculum, Curriculum) + has_one(:grade, Grade) + has_one(:subject, Subject) + has_one(:chapter, Chapter) + has_one(:topic, Topic) + has_one(:concept, Concept) + has_one(:learning_objective, LearningObjective) + has_one(:purpose, Purpose) + has_one(:source, Source) + has_one(:resource, Resource) + end + + @doc false + def changeset(tag, attrs) do + tag + |> cast(attrs, [ + :name, + :description + ]) + end +end diff --git a/lib/dbservice/topics.ex b/lib/dbservice/topics.ex new file mode 100644 index 00000000..db708e1f --- /dev/null +++ b/lib/dbservice/topics.ex @@ -0,0 +1,81 @@ +defmodule Dbservice.Topics do + @moduledoc """ + The Topics context. + """ + + import Ecto.Query, warn: false + alias Dbservice.Repo + + alias Dbservice.Topics.Topic + + @doc """ + Returns the list of topic. + ## Examples + iex> list_topic() + [%Topic{}, ...] + """ + def list_topic do + Repo.all(Topic) + end + + @doc """ + Gets a single topic. + Raises `Ecto.NoResultsError` if the topic does not exist. + ## Examples + iex> get_topic!(123) + %Topic{} + iex> get_topic!(456) + ** (Ecto.NoResultsError) + """ + def get_topic!(id), do: Repo.get!(Topic, id) + + @doc """ + Creates a topic. + ## Examples + iex> create_topic(%{field: value}) + {:ok, %Topic{}} + iex> create_topic(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + """ + def create_topic(attrs \\ %{}) do + %Topic{} + |> Topic.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a topic. + ## Examples + iex> update_topic(topic, %{field: new_value}) + {:ok, %Topic{}} + iex> update_topic(topic, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + """ + def update_topic(%Topic{} = topic, attrs) do + topic + |> Topic.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a topic. + ## Examples + iex> delete_topic(topic) + {:ok, %Topic{}} + iex> delete_topic(topic) + {:error, %Ecto.Changeset{}} + """ + def delete_topic(%Topic{} = topic) do + Repo.delete(topic) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking topic changes. + ## Examples + iex> change_topic(topic) + %Ecto.Changeset{data: %Topic{}} + """ + def change_topic(%Topic{} = topic, attrs \\ %{}) do + Topic.changeset(topic, attrs) + end +end diff --git a/lib/dbservice/topics/topic.ex b/lib/dbservice/topics/topic.ex new file mode 100644 index 00000000..d5bc7823 --- /dev/null +++ b/lib/dbservice/topics/topic.ex @@ -0,0 +1,37 @@ +defmodule Dbservice.Topics.Topic do + @moduledoc false + + use Ecto.Schema + import Ecto.Changeset + + alias Dbservice.Grades.Grade + alias Dbservice.Chapters.Chapter + alias Dbservice.Tags.Tag + alias Dbservice.Concepts.Concept + alias Dbservice.Resources.Resource + + schema "topic" do + field(:name, :string) + field(:code, :string) + + timestamps() + + has_many(:concept, Concept) + has_many(:resource, Resource) + belongs_to(:grade, Grade) + belongs_to(:chapter, Chapter) + belongs_to(:tag, Tag) + end + + @doc false + def changeset(topic, attrs) do + topic + |> cast(attrs, [ + :name, + :code, + :grade_id, + :chapter_id, + :tag_id + ]) + end +end diff --git a/lib/dbservice_web/controllers/chapter_controller.ex b/lib/dbservice_web/controllers/chapter_controller.ex new file mode 100644 index 00000000..c5c8ac83 --- /dev/null +++ b/lib/dbservice_web/controllers/chapter_controller.ex @@ -0,0 +1,131 @@ +defmodule DbserviceWeb.ChapterController do + use DbserviceWeb, :controller + + import Ecto.Query + alias Dbservice.Repo + alias Dbservice.Chapters + alias Dbservice.Chapters.Chapter + + action_fallback(DbserviceWeb.FallbackController) + + use PhoenixSwagger + + alias DbserviceWeb.SwaggerSchema.Chapter, as: SwaggerSchemaChapter + + def swagger_definitions do + Map.merge( + SwaggerSchemaChapter.chapter(), + SwaggerSchemaChapter.chapters() + ) + end + + swagger_path :index do + get("/api/chapter") + + parameters do + params(:query, :string, "The chapter of a subject", + required: false, + name: "name" + ) + + params(:query, :string, "The code of the chapter", + required: false, + name: "code" + ) + end + + response(200, "OK", Schema.ref(:Chapters)) + end + + def index(conn, params) do + query = + from(m in Chapter, + order_by: [asc: m.id], + offset: ^params["offset"], + limit: ^params["limit"] + ) + + query = + Enum.reduce(params, query, fn {key, value}, acc -> + case String.to_existing_atom(key) do + :offset -> acc + :limit -> acc + atom -> from(u in acc, where: field(u, ^atom) == ^value) + end + end) + + chapter = Repo.all(query) + render(conn, "index.json", chapter: chapter) + end + + swagger_path :create do + post("/api/chapter") + + parameters do + body(:body, Schema.ref(:Chapter), "Chapter to create", required: true) + end + + response(201, "Created", Schema.ref(:Chapter)) + end + + def create(conn, params) do + with {:ok, %Chapter{} = chapter} <- Chapters.create_chapter(params) do + conn + |> put_status(:created) + |> put_resp_header("location", Routes.chapter_path(conn, :show, chapter)) + |> render("show.json", chapter: chapter) + end + end + + swagger_path :show do + get("/api/chapter/{chapterId}") + + parameters do + chapterId(:path, :integer, "The id of the chapter record", required: true) + end + + response(200, "OK", Schema.ref(:Chapter)) + end + + def show(conn, %{"id" => id}) do + chapter = Chapters.get_chapter!(id) + render(conn, "show.json", chapter: chapter) + end + + swagger_path :update do + patch("/api/chapter/{chapterId}") + + parameters do + chapterId(:path, :integer, "The id of the chapter record", required: true) + body(:body, Schema.ref(:Chapter), "Chapter to create", required: true) + end + + response(200, "Updated", Schema.ref(:Chapter)) + end + + def update(conn, params) do + chapter = Chapters.get_chapter!(params["id"]) + + with {:ok, %Chapter{} = chapter} <- Chapters.update_chapter(chapter, params) do + render(conn, "show.json", chapter: chapter) + end + end + + swagger_path :delete do + PhoenixSwagger.Path.delete("/api/chapter/{chapterId}") + + parameters do + chapterId(:path, :integer, "The id of the chapter record", required: true) + end + + response(204, "No Content") + end + + def delete(conn, %{"id" => id}) do + chapter = Chapters.get_chapter!(id) + + with {:ok, %Chapter{}} <- Chapters.delete_chapter(chapter) do + send_resp(conn, :no_content, "") + end + end +end diff --git a/lib/dbservice_web/controllers/concept_controller.ex b/lib/dbservice_web/controllers/concept_controller.ex new file mode 100644 index 00000000..cee5a489 --- /dev/null +++ b/lib/dbservice_web/controllers/concept_controller.ex @@ -0,0 +1,126 @@ +defmodule DbserviceWeb.ConceptController do + use DbserviceWeb, :controller + + import Ecto.Query + alias Dbservice.Repo + alias Dbservice.Concepts + alias Dbservice.Concepts.Concept + + action_fallback(DbserviceWeb.FallbackController) + + use PhoenixSwagger + + alias DbserviceWeb.SwaggerSchema.Concept, as: SwaggerSchemaConcept + + def swagger_definitions do + Map.merge( + SwaggerSchemaConcept.concept(), + SwaggerSchemaConcept.concepts() + ) + end + + swagger_path :index do + get("/api/concept") + + parameters do + params(:query, :string, "The concept of a topic", + required: false, + name: "name" + ) + end + + response(200, "OK", Schema.ref(:Concepts)) + end + + def index(conn, params) do + query = + from(m in Concept, + order_by: [asc: m.id], + offset: ^params["offset"], + limit: ^params["limit"] + ) + + query = + Enum.reduce(params, query, fn {key, value}, acc -> + case String.to_existing_atom(key) do + :offset -> acc + :limit -> acc + atom -> from(u in acc, where: field(u, ^atom) == ^value) + end + end) + + concept = Repo.all(query) + render(conn, "index.json", concept: concept) + end + + swagger_path :create do + post("/api/concept") + + parameters do + body(:body, Schema.ref(:Concept), "Concept to create", required: true) + end + + response(201, "Created", Schema.ref(:Concept)) + end + + def create(conn, params) do + with {:ok, %Concept{} = concept} <- Concepts.create_concept(params) do + conn + |> put_status(:created) + |> put_resp_header("location", Routes.concept_path(conn, :show, concept)) + |> render("show.json", concept: concept) + end + end + + swagger_path :show do + get("/api/concept/{conceptId}") + + parameters do + conceptId(:path, :integer, "The id of the concept record", required: true) + end + + response(200, "OK", Schema.ref(:Concept)) + end + + def show(conn, %{"id" => id}) do + concept = Concepts.get_concept!(id) + render(conn, "show.json", concept: concept) + end + + swagger_path :update do + patch("/api/concept/{conceptId}") + + parameters do + conceptId(:path, :integer, "The id of the concept record", required: true) + body(:body, Schema.ref(:Concept), "Concept to create", required: true) + end + + response(200, "Updated", Schema.ref(:Concept)) + end + + def update(conn, params) do + concept = Concepts.get_concept!(params["id"]) + + with {:ok, %Concept{} = concept} <- Concepts.update_concept(concept, params) do + render(conn, "show.json", concept: concept) + end + end + + swagger_path :delete do + PhoenixSwagger.Path.delete("/api/concept/{conceptId}") + + parameters do + conceptId(:path, :integer, "The id of the concept record", required: true) + end + + response(204, "No Content") + end + + def delete(conn, %{"id" => id}) do + concept = Concepts.get_concept!(id) + + with {:ok, %Concept{}} <- Concepts.delete_concept(concept) do + send_resp(conn, :no_content, "") + end + end +end diff --git a/lib/dbservice_web/controllers/curriculum_controller.ex b/lib/dbservice_web/controllers/curriculum_controller.ex new file mode 100644 index 00000000..94e12e20 --- /dev/null +++ b/lib/dbservice_web/controllers/curriculum_controller.ex @@ -0,0 +1,128 @@ +defmodule DbserviceWeb.CurriculumController do + use DbserviceWeb, :controller + + import Ecto.Query + alias Dbservice.Repo + alias Dbservice.Curriculums + alias Dbservice.Curriculums.Curriculum + + action_fallback(DbserviceWeb.FallbackController) + + use PhoenixSwagger + + alias DbserviceWeb.SwaggerSchema.Curriculum, as: SwaggerSchemaCurriculum + + def swagger_definitions do + Map.merge( + SwaggerSchemaCurriculum.curriculum(), + SwaggerSchemaCurriculum.curriculums() + ) + end + + swagger_path :index do + get("/api/curriculum") + + parameters do + params(:query, :string, "The name of the curriculum", + required: false, + name: "name" + ) + + params(:query, :string, "The code of the curriculum", required: false, name: "code") + end + + response(200, "OK", Schema.ref(:Curriculums)) + end + + def index(conn, params) do + query = + from(m in Curriculum, + order_by: [asc: m.id], + offset: ^params["offset"], + limit: ^params["limit"] + ) + + query = + Enum.reduce(params, query, fn {key, value}, acc -> + case String.to_existing_atom(key) do + :offset -> acc + :limit -> acc + atom -> from(u in acc, where: field(u, ^atom) == ^value) + end + end) + + curriculum = Repo.all(query) + render(conn, "index.json", curriculum: curriculum) + end + + swagger_path :create do + post("/api/curriculum") + + parameters do + body(:body, Schema.ref(:Curriculum), "Curriculum to create", required: true) + end + + response(201, "Created", Schema.ref(:Curriculum)) + end + + def create(conn, params) do + with {:ok, %Curriculum{} = curriculum} <- Curriculums.create_curriculum(params) do + conn + |> put_status(:created) + |> put_resp_header("location", Routes.curriculum_path(conn, :show, curriculum)) + |> render("show.json", curriculum: curriculum) + end + end + + swagger_path :show do + get("/api/curriculum/{curriculumId}") + + parameters do + curriculumId(:path, :integer, "The id of the curriculum record", required: true) + end + + response(200, "OK", Schema.ref(:Curriculum)) + end + + def show(conn, %{"id" => id}) do + curriculum = Curriculums.get_curriculum!(id) + render(conn, "show.json", curriculum: curriculum) + end + + swagger_path :update do + patch("/api/curriculum/{curriculumId}") + + parameters do + curriculumId(:path, :integer, "The id of the curriculum record", required: true) + body(:body, Schema.ref(:Curriculum), "Curriculum to create", required: true) + end + + response(200, "Updated", Schema.ref(:Curriculum)) + end + + def update(conn, params) do + curriculum = Curriculums.get_curriculum!(params["id"]) + + with {:ok, %Curriculum{} = curriculum} <- Curriculums.update_curriculum(curriculum, params) do + render(conn, "show.json", curriculum: curriculum) + end + end + + swagger_path :delete do + PhoenixSwagger.Path.delete("/api/curriculum/{curriculumId}") + + parameters do + curriculumId(:path, :integer, "The id of the curriculum record", required: true) + end + + response(204, "No Content") + end + + def delete(conn, %{"id" => id}) do + curriculum = Curriculums.get_curriculum!(id) + + with {:ok, %Curriculum{}} <- Curriculums.delete_curriculum(curriculum) do + send_resp(conn, :no_content, "") + end + end +end diff --git a/lib/dbservice_web/controllers/grade_controller.ex b/lib/dbservice_web/controllers/grade_controller.ex new file mode 100644 index 00000000..99bfc83a --- /dev/null +++ b/lib/dbservice_web/controllers/grade_controller.ex @@ -0,0 +1,126 @@ +defmodule DbserviceWeb.GradeController do + use DbserviceWeb, :controller + + import Ecto.Query + alias Dbservice.Repo + alias Dbservice.Grades + alias Dbservice.Grades.Grade + + action_fallback(DbserviceWeb.FallbackController) + + use PhoenixSwagger + + alias DbserviceWeb.SwaggerSchema.Grade, as: SwaggerSchemaGrade + + def swagger_definitions do + Map.merge( + SwaggerSchemaGrade.grade(), + SwaggerSchemaGrade.grades() + ) + end + + swagger_path :index do + get("/api/grade") + + parameters do + params(:query, :string, "The grade in school", + required: false, + name: "number" + ) + end + + response(200, "OK", Schema.ref(:Grades)) + end + + def index(conn, params) do + query = + from(m in Grade, + order_by: [asc: m.id], + offset: ^params["offset"], + limit: ^params["limit"] + ) + + query = + Enum.reduce(params, query, fn {key, value}, acc -> + case String.to_existing_atom(key) do + :offset -> acc + :limit -> acc + atom -> from(u in acc, where: field(u, ^atom) == ^value) + end + end) + + grade = Repo.all(query) + render(conn, "index.json", grade: grade) + end + + swagger_path :create do + post("/api/grade") + + parameters do + body(:body, Schema.ref(:Grade), "Grade to create", required: true) + end + + response(201, "Created", Schema.ref(:Grade)) + end + + def create(conn, params) do + with {:ok, %Grade{} = grade} <- Grades.create_grade(params) do + conn + |> put_status(:created) + |> put_resp_header("location", Routes.grade_path(conn, :show, grade)) + |> render("show.json", grade: grade) + end + end + + swagger_path :show do + get("/api/grade/{gradeId}") + + parameters do + gradeId(:path, :integer, "The id of the grade record", required: true) + end + + response(200, "OK", Schema.ref(:Grade)) + end + + def show(conn, %{"id" => id}) do + grade = Grades.get_grade!(id) + render(conn, "show.json", grade: grade) + end + + swagger_path :update do + patch("/api/grade/{gradeId}") + + parameters do + gradeId(:path, :integer, "The id of the grade record", required: true) + body(:body, Schema.ref(:Grade), "Grade to create", required: true) + end + + response(200, "Updated", Schema.ref(:Grade)) + end + + def update(conn, params) do + grade = Grades.get_grade!(params["id"]) + + with {:ok, %Grade{} = grade} <- Grades.update_grade(grade, params) do + render(conn, "show.json", grade: grade) + end + end + + swagger_path :delete do + PhoenixSwagger.Path.delete("/api/grade/{gradeId}") + + parameters do + gradeId(:path, :integer, "The id of the grade record", required: true) + end + + response(204, "No Content") + end + + def delete(conn, %{"id" => id}) do + grade = Grades.get_grade!(id) + + with {:ok, %Grade{}} <- Grades.delete_grade(grade) do + send_resp(conn, :no_content, "") + end + end +end diff --git a/lib/dbservice_web/controllers/learning_objective_controller.ex b/lib/dbservice_web/controllers/learning_objective_controller.ex new file mode 100644 index 00000000..60b9f48a --- /dev/null +++ b/lib/dbservice_web/controllers/learning_objective_controller.ex @@ -0,0 +1,126 @@ +defmodule DbserviceWeb.LearningObjectiveController do + use DbserviceWeb, :controller + + import Ecto.Query + alias Dbservice.Repo + alias Dbservice.LearningObjectives + alias Dbservice.LearningObjectives.LearningObjective + + action_fallback(DbserviceWeb.FallbackController) + + use PhoenixSwagger + + alias DbserviceWeb.SwaggerSchema.LearningObjective, as: SwaggerSchemaLearningObjective + + def swagger_definitions do + Map.merge( + SwaggerSchemaLearningObjective.learning_objective(), + SwaggerSchemaLearningObjective.learning_objectives() + ) + end + + swagger_path :index do + get("/api/learning-objective") + + parameters do + params(:query, :string, "The learning objective of a topic", + required: false, + name: "title" + ) + end + + response(200, "OK", Schema.ref(:LearningObjectives)) + end + + def index(conn, params) do + query = + from(m in LearningObjective, + order_by: [asc: m.id], + offset: ^params["offset"], + limit: ^params["limit"] + ) + + query = + Enum.reduce(params, query, fn {key, value}, acc -> + case String.to_existing_atom(key) do + :offset -> acc + :limit -> acc + atom -> from(u in acc, where: field(u, ^atom) == ^value) + end + end) + + learning_objective = Repo.all(query) + render(conn, "index.json", learning_objective: learning_objective) + end + + swagger_path :create do + post("/api/learning-objective") + + parameters do + body(:body, Schema.ref(:LearningObjective), "Learning objective to create", required: true) + end + + response(201, "Created", Schema.ref(:LearningObjective)) + end + + def create(conn, params) do + with {:ok, %LearningObjective{} = learning_objective} <- LearningObjectives.create_learning_objective(params) do + conn + |> put_status(:created) + |> put_resp_header("location", Routes.learning_objective_path(conn, :show, learning_objective)) + |> render("show.json", learning_objective: learning_objective) + end + end + + swagger_path :show do + get("/api/learning-objective/{learningObjectiveId}") + + parameters do + learningObjectiveId(:path, :integer, "The id of the learning_objective record", required: true) + end + + response(200, "OK", Schema.ref(:LearningObjective)) + end + + def show(conn, %{"id" => id}) do + learning_objective = LearningObjectives.get_learning_objective!(id) + render(conn, "show.json", learning_objective: learning_objective) + end + + swagger_path :update do + patch("/api/learning-objective/{learningObjectiveId}") + + parameters do + learningObjectiveId(:path, :integer, "The id of the learning objective record", required: true) + body(:body, Schema.ref(:LearningObjective), "LearningObjective to create", required: true) + end + + response(200, "Updated", Schema.ref(:LearningObjective)) + end + + def update(conn, params) do + learning_objective = LearningObjectives.get_learning_objective!(params["id"]) + + with {:ok, %LearningObjective{} = learning_objective} <- LearningObjectives.update_learning_objective(learning_objective, params) do + render(conn, "show.json", learning_objective: learning_objective) + end + end + + swagger_path :delete do + PhoenixSwagger.Path.delete("/api/learning-objective/{learningObjectiveId}") + + parameters do + learningObjectiveId(:path, :integer, "The id of the learning objective", required: true) + end + + response(204, "No Content") + end + + def delete(conn, %{"id" => id}) do + learning_objective = LearningObjectives.get_learning_objective!(id) + + with {:ok, %LearningObjective{}} <- LearningObjectives.delete_learning_objective(learning_objective) do + send_resp(conn, :no_content, "") + end + end +end diff --git a/lib/dbservice_web/controllers/purpose_controller.ex b/lib/dbservice_web/controllers/purpose_controller.ex new file mode 100644 index 00000000..c68004b5 --- /dev/null +++ b/lib/dbservice_web/controllers/purpose_controller.ex @@ -0,0 +1,126 @@ +defmodule DbserviceWeb.PurposeController do + use DbserviceWeb, :controller + + import Ecto.Query + alias Dbservice.Repo + alias Dbservice.Purposes + alias Dbservice.Purposes.Purpose + + action_fallback(DbserviceWeb.FallbackController) + + use PhoenixSwagger + + alias DbserviceWeb.SwaggerSchema.Purpose, as: SwaggerSchemaPurpose + + def swagger_definitions do + Map.merge( + SwaggerSchemaPurpose.purpose(), + SwaggerSchemaPurpose.purposes() + ) + end + + swagger_path :index do + get("/api/purpose") + + parameters do + params(:query, :string, "The purpose of the content", + required: false, + name: "name" + ) + end + + response(200, "OK", Schema.ref(:Purposes)) + end + + def index(conn, params) do + query = + from(m in Purpose, + order_by: [asc: m.id], + offset: ^params["offset"], + limit: ^params["limit"] + ) + + query = + Enum.reduce(params, query, fn {key, value}, acc -> + case String.to_existing_atom(key) do + :offset -> acc + :limit -> acc + atom -> from(u in acc, where: field(u, ^atom) == ^value) + end + end) + + purpose = Repo.all(query) + render(conn, "index.json", purpose: purpose) + end + + swagger_path :create do + post("/api/purpose") + + parameters do + body(:body, Schema.ref(:Purpose), "Purpose to create", required: true) + end + + response(201, "Created", Schema.ref(:Purpose)) + end + + def create(conn, params) do + with {:ok, %Purpose{} = purpose} <- Purposes.create_purpose(params) do + conn + |> put_status(:created) + |> put_resp_header("location", Routes.purpose_path(conn, :show, purpose)) + |> render("show.json", purpose: purpose) + end + end + + swagger_path :show do + get("/api/purpose/{purposeId}") + + parameters do + purposeId(:path, :integer, "The id of the purpose record", required: true) + end + + response(200, "OK", Schema.ref(:Purpose)) + end + + def show(conn, %{"id" => id}) do + purpose = Purposes.get_purpose!(id) + render(conn, "show.json", purpose: purpose) + end + + swagger_path :update do + patch("/api/purpose/{purposeId}") + + parameters do + purposeId(:path, :integer, "The id of the purpose record", required: true) + body(:body, Schema.ref(:Purpose), "Purpose to create", required: true) + end + + response(200, "Updated", Schema.ref(:Purpose)) + end + + def update(conn, params) do + purpose = Purposes.get_purpose!(params["id"]) + + with {:ok, %Purpose{} = purpose} <- Purposes.update_purpose(purpose, params) do + render(conn, "show.json", purpose: purpose) + end + end + + swagger_path :delete do + PhoenixSwagger.Path.delete("/api/purpose/{purposeId}") + + parameters do + purposeId(:path, :integer, "The id of the purpose record", required: true) + end + + response(204, "No Content") + end + + def delete(conn, %{"id" => id}) do + purpose = Purposes.get_purpose!(id) + + with {:ok, %Purpose{}} <- Purposes.delete_purpose(purpose) do + send_resp(conn, :no_content, "") + end + end +end diff --git a/lib/dbservice_web/controllers/resource_controller.ex b/lib/dbservice_web/controllers/resource_controller.ex new file mode 100644 index 00000000..314d1db6 --- /dev/null +++ b/lib/dbservice_web/controllers/resource_controller.ex @@ -0,0 +1,126 @@ +defmodule DbserviceWeb.ResourceController do + use DbserviceWeb, :controller + + import Ecto.Query + alias Dbservice.Repo + alias Dbservice.Resources + alias Dbservice.Resources.Resource + + action_fallback(DbserviceWeb.FallbackController) + + use PhoenixSwagger + + alias DbserviceWeb.SwaggerSchema.Resource, as: SwaggerSchemaResource + + def swagger_definitions do + Map.merge( + SwaggerSchemaResource.resource(), + SwaggerSchemaResource.resources() + ) + end + + swagger_path :index do + get("/api/resource") + + parameters do + params(:query, :string, "The resource of the content", + required: false, + name: "name" + ) + end + + response(200, "OK", Schema.ref(:Resources)) + end + + def index(conn, params) do + query = + from(m in Resource, + order_by: [asc: m.id], + offset: ^params["offset"], + limit: ^params["limit"] + ) + + query = + Enum.reduce(params, query, fn {key, value}, acc -> + case String.to_existing_atom(key) do + :offset -> acc + :limit -> acc + atom -> from(u in acc, where: field(u, ^atom) == ^value) + end + end) + + resource = Repo.all(query) + render(conn, "index.json", resource: resource) + end + + swagger_path :create do + post("/api/resource") + + parameters do + body(:body, Schema.ref(:Resource), "Resource to create", required: true) + end + + response(201, "Created", Schema.ref(:Resource)) + end + + def create(conn, params) do + with {:ok, %Resource{} = resource} <- Resources.create_resource(params) do + conn + |> put_status(:created) + |> put_resp_header("location", Routes.resource_path(conn, :show, resource)) + |> render("show.json", resource: resource) + end + end + + swagger_path :show do + get("/api/resource/{resourceId}") + + parameters do + resourceId(:path, :integer, "The id of the resource record", required: true) + end + + response(200, "OK", Schema.ref(:Resource)) + end + + def show(conn, %{"id" => id}) do + resource = Resources.get_resource!(id) + render(conn, "show.json", resource: resource) + end + + swagger_path :update do + patch("/api/resource/{resourceId}") + + parameters do + resourceId(:path, :integer, "The id of the resource record", required: true) + body(:body, Schema.ref(:Resource), "Resource to create", required: true) + end + + response(200, "Updated", Schema.ref(:Resource)) + end + + def update(conn, params) do + resource = Resources.get_resource!(params["id"]) + + with {:ok, %Resource{} = resource} <- Resources.update_resource(resource, params) do + render(conn, "show.json", resource: resource) + end + end + + swagger_path :delete do + PhoenixSwagger.Path.delete("/api/resource/{resourceId}") + + parameters do + resourceId(:path, :integer, "The id of the resource record", required: true) + end + + response(204, "No Content") + end + + def delete(conn, %{"id" => id}) do + resource = Resources.get_resource!(id) + + with {:ok, %Resource{}} <- Resources.delete_resource(resource) do + send_resp(conn, :no_content, "") + end + end +end diff --git a/lib/dbservice_web/controllers/source_controller.ex b/lib/dbservice_web/controllers/source_controller.ex new file mode 100644 index 00000000..8d9516d9 --- /dev/null +++ b/lib/dbservice_web/controllers/source_controller.ex @@ -0,0 +1,131 @@ +defmodule DbserviceWeb.SourceController do + use DbserviceWeb, :controller + + import Ecto.Query + alias Dbservice.Repo + alias Dbservice.Sources + alias Dbservice.Sources.Source + + action_fallback(DbserviceWeb.FallbackController) + + use PhoenixSwagger + + alias DbserviceWeb.SwaggerSchema.Source, as: SwaggerSchemaSource + + def swagger_definitions do + Map.merge( + SwaggerSchemaSource.source(), + SwaggerSchemaSource.sources() + ) + end + + swagger_path :index do + get("/api/source") + + parameters do + params(:query, :string, "The source of the content", + required: false, + name: "name" + ) + + params(:query, :string, "The link of the source", + required: false, + name: "link" + ) + end + + response(200, "OK", Schema.ref(:Sources)) + end + + def index(conn, params) do + query = + from(m in Source, + order_by: [asc: m.id], + offset: ^params["offset"], + limit: ^params["limit"] + ) + + query = + Enum.reduce(params, query, fn {key, value}, acc -> + case String.to_existing_atom(key) do + :offset -> acc + :limit -> acc + atom -> from(u in acc, where: field(u, ^atom) == ^value) + end + end) + + source = Repo.all(query) + render(conn, "index.json", source: source) + end + + swagger_path :create do + post("/api/source") + + parameters do + body(:body, Schema.ref(:Source), "Source to create", required: true) + end + + response(201, "Created", Schema.ref(:Source)) + end + + def create(conn, params) do + with {:ok, %Source{} = source} <- Sources.create_source(params) do + conn + |> put_status(:created) + |> put_resp_header("location", Routes.source_path(conn, :show, source)) + |> render("show.json", source: source) + end + end + + swagger_path :show do + get("/api/source/{sourceId}") + + parameters do + sourceId(:path, :integer, "The id of the source record", required: true) + end + + response(200, "OK", Schema.ref(:Source)) + end + + def show(conn, %{"id" => id}) do + source = Sources.get_source!(id) + render(conn, "show.json", source: source) + end + + swagger_path :update do + patch("/api/source/{sourceId}") + + parameters do + sourceId(:path, :integer, "The id of the source record", required: true) + body(:body, Schema.ref(:Source), "Source to create", required: true) + end + + response(200, "Updated", Schema.ref(:Source)) + end + + def update(conn, params) do + source = Sources.get_source!(params["id"]) + + with {:ok, %Source{} = source} <- Sources.update_source(source, params) do + render(conn, "show.json", source: source) + end + end + + swagger_path :delete do + PhoenixSwagger.Path.delete("/api/source/{sourceId}") + + parameters do + sourceId(:path, :integer, "The id of the source record", required: true) + end + + response(204, "No Content") + end + + def delete(conn, %{"id" => id}) do + source = Sources.get_source!(id) + + with {:ok, %Source{}} <- Sources.delete_source(source) do + send_resp(conn, :no_content, "") + end + end +end diff --git a/lib/dbservice_web/controllers/subject_controller.ex b/lib/dbservice_web/controllers/subject_controller.ex new file mode 100644 index 00000000..5c829eaf --- /dev/null +++ b/lib/dbservice_web/controllers/subject_controller.ex @@ -0,0 +1,131 @@ +defmodule DbserviceWeb.SubjectController do + use DbserviceWeb, :controller + + import Ecto.Query + alias Dbservice.Repo + alias Dbservice.Subjects + alias Dbservice.Subjects.Subject + + action_fallback(DbserviceWeb.FallbackController) + + use PhoenixSwagger + + alias DbserviceWeb.SwaggerSchema.Subject, as: SwaggerSchemaSubject + + def swagger_definitions do + Map.merge( + SwaggerSchemaSubject.subject(), + SwaggerSchemaSubject.subjects() + ) + end + + swagger_path :index do + get("/api/subject") + + parameters do + params(:query, :string, "The subject of a grade", + required: false, + name: "name" + ) + + params(:query, :string, "The code of the subject", + required: false, + name: "code" + ) + end + + response(200, "OK", Schema.ref(:Subjects)) + end + + def index(conn, params) do + query = + from(m in Subject, + order_by: [asc: m.id], + offset: ^params["offset"], + limit: ^params["limit"] + ) + + query = + Enum.reduce(params, query, fn {key, value}, acc -> + case String.to_existing_atom(key) do + :offset -> acc + :limit -> acc + atom -> from(u in acc, where: field(u, ^atom) == ^value) + end + end) + + subject = Repo.all(query) + render(conn, "index.json", subject: subject) + end + + swagger_path :create do + post("/api/subject") + + parameters do + body(:body, Schema.ref(:Subject), "Subject to create", required: true) + end + + response(201, "Created", Schema.ref(:Subject)) + end + + def create(conn, params) do + with {:ok, %Subject{} = subject} <- Subjects.create_subject(params) do + conn + |> put_status(:created) + |> put_resp_header("location", Routes.subject_path(conn, :show, subject)) + |> render("show.json", subject: subject) + end + end + + swagger_path :show do + get("/api/subject/{subjectId}") + + parameters do + subjectId(:path, :integer, "The id of the subject record", required: true) + end + + response(200, "OK", Schema.ref(:Subject)) + end + + def show(conn, %{"id" => id}) do + subject = Subjects.get_subject!(id) + render(conn, "show.json", subject: subject) + end + + swagger_path :update do + patch("/api/subject/{subjectId}") + + parameters do + subjectId(:path, :integer, "The id of the subject record", required: true) + body(:body, Schema.ref(:Subject), "Subject to create", required: true) + end + + response(200, "Updated", Schema.ref(:Subject)) + end + + def update(conn, params) do + subject = Subjects.get_subject!(params["id"]) + + with {:ok, %Subject{} = subject} <- Subjects.update_subject(subject, params) do + render(conn, "show.json", subject: subject) + end + end + + swagger_path :delete do + PhoenixSwagger.Path.delete("/api/subject/{subjectId}") + + parameters do + subjectId(:path, :integer, "The id of the subject record", required: true) + end + + response(204, "No Content") + end + + def delete(conn, %{"id" => id}) do + subject = Subjects.get_subject!(id) + + with {:ok, %Subject{}} <- Subjects.delete_subject(subject) do + send_resp(conn, :no_content, "") + end + end +end diff --git a/lib/dbservice_web/controllers/tag_controller.ex b/lib/dbservice_web/controllers/tag_controller.ex new file mode 100644 index 00000000..3c22fc06 --- /dev/null +++ b/lib/dbservice_web/controllers/tag_controller.ex @@ -0,0 +1,127 @@ +defmodule DbserviceWeb.TagController do + use DbserviceWeb, :controller + + import Ecto.Query + alias Dbservice.Repo + alias Dbservice.Tags + alias Dbservice.Tags.Tag + + action_fallback DbserviceWeb.FallbackController + + use PhoenixSwagger + + alias DbserviceWeb.SwaggerSchema.Tag, as: SwaggerSchemaTag + + def swagger_definitions do + Map.merge( + SwaggerSchemaTag.tag(), + SwaggerSchemaTag.tags() + ) + end + + swagger_path :index do + get("/api/tag") + + parameters do + params(:query, :string, "The name of the tag", + required: false, + name: "name" + ) + + params(:query, :string, "The description of the tag", required: false, name: "description") + end + + response(200, "OK", Schema.ref(:Tags)) + end + + def index(conn, params) do + query = + from m in Tag, + order_by: [asc: m.id], + offset: ^params["offset"], + limit: ^params["limit"] + + query = + Enum.reduce(params, query, fn {key, value}, acc -> + case String.to_existing_atom(key) do + :offset -> acc + :limit -> acc + atom -> from u in acc, where: field(u, ^atom) == ^value + end + end) + + tag = Repo.all(query) + render(conn, "index.json", tag: tag) + end + + swagger_path :create do + post("/api/tag") + + parameters do + body(:body, Schema.ref(:Tag), "Tag to create", required: true) + end + + response(201, "Created", Schema.ref(:Tag)) + end + + def create(conn, params) do + with {:ok, %Tag{} = tag} <- Tags.create_tag(params) do + conn + |> put_status(:created) + |> put_resp_header("location", Routes.tag_path(conn, :show, tag)) + |> render("show.json", tag: tag) + end + end + + swagger_path :show do + get("/api/tag/{tagId}") + + parameters do + tagId(:path, :integer, "The id of the tag record", required: true) + end + + response(200, "OK", Schema.ref(:Tag)) + end + + def show(conn, %{"id" => id}) do + tag = Tags.get_tag!(id) + render(conn, "show.json", tag: tag) + end + + swagger_path :update do + patch("/api/tag/{tagId}") + + parameters do + tagId(:path, :integer, "The id of the tag record", required: true) + body(:body, Schema.ref(:Tag), "Tag to create", required: true) + end + + response(200, "Updated", Schema.ref(:Tag)) + end + + def update(conn, params) do + tag = Tags.get_tag!(params["id"]) + + with {:ok, %Tag{} = tag} <- Tags.update_tag(tag, params) do + render(conn, "show.json", tag: tag) + end + end + + swagger_path :delete do + PhoenixSwagger.Path.delete("/api/tag/{tagId}") + + parameters do + tagId(:path, :integer, "The id of the tag record", required: true) + end + + response(204, "No Content") + end + + def delete(conn, %{"id" => id}) do + tag = Tags.get_tag!(id) + + with {:ok, %Tag{}} <- Tags.delete_tag(tag) do + send_resp(conn, :no_content, "") + end + end +end diff --git a/lib/dbservice_web/controllers/topic_controller.ex b/lib/dbservice_web/controllers/topic_controller.ex new file mode 100644 index 00000000..feb9444a --- /dev/null +++ b/lib/dbservice_web/controllers/topic_controller.ex @@ -0,0 +1,132 @@ +defmodule DbserviceWeb.TopicController do + use DbserviceWeb, :controller + + import Ecto.Query + alias Dbservice.Repo + alias Dbservice.Topics + alias Dbservice.Topics.Topic + + action_fallback(DbserviceWeb.FallbackController) + + use PhoenixSwagger + + alias DbserviceWeb.SwaggerSchema.Topic, as: SwaggerSchemaTopic + + def swagger_definitions do + Map.merge( + SwaggerSchemaTopic.topic(), + SwaggerSchemaTopic.topics() + ) + end + + swagger_path :index do + get("/api/topic") + + parameters do + params(:query, :string, "The topic of a chapter", + required: false, + name: "name" + ) + + params(:query, :string, "The code of the topic", + required: false, + name: "code" + ) + end + + response(200, "OK", Schema.ref(:Topics)) + end + + def index(conn, params) do + query = + from(m in Topic, + order_by: [asc: m.id], + offset: ^params["offset"], + limit: ^params["limit"] + ) + |> Ecto.Query.preload(:tag) + + query = + Enum.reduce(params, query, fn {key, value}, acc -> + case String.to_existing_atom(key) do + :offset -> acc + :limit -> acc + atom -> from(u in acc, where: field(u, ^atom) == ^value) + end + end) + + topic = Repo.all(query) + render(conn, "index.json", topic: topic) + end + + swagger_path :create do + post("/api/topic") + + parameters do + body(:body, Schema.ref(:Topic), "Topic to create", required: true) + end + + response(201, "Created", Schema.ref(:Topic)) + end + + def create(conn, params) do + with {:ok, %Topic{} = topic} <- Topics.create_topic(params) do + conn + |> put_status(:created) + |> put_resp_header("location", Routes.topic_path(conn, :show, topic)) + |> render("show.json", topic: topic) + end + end + + swagger_path :show do + get("/api/topic/{topicId}") + + parameters do + topicId(:path, :integer, "The id of the topic record", required: true) + end + + response(200, "OK", Schema.ref(:Topic)) + end + + def show(conn, %{"id" => id}) do + topic = Topics.get_topic!(id) + render(conn, "show.json", topic: topic) + end + + swagger_path :update do + patch("/api/topic/{topicId}") + + parameters do + topicId(:path, :integer, "The id of the topic record", required: true) + body(:body, Schema.ref(:Topic), "Topic to create", required: true) + end + + response(200, "Updated", Schema.ref(:Topic)) + end + + def update(conn, params) do + topic = Topics.get_topic!(params["id"]) + + with {:ok, %Topic{} = topic} <- Topics.update_topic(topic, params) do + render(conn, "show.json", topic: topic) + end + end + + swagger_path :delete do + PhoenixSwagger.Path.delete("/api/topic/{topicId}") + + parameters do + topicId(:path, :integer, "The id of the topic record", required: true) + end + + response(204, "No Content") + end + + def delete(conn, %{"id" => id}) do + topic = Topics.get_topic!(id) + + with {:ok, %Topic{}} <- Topics.delete_topic(topic) do + send_resp(conn, :no_content, "") + end + end +end diff --git a/lib/dbservice_web/router.ex b/lib/dbservice_web/router.ex index b77380e1..1b05833b 100644 --- a/lib/dbservice_web/router.ex +++ b/lib/dbservice_web/router.ex @@ -36,6 +36,17 @@ defmodule DbserviceWeb.Router do resources("/group-type", GroupTypeController, except: [:new, :edit]) resources("/form-schema", FormSchemaController) resources("/group-user", GroupUserController) + resources("/tag", TagController, except: [:new, :edit]) + resources("/curriculum", CurriculumController, except: [:new, :edit]) + resources("/grade", GradeController, except: [:new, :edit]) + resources("/subject", SubjectController, except: [:new, :edit]) + resources("/chapter", ChapterController, except: [:new, :edit]) + resources("/topic", TopicController, except: [:new, :edit]) + resources("/concept", ConceptController, except: [:new, :edit]) + resources("/learning-objective", LearningObjectiveController, except: [:new, :edit]) + resources("/source", SourceController, except: [:new, :edit]) + resources("/purpose", PurposeController, except: [:new, :edit]) + resources("/resource", ResourceController, except: [:new, :edit]) def swagger_info do source(["config/.env", "config/.env"]) diff --git a/lib/dbservice_web/swagger_schemas/chapter.ex b/lib/dbservice_web/swagger_schemas/chapter.ex new file mode 100644 index 00000000..dcce0cca --- /dev/null +++ b/lib/dbservice_web/swagger_schemas/chapter.ex @@ -0,0 +1,43 @@ +defmodule DbserviceWeb.SwaggerSchema.Chapter do + @moduledoc false + + use PhoenixSwagger + + def chapter do + %{ + Chapter: + swagger_schema do + title("Chapter") + description("A chapter in the application") + + properties do + name(:string, "Chapter name") + code(:string, "Chapter Code") + grade_id(:integer, "Grade id associated with the chapter") + subject_id(:integer, "Subject id associated with the chapter") + tag_id(:integer, "Tag id associated with the chapter") + end + + example(%{ + name: "हमारे आस-पास के पदार्थ | Matter in Our Surroundings", + code: "9C01", + grade_id: 1, + subject_id: 1, + tag_id: 4 + }) + end + } + end + + def chapters do + %{ + Chapters: + swagger_schema do + title("Chapters") + description("All chapters in the application") + type(:array) + items(Schema.ref(:Chapter)) + end + } + end +end diff --git a/lib/dbservice_web/swagger_schemas/concept.ex b/lib/dbservice_web/swagger_schemas/concept.ex new file mode 100644 index 00000000..4d074dcb --- /dev/null +++ b/lib/dbservice_web/swagger_schemas/concept.ex @@ -0,0 +1,39 @@ +defmodule DbserviceWeb.SwaggerSchema.Concept do + @moduledoc false + + use PhoenixSwagger + + def concept do + %{ + Concept: + swagger_schema do + title("Concept") + description("A concept in the application") + + properties do + name(:string, "Concept name") + topic_id(:integer, "Topic id associated with the concept") + tag_id(:integer, "Tag id associated with the concept") + end + + example(%{ + name: "Coulomb's Law", + topic_id: 5, + tag_id: 6 + }) + end + } + end + + def concepts do + %{ + Concepts: + swagger_schema do + title("Concepts") + description("All concepts in the application") + type(:array) + items(Schema.ref(:Concept)) + end + } + end +end diff --git a/lib/dbservice_web/swagger_schemas/curriculum.ex b/lib/dbservice_web/swagger_schemas/curriculum.ex new file mode 100644 index 00000000..20c9e592 --- /dev/null +++ b/lib/dbservice_web/swagger_schemas/curriculum.ex @@ -0,0 +1,39 @@ +defmodule DbserviceWeb.SwaggerSchema.Curriculum do + @moduledoc false + + use PhoenixSwagger + + def curriculum do + %{ + Curriculum: + swagger_schema do + title("Curriculum") + description("A curriculum in the application") + + properties do + name(:string, "Name of the curriculum") + code(:string, "Code of the curriculum") + tag_id(:integer, "Tag id associated with the curriculum") + end + + example(%{ + name: "Sankalp", + code: "S-10", + tag_id: 1 + }) + end + } + end + + def curriculums do + %{ + Curriculums: + swagger_schema do + title("Curriculums") + description("All Curriculums in the application") + type(:array) + items(Schema.ref(:Curriculum)) + end + } + end +end diff --git a/lib/dbservice_web/swagger_schemas/grade.ex b/lib/dbservice_web/swagger_schemas/grade.ex new file mode 100644 index 00000000..d22b9a58 --- /dev/null +++ b/lib/dbservice_web/swagger_schemas/grade.ex @@ -0,0 +1,37 @@ +defmodule DbserviceWeb.SwaggerSchema.Grade do + @moduledoc false + + use PhoenixSwagger + + def grade do + %{ + Grade: + swagger_schema do + title("Grade") + description("A grade in the application") + + properties do + number(:integer, "Grade in school") + tag_id(:integer, "Tag id associated with the grade") + end + + example(%{ + number: 12, + tag_id: 1 + }) + end + } + end + + def grades do + %{ + Grades: + swagger_schema do + title("Grades") + description("All grades in the application") + type(:array) + items(Schema.ref(:Grade)) + end + } + end +end diff --git a/lib/dbservice_web/swagger_schemas/learning_objective.ex b/lib/dbservice_web/swagger_schemas/learning_objective.ex new file mode 100644 index 00000000..51dd3d4f --- /dev/null +++ b/lib/dbservice_web/swagger_schemas/learning_objective.ex @@ -0,0 +1,39 @@ +defmodule DbserviceWeb.SwaggerSchema.LearningObjective do + @moduledoc false + + use PhoenixSwagger + + def learning_objective do + %{ + LearningObjective: + swagger_schema do + title("LearningObjective") + description("A learning objective in the application") + + properties do + title(:string, "Title of the learning objective") + concept_id(:integer, "Concept id associated with the learning objective") + tag_id(:integer, "Tag id associated with the learning objective") + end + + example(%{ + title: "Understanding fundamental concept of Electromagnetism", + concept_id: 1, + tag_id: 6 + }) + end + } + end + + def learning_objectives do + %{ + LearningObjectives: + swagger_schema do + title("LearningObjectives") + description("All learning objectives in the application") + type(:array) + items(Schema.ref(:LearningObjective)) + end + } + end +end diff --git a/lib/dbservice_web/swagger_schemas/purpose.ex b/lib/dbservice_web/swagger_schemas/purpose.ex new file mode 100644 index 00000000..7d5ed90b --- /dev/null +++ b/lib/dbservice_web/swagger_schemas/purpose.ex @@ -0,0 +1,39 @@ +defmodule DbserviceWeb.SwaggerSchema.Purpose do + @moduledoc false + + use PhoenixSwagger + + def purpose do + %{ + Purpose: + swagger_schema do + title("Purpose") + description("A purpose in the application") + + properties do + name(:string, "Purpose name") + description(:text, "Purpose description") + tag_id(:integer, "Tag id associated with the purpose") + end + + example(%{ + name: "learningModule", + description: "workbook for the program", + tag_id: 6 + }) + end + } + end + + def purposes do + %{ + Purposes: + swagger_schema do + title("Purposes") + description("All purposes in the application") + type(:array) + items(Schema.ref(:Purpose)) + end + } + end +end diff --git a/lib/dbservice_web/swagger_schemas/resource.ex b/lib/dbservice_web/swagger_schemas/resource.ex new file mode 100644 index 00000000..1d4d4971 --- /dev/null +++ b/lib/dbservice_web/swagger_schemas/resource.ex @@ -0,0 +1,57 @@ +defmodule DbserviceWeb.SwaggerSchema.Resource do + @moduledoc false + + use PhoenixSwagger + + def resource do + %{ + Resource: + swagger_schema do + title("Resource") + description("A resource in the application") + + properties do + name(:string, "Resource name") + type(:string, "Resource type") + type_params(:map, "Parameters of the resource type") + difficulty_level(:string, "Difficulty level of a resource") + curriculum_id(:integer, "Curriculum id associated with the resource") + chapter_id(:integer, "Chapter id associated with the resource") + topic_id(:integer, "Topic id associated with the resource") + purpose_id(:integer, "Purpose id associated with the resource") + concept_id(:integer, "Concept id associated with the resource") + learning_objective_id(:integer, "Learning objective id associated with the resource") + tag_id(:integer, "Tag id associated with the resource") + end + + example(%{ + name: "1. 9C01 Introduction - हमारे आस पास के पदार्थ | Matter in our Surroundings", + type: "video", + type_params: %{ + "duration" => "45 minutes" + }, + difficulty_level: "medium", + curriculum_id: 1, + chapter_id: 1, + topic_id: 1, + purpose_id: 1, + concept_id: 1, + learning_objective_id: 1, + tag_id: 5 + }) + end + } + end + + def resources do + %{ + Resources: + swagger_schema do + title("Resources") + description("All resources in the application") + type(:array) + items(Schema.ref(:Resource)) + end + } + end +end diff --git a/lib/dbservice_web/swagger_schemas/source.ex b/lib/dbservice_web/swagger_schemas/source.ex new file mode 100644 index 00000000..a2ba6f77 --- /dev/null +++ b/lib/dbservice_web/swagger_schemas/source.ex @@ -0,0 +1,39 @@ +defmodule DbserviceWeb.SwaggerSchema.Source do + @moduledoc false + + use PhoenixSwagger + + def source do + %{ + Source: + swagger_schema do + title("Source") + description("A source in the application") + + properties do + name(:string, "Source name") + link(:text, "Source Link") + tag_id(:integer, "Tag id associated with the source") + end + + example(%{ + name: "youtube", + link: "https://www.youtube.com/watch?v=k01UwiIvo9o", + tag_id: 5 + }) + end + } + end + + def sources do + %{ + Sources: + swagger_schema do + title("Sources") + description("All sources in the application") + type(:array) + items(Schema.ref(:Source)) + end + } + end +end diff --git a/lib/dbservice_web/swagger_schemas/subject.ex b/lib/dbservice_web/swagger_schemas/subject.ex new file mode 100644 index 00000000..bbe1358c --- /dev/null +++ b/lib/dbservice_web/swagger_schemas/subject.ex @@ -0,0 +1,38 @@ +defmodule DbserviceWeb.SwaggerSchema.Subject do + @moduledoc false + + use PhoenixSwagger + + def subject do + %{ + Subject: + swagger_schema do + title("Subject") + description("A subject in the application") + + properties do + name(:string, "Subject name") + code(:string, "Subject Code") + tag_id(:integer, "Tag id associated with the subject") + end + + example(%{ + name: "Physics", + code: "P11" + }) + end + } + end + + def subjects do + %{ + Subjects: + swagger_schema do + title("Subjects") + description("All subjects in the application") + type(:array) + items(Schema.ref(:Subject)) + end + } + end +end diff --git a/lib/dbservice_web/swagger_schemas/tag.ex b/lib/dbservice_web/swagger_schemas/tag.ex new file mode 100644 index 00000000..a474f42c --- /dev/null +++ b/lib/dbservice_web/swagger_schemas/tag.ex @@ -0,0 +1,37 @@ +defmodule DbserviceWeb.SwaggerSchema.Tag do + @moduledoc false + + use PhoenixSwagger + + def tag do + %{ + Tag: + swagger_schema do + title("Tag") + description("A tag in the application") + + properties do + name(:string, "Name of the tag") + description(:string, "Description of the tag") + end + + example(%{ + name: "A tag for resource", + description: "Description of the tag" + }) + end + } + end + + def tags do + %{ + Tags: + swagger_schema do + title("Tags") + description("All tags in the application") + type(:array) + items(Schema.ref(:Tag)) + end + } + end +end diff --git a/lib/dbservice_web/swagger_schemas/topic.ex b/lib/dbservice_web/swagger_schemas/topic.ex new file mode 100644 index 00000000..cda349e3 --- /dev/null +++ b/lib/dbservice_web/swagger_schemas/topic.ex @@ -0,0 +1,43 @@ +defmodule DbserviceWeb.SwaggerSchema.Topic do + @moduledoc false + + use PhoenixSwagger + + def topic do + %{ + Topic: + swagger_schema do + title("Topic") + description("A topic in the application") + + properties do + name(:string, "Topic name") + code(:string, "Topic Code") + grade_id(:integer, "Grade id associated with the topic") + chapter_id(:integer, "Chapter id associated with the topic") + tag_id(:integer, "Tag id associated with the topic") + end + + example(%{ + name: "What is Matter?", + code: "9C01.1", + grade_id: 1, + subject_id: 1, + tag_id: 3 + }) + end + } + end + + def topics do + %{ + Topics: + swagger_schema do + title("Topics") + description("All topics in the application") + type(:array) + items(Schema.ref(:Topic)) + end + } + end +end diff --git a/lib/dbservice_web/views/chapter_view.ex b/lib/dbservice_web/views/chapter_view.ex new file mode 100644 index 00000000..6380b2c7 --- /dev/null +++ b/lib/dbservice_web/views/chapter_view.ex @@ -0,0 +1,23 @@ +defmodule DbserviceWeb.ChapterView do + use DbserviceWeb, :view + alias DbserviceWeb.ChapterView + + def render("index.json", %{chapter: chapter}) do + render_many(chapter, ChapterView, "chapter.json") + end + + def render("show.json", %{chapter: chapter}) do + render_one(chapter, ChapterView, "chapter.json") + end + + def render("chapter.json", %{chapter: chapter}) do + %{ + id: chapter.id, + name: chapter.name, + code: chapter.code, + grade_id: chapter.grade_id, + subject_id: chapter.subject_id, + tag_id: chapter.tag_id + } + end +end diff --git a/lib/dbservice_web/views/concept_view.ex b/lib/dbservice_web/views/concept_view.ex new file mode 100644 index 00000000..8153b707 --- /dev/null +++ b/lib/dbservice_web/views/concept_view.ex @@ -0,0 +1,21 @@ +defmodule DbserviceWeb.ConceptView do + use DbserviceWeb, :view + alias DbserviceWeb.ConceptView + + def render("index.json", %{concept: concept}) do + render_many(concept, ConceptView, "concept.json") + end + + def render("show.json", %{concept: concept}) do + render_one(concept, ConceptView, "concept.json") + end + + def render("concept.json", %{concept: concept}) do + %{ + id: concept.id, + name: concept.name, + topic_id: concept.topic_id, + tag_id: concept.tag_id + } + end +end diff --git a/lib/dbservice_web/views/curriculum_view.ex b/lib/dbservice_web/views/curriculum_view.ex new file mode 100644 index 00000000..5efb4e65 --- /dev/null +++ b/lib/dbservice_web/views/curriculum_view.ex @@ -0,0 +1,21 @@ +defmodule DbserviceWeb.CurriculumView do + use DbserviceWeb, :view + alias DbserviceWeb.CurriculumView + + def render("index.json", %{curriculum: curriculum}) do + render_many(curriculum, CurriculumView, "curriculum.json") + end + + def render("show.json", %{curriculum: curriculum}) do + render_one(curriculum, CurriculumView, "curriculum.json") + end + + def render("curriculum.json", %{curriculum: curriculum}) do + %{ + id: curriculum.id, + name: curriculum.name, + code: curriculum.code, + tag_id: curriculum.tag_id + } + end +end diff --git a/lib/dbservice_web/views/grade_view.ex b/lib/dbservice_web/views/grade_view.ex new file mode 100644 index 00000000..d4c77505 --- /dev/null +++ b/lib/dbservice_web/views/grade_view.ex @@ -0,0 +1,20 @@ +defmodule DbserviceWeb.GradeView do + use DbserviceWeb, :view + alias DbserviceWeb.GradeView + + def render("index.json", %{grade: grade}) do + render_many(grade, GradeView, "grade.json") + end + + def render("show.json", %{grade: grade}) do + render_one(grade, GradeView, "grade.json") + end + + def render("grade.json", %{grade: grade}) do + %{ + id: grade.id, + number: grade.number, + tag_id: grade.tag_id + } + end +end diff --git a/lib/dbservice_web/views/learning_objective_view.ex b/lib/dbservice_web/views/learning_objective_view.ex new file mode 100644 index 00000000..6b6adda2 --- /dev/null +++ b/lib/dbservice_web/views/learning_objective_view.ex @@ -0,0 +1,21 @@ +defmodule DbserviceWeb.LearningObjectiveView do + use DbserviceWeb, :view + alias DbserviceWeb.LearningObjectiveView + + def render("index.json", %{learning_objective: learning_objective}) do + render_many(learning_objective, LearningObjectiveView, "learning_objective.json") + end + + def render("show.json", %{learning_objective: learning_objective}) do + render_one(learning_objective, LearningObjectiveView, "learning_objective.json") + end + + def render("learning_objective.json", %{learning_objective: learning_objective}) do + %{ + id: learning_objective.id, + title: learning_objective.title, + concept_id: learning_objective.concept_id, + tag_id: learning_objective.tag_id + } + end +end diff --git a/lib/dbservice_web/views/purpose_view.ex b/lib/dbservice_web/views/purpose_view.ex new file mode 100644 index 00000000..17affe8f --- /dev/null +++ b/lib/dbservice_web/views/purpose_view.ex @@ -0,0 +1,21 @@ +defmodule DbserviceWeb.PurposeView do + use DbserviceWeb, :view + alias DbserviceWeb.PurposeView + + def render("index.json", %{purpose: purpose}) do + render_many(purpose, PurposeView, "purpose.json") + end + + def render("show.json", %{purpose: purpose}) do + render_one(purpose, PurposeView, "purpose.json") + end + + def render("purpose.json", %{purpose: purpose}) do + %{ + id: purpose.id, + name: purpose.name, + description: purpose.description, + tag_id: purpose.tag_id + } + end +end diff --git a/lib/dbservice_web/views/resource_view.ex b/lib/dbservice_web/views/resource_view.ex new file mode 100644 index 00000000..77f80307 --- /dev/null +++ b/lib/dbservice_web/views/resource_view.ex @@ -0,0 +1,30 @@ +defmodule DbserviceWeb.ResourceView do + use DbserviceWeb, :view + alias DbserviceWeb.ResourceView + + def render("index.json", %{resource: resource}) do + render_many(resource, ResourceView, "resource.json") + end + + def render("show.json", %{resource: resource}) do + render_one(resource, ResourceView, "resource.json") + end + + def render("resource.json", %{resource: resource}) do + %{ + id: resource.id, + name: resource.name, + type: resource.type, + type_params: resource.type_params, + difficulty_level: resource.difficulty_level, + curriculum_id: resource.curriculum_id, + chapter_id: resource.chapter_id, + topic_id: resource.topic_id, + source_id: resource.source_id, + purpose_id: resource.purpose_id, + concept_id: resource.concept_id, + learning_objective_id: resource.learning_objective_id, + tag_id: resource.tag_id + } + end +end diff --git a/lib/dbservice_web/views/source_view.ex b/lib/dbservice_web/views/source_view.ex new file mode 100644 index 00000000..de0e3012 --- /dev/null +++ b/lib/dbservice_web/views/source_view.ex @@ -0,0 +1,21 @@ +defmodule DbserviceWeb.SourceView do + use DbserviceWeb, :view + alias DbserviceWeb.SourceView + + def render("index.json", %{source: source}) do + render_many(source, SourceView, "source.json") + end + + def render("show.json", %{source: source}) do + render_one(source, SourceView, "source.json") + end + + def render("source.json", %{source: source}) do + %{ + id: source.id, + name: source.name, + link: source.link, + tag_id: source.tag_id + } + end +end diff --git a/lib/dbservice_web/views/subject_view.ex b/lib/dbservice_web/views/subject_view.ex new file mode 100644 index 00000000..63535111 --- /dev/null +++ b/lib/dbservice_web/views/subject_view.ex @@ -0,0 +1,21 @@ +defmodule DbserviceWeb.SubjectView do + use DbserviceWeb, :view + alias DbserviceWeb.SubjectView + + def render("index.json", %{subject: subject}) do + render_many(subject, SubjectView, "subject.json") + end + + def render("show.json", %{subject: subject}) do + render_one(subject, SubjectView, "subject.json") + end + + def render("subject.json", %{subject: subject}) do + %{ + id: subject.id, + name: subject.name, + code: subject.code, + tag_id: subject.tag_id + } + end +end diff --git a/lib/dbservice_web/views/tag_view.ex b/lib/dbservice_web/views/tag_view.ex new file mode 100644 index 00000000..2b026427 --- /dev/null +++ b/lib/dbservice_web/views/tag_view.ex @@ -0,0 +1,20 @@ +defmodule DbserviceWeb.TagView do + use DbserviceWeb, :view + alias DbserviceWeb.TagView + + def render("index.json", %{tag: tag}) do + render_many(tag, TagView, "tag.json") + end + + def render("show.json", %{tag: tag}) do + render_one(tag, TagView, "tag.json") + end + + def render("tag.json", %{tag: tag}) do + %{ + id: tag.id, + name: tag.name, + description: tag.description + } + end +end diff --git a/lib/dbservice_web/views/topic_view.ex b/lib/dbservice_web/views/topic_view.ex new file mode 100644 index 00000000..fb3125ac --- /dev/null +++ b/lib/dbservice_web/views/topic_view.ex @@ -0,0 +1,23 @@ +defmodule DbserviceWeb.TopicView do + use DbserviceWeb, :view + alias DbserviceWeb.TopicView + + def render("index.json", %{topic: topic}) do + render_many(topic, TopicView, "topic.json") + end + + def render("show.json", %{topic: topic}) do + render_one(topic, TopicView, "topic.json") + end + + def render("topic.json", %{topic: topic}) do + %{ + id: topic.id, + name: topic.name, + code: topic.code, + grade_id: topic.grade_id, + chapter_id: topic.chapter_id, + tag_id: topic.tag_id + } + end +end diff --git a/priv/repo/migrations/20231004104045_create_tag_table.exs b/priv/repo/migrations/20231004104045_create_tag_table.exs new file mode 100644 index 00000000..b37e561f --- /dev/null +++ b/priv/repo/migrations/20231004104045_create_tag_table.exs @@ -0,0 +1,12 @@ +defmodule Dbservice.Repo.Migrations.CreateTagTable do + use Ecto.Migration + + def change do + create table(:tag) do + add(:name, :string) + add(:description, :text) + + timestamps() + end + end +end diff --git a/priv/repo/migrations/20231004104058_create_curriculum_table.exs b/priv/repo/migrations/20231004104058_create_curriculum_table.exs new file mode 100644 index 00000000..939f9d38 --- /dev/null +++ b/priv/repo/migrations/20231004104058_create_curriculum_table.exs @@ -0,0 +1,13 @@ +defmodule Dbservice.Repo.Migrations.CreateCurriculumTable do + use Ecto.Migration + + def change do + create table(:curriculum) do + add(:name, :string) + add(:code, :string) + add(:tag_id, references(:tag, on_delete: :nothing)) + + timestamps() + end + end +end diff --git a/priv/repo/migrations/20231004104120_create_grade_table.exs b/priv/repo/migrations/20231004104120_create_grade_table.exs new file mode 100644 index 00000000..58822896 --- /dev/null +++ b/priv/repo/migrations/20231004104120_create_grade_table.exs @@ -0,0 +1,12 @@ +defmodule Dbservice.Repo.Migrations.CreateGradeTable do + use Ecto.Migration + + def change do + create table(:grade) do + add(:number, :integer) + add(:tag_id, references(:tag, on_delete: :nothing)) + + timestamps() + end + end +end diff --git a/priv/repo/migrations/20231004104137_create_subject_table.exs b/priv/repo/migrations/20231004104137_create_subject_table.exs new file mode 100644 index 00000000..894fb6a5 --- /dev/null +++ b/priv/repo/migrations/20231004104137_create_subject_table.exs @@ -0,0 +1,13 @@ +defmodule Dbservice.Repo.Migrations.CreateSubjectTable do + use Ecto.Migration + + def change do + create table(:subject) do + add(:name, :string) + add(:code, :string) + add(:tag_id, references(:tag, on_delete: :nothing)) + + timestamps() + end + end +end diff --git a/priv/repo/migrations/20231004104148_create_chapter_table.exs b/priv/repo/migrations/20231004104148_create_chapter_table.exs new file mode 100644 index 00000000..7b97bfd0 --- /dev/null +++ b/priv/repo/migrations/20231004104148_create_chapter_table.exs @@ -0,0 +1,15 @@ +defmodule Dbservice.Repo.Migrations.CreateChapterTable do + use Ecto.Migration + + def change do + create table(:chapter) do + add(:name, :string) + add(:code, :string) + add(:grade_id, references(:grade, on_delete: :nothing)) + add(:subject_id, references(:subject, on_delete: :nothing)) + add(:tag_id, references(:tag, on_delete: :nothing)) + + timestamps() + end + end +end diff --git a/priv/repo/migrations/20231004104203_create_topic_table.exs b/priv/repo/migrations/20231004104203_create_topic_table.exs new file mode 100644 index 00000000..db453779 --- /dev/null +++ b/priv/repo/migrations/20231004104203_create_topic_table.exs @@ -0,0 +1,15 @@ +defmodule Dbservice.Repo.Migrations.CreateTopicTable do + use Ecto.Migration + + def change do + create table(:topic) do + add(:name, :string) + add(:code, :string) + add(:chapter_id, references(:chapter, on_delete: :nothing)) + add(:grade_id, references(:grade, on_delete: :nothing)) + add(:tag_id, references(:tag, on_delete: :nothing)) + + timestamps() + end + end +end diff --git a/priv/repo/migrations/20231004104222_create_concept_table.exs b/priv/repo/migrations/20231004104222_create_concept_table.exs new file mode 100644 index 00000000..0f1ab0cd --- /dev/null +++ b/priv/repo/migrations/20231004104222_create_concept_table.exs @@ -0,0 +1,13 @@ +defmodule Dbservice.Repo.Migrations.CreateConceptTable do + use Ecto.Migration + + def change do + create table(:concept) do + add(:name, :string) + add(:topic_id, references(:topic, on_delete: :nothing)) + add(:tag_id, references(:tag, on_delete: :nothing)) + + timestamps() + end + end +end diff --git a/priv/repo/migrations/20231004104241_create_learning_objective_table.exs b/priv/repo/migrations/20231004104241_create_learning_objective_table.exs new file mode 100644 index 00000000..7e08d17c --- /dev/null +++ b/priv/repo/migrations/20231004104241_create_learning_objective_table.exs @@ -0,0 +1,13 @@ +defmodule Dbservice.Repo.Migrations.CreateLearningObjectiveTable do + use Ecto.Migration + + def change do + create table(:learning_objective) do + add(:title, :string) + add(:concept_id, references(:concept, on_delete: :nothing)) + add(:tag_id, references(:tag, on_delete: :nothing)) + + timestamps() + end + end +end diff --git a/priv/repo/migrations/20231004104257_create_source_table.exs b/priv/repo/migrations/20231004104257_create_source_table.exs new file mode 100644 index 00000000..5159c4ae --- /dev/null +++ b/priv/repo/migrations/20231004104257_create_source_table.exs @@ -0,0 +1,13 @@ +defmodule Dbservice.Repo.Migrations.CreateSourceTable do + use Ecto.Migration + + def change do + create table(:source) do + add(:name, :string) + add(:link, :text) + add(:tag_id, references(:tag, on_delete: :nothing)) + + timestamps() + end + end +end diff --git a/priv/repo/migrations/20231004104301_create_purpose_table.exs b/priv/repo/migrations/20231004104301_create_purpose_table.exs new file mode 100644 index 00000000..73aa17d5 --- /dev/null +++ b/priv/repo/migrations/20231004104301_create_purpose_table.exs @@ -0,0 +1,13 @@ +defmodule Dbservice.Repo.Migrations.CreatePurposeTable do + use Ecto.Migration + + def change do + create table(:purpose) do + add(:name, :string) + add(:description, :text) + add(:tag_id, references(:tag, on_delete: :nothing)) + + timestamps() + end + end +end diff --git a/priv/repo/migrations/20231004104309_create_resource_table.exs b/priv/repo/migrations/20231004104309_create_resource_table.exs new file mode 100644 index 00000000..3728153e --- /dev/null +++ b/priv/repo/migrations/20231004104309_create_resource_table.exs @@ -0,0 +1,22 @@ +defmodule Dbservice.Repo.Migrations.CreateResourceTable do + use Ecto.Migration + + def change do + create table(:resource) do + add(:name, :string) + add(:type, :string) + add(:type_params, :map) + add(:difficulty_level, :string) + add(:curriculum_id, references(:curriculum, on_delete: :nothing)) + add(:chapter_id, references(:chapter, on_delete: :nothing)) + add(:topic_id, references(:topic, on_delete: :nothing)) + add(:source_id, references(:source, on_delete: :nothing)) + add(:purpose_id, references(:purpose, on_delete: :nothing)) + add(:concept_id, references(:concept, on_delete: :nothing)) + add(:learning_objective_id, references(:learning_objective, on_delete: :nothing)) + add(:tag_id, references(:tag, on_delete: :nothing)) + + timestamps() + end + end +end diff --git a/priv/repo/seeds.exs b/priv/repo/seeds.exs index 74cdd0ff..8edcd7e1 100644 --- a/priv/repo/seeds.exs +++ b/priv/repo/seeds.exs @@ -23,6 +23,17 @@ alias Dbservice.GroupTypes alias Dbservice.Batches alias Dbservice.BatchPrograms alias Dbservice.FormSchemas +alias Dbservice.Tags +alias Dbservice.Curriculums +alias Dbservice.Grades +alias Dbservice.Subjects +alias Dbservice.Chapters +alias Dbservice.Topics +alias Dbservice.Concepts +alias Dbservice.LearningObjectives +alias Dbservice.Sources +alias Dbservice.Purposes +alias Dbservice.Resources alias Faker.Person alias Faker.Internet @@ -205,7 +216,6 @@ defmodule Seed do stream: Enum.random(["Science", "Commerce", "Arts"]), user_id: user.id, physically_handicapped: Enum.random([true, false]), - family_income: Enum.random(["1LPA-3LPA", "3LPA-6LPA", ">6LPA"]), father_profession: Enum.random(["Self-employed", "Unemployed", "Private employee", "Government employee"]), father_education_level: Enum.random(["UG", "PG", "NA"]), @@ -218,12 +228,34 @@ defmodule Seed do "Unemployed" ]), mother_education_level: Enum.random(["UG", "PG", "NA"]), - time_of_device_availability: Faker.DateTime.forward(Enum.random(1..10)), - has_internet_access: Enum.random([true, false]), + time_of_device_availability: Enum.random(["Between 8am-10am", "Between 5pm-7pm", "Between 10pm-11pm"]), + has_internet_access: Enum.random(["yes", "no"]), primary_smartphone_owner: Enum.random(["Father", "Mother"]), primary_smartphone_owner_profession: Enum.random(["Employed", "Unemployed"]), is_dropper: Enum.random([true, false]), - contact_hours_per_week: Enum.random(20..48) + contact_hours_per_week: Enum.random(20..48), + guardian_name: Person.name(), + guardian_relation: Enum.random(["Parent", "Sibling", "Aunt", "Uncle"]), + guardian_phone: Phone.PtPt.number(), + guardian_education_level: Enum.random(["UG", "PG", "NA"]), + guardian_profession: Enum.random(["Self-employed", "Unemployed", "Private employee", "Government employee"]), + has_category_certificate: Enum.random([true, false]), + category_certificate: Seed.random_alphanumeric(), + physically_handicapped_certificate: Seed.random_alphanumeric(), + annual_family_income: Enum.random(["1LPA-3LPA", "3LPA-6LPA", ">6LPA"]), + monthly_family_income: Enum.random(["5K-20K", "20K-50K", ">50K"]), + number_of_smartphones: Enum.random(["1", "2", "3", "4", ">4"]), + family_type: Enum.random(["Joint", "Nuclear", "Other"]), + number_of_four_wheelers: Enum.random(["1", "2", "3", "4", ">4"]), + number_of_two_wheelers: Enum.random(["1", "2", "3", "4", ">4"]), + has_air_conditioner: Enum.random([true, false]), + goes_for_tuition_or_other_coaching: Enum.random(["yes", "no"]), + know_about_avanti: Enum.random(["yes", "no"]), + percentage_in_grade_10_science: Enum.random([">49%", "50-59%", "60-69%", "70-79%", "80-89%", ">90%"]), + percentage_in_grade_10_math: Enum.random([">49%", "50-59%", "60-69%", "70-79%", "80-89%", ">90%"]), + percentage_in_grade_10_english: Enum.random([">49%", "50-59%", "60-69%", "70-79%", "80-89%", ">90%"]), + grade_10_marksheet: Seed.random_alphanumeric(), + photo: Seed.random_alphanumeric() }) student @@ -379,6 +411,225 @@ defmodule Seed do group_type end + + def create_tag() do + {:ok, tag} = + Tags.create_tag(%{ + name: Person.name(), + description: Faker.Lorem.sentence() + }) + + tag + end + + def create_curriculum() do + tag = Tags.Tag |> offset(^Enum.random(1..9)) |> limit(1) |> Repo.one() + + {:ok, curriculum} = + Curriculums.create_curriculum(%{ + name: Faker.Lorem.word(), + code: Faker.Lorem.word(), + tag_id: tag.id + }) + + curriculum + end + + def create_grade() do + tag = Tags.Tag |> offset(^Enum.random(1..9)) |> limit(1) |> Repo.one() + + {:ok, grade} = + Grades.create_grade(%{ + number: Enum.random([9, 10, 11, 12]), + tag_id: tag.id + }) + + grade + end + + def create_subject() do + tag = Tags.Tag |> offset(^Enum.random(1..9)) |> limit(1) |> Repo.one() + + {:ok, subject} = + Subjects.create_subject(%{ + name: Enum.random(["Physics", "Chemistry", "Maths", "Biology"]), + code: Faker.Lorem.word(), + tag_id: tag.id + }) + + subject + end + + def create_chapter() do + grade = Grades.Grade |> offset(^Enum.random(1..9)) |> limit(1) |> Repo.one() + subject = Subjects.Subject |> offset(^Enum.random(1..9)) |> limit(1) |> Repo.one() + tag = Tags.Tag |> offset(^Enum.random(1..9)) |> limit(1) |> Repo.one() + + {:ok, chapter} = + Chapters.create_chapter(%{ + name: + Enum.random([ + "Electrostatics", + "Electronic Devices", + "Electrochemistry", + "Polymers", + "Differential Equations" + ]), + code: Faker.Lorem.word(), + grade_id: grade.id, + subject_id: subject.id, + tag_id: tag.id + }) + + chapter + end + + def create_topic() do + grade = Grades.Grade |> offset(^Enum.random(1..9)) |> limit(1) |> Repo.one() + chapter = Chapters.Chapter |> offset(^Enum.random(1..9)) |> limit(1) |> Repo.one() + tag = Tags.Tag |> offset(^Enum.random(1..9)) |> limit(1) |> Repo.one() + + {:ok, topic} = + Topics.create_topic(%{ + name: + Enum.random([ + "Coulomb's Law", + "Transistors", + "Electrolysis", + "Hydrocarbons", + "Determinant Properties" + ]), + code: Faker.Lorem.word(), + grade_id: grade.id, + chapter_id: chapter.id, + tag_id: tag.id + }) + + topic + end + + def create_concept() do + topic = Topics.Topic |> offset(^Enum.random(1..9)) |> limit(1) |> Repo.one() + tag = Tags.Tag |> offset(^Enum.random(1..9)) |> limit(1) |> Repo.one() + + {:ok, concept} = + Concepts.create_concept(%{ + name: + Enum.random([ + "Electric Field Lines", + "Transistor Amplification", + "Oxidation and Reduction", + "Polymer Structures", + "Rolle's Theorem and Mean Value Theorem" + ]), + topic_id: 1, + tag_id: 1 + }) + + concept + end + + def create_learning_objective() do + concept = Concepts.Concept |> offset(^Enum.random(1..9)) |> limit(1) |> Repo.one() + tag = Tags.Tag |> offset(^Enum.random(1..9)) |> limit(1) |> Repo.one() + + {:ok, learning_objective} = + LearningObjectives.create_learning_objective(%{ + title: + Enum.random([ + "Calculate the electrostatic force between two point charges using Coulomb's Law and understand its dependence on charge and distance", + "Explain the properties and behavior of semiconductor materials, including their role in the operation of diodes and transistors", + "Define oxidation and reduction reactions, identify redox reactions, and balance chemical equations involving electron transfer", + "Classify hydrocarbons into alkanes, alkenes, and alkynes based on their structure and understand the nomenclature of organic compounds", + "Calculate the inverse of a square matrix and understand the conditions under which a matrix is invertible" + ]), + concept_id: concept.id, + tag_id: tag.id + }) + + learning_objective + end + + def create_source() do + tag = Tags.Tag |> offset(^Enum.random(1..9)) |> limit(1) |> Repo.one() + + {:ok, source} = + Sources.create_source(%{ + name: + Enum.random([ + "youtube", + "CMS", + "plio", + "tictaclearn", + "diksha" + ]), + link: Seed.random_alphanumeric(), + tag_id: tag.id + }) + + source + end + + def create_purpose() do + tag = Tags.Tag |> offset(^Enum.random(1..9)) |> limit(1) |> Repo.one() + + {:ok, purpose} = + Purposes.create_purpose(%{ + name: + Enum.random([ + "introVideo", + "conceptVideo", + "problemSolvingVideo", + "learningModule ", + "conceptTest" + ]), + description: Faker.Lorem.sentence(), + tag_id: tag.id + }) + + purpose + end + + def create_resource() do + curriculum = Curriculums.Curriculum |> offset(^Enum.random(1..9)) |> limit(1) |> Repo.one() + chapter = Chapters.Chapter |> offset(^Enum.random(1..9)) |> limit(1) |> Repo.one() + topic = Topics.Topic |> offset(^Enum.random(1..9)) |> limit(1) |> Repo.one() + source = Sources.Source |> offset(^Enum.random(1..9)) |> limit(1) |> Repo.one() + purpose = Purposes.Purpose |> offset(^Enum.random(1..9)) |> limit(1) |> Repo.one() + concept = Concepts.Concept |> offset(^Enum.random(1..9)) |> limit(1) |> Repo.one() + + learning_objective = + LearningObjectives.LearningObjective |> offset(^Enum.random(1..9)) |> limit(1) |> Repo.one() + + tag = Tags.Tag |> offset(^Enum.random(1..9)) |> limit(1) |> Repo.one() + + {:ok, resource} = + Resources.create_resource(%{ + name: + Enum.random([ + "1. 9C01 Introduction - हमारे आस पास के पदार्थ | Matter in our Surroundings", + "2. 9C01.1 CV1 पदार्थ क्या है? | What is matter?", + "3. 9C01.1 CV2 पदार्थ की भौतिक प्रकृति | Physical Nature of Matter", + "4. 9C01.1 CV3 पदार्थ के कण | Particles of Matter ", + "5. 9C01.1 CV4 पदार्थ के कण - क्रियाकलाप | Particles of Matter - Activity" + ]), + type: Enum.random(["video", "test"]), + type_params: %{ + "duration" => Enum.random(["30 min", "45 min", "1 hr"]) + }, + difficulty_level: Enum.random(["easy", "medium", "hard"]), + curriculum_id: curriculum.id, + chapter_id: chapter.id, + topic_id: topic.id, + source_id: source.id, + purpose_id: purpose.id, + concept_id: concept.id, + learning_objective_id: learning_objective.id, + tag_id: tag.id + }) + + resource + end end Repo.delete_all(Users.Teacher) @@ -396,6 +647,17 @@ Repo.delete_all(Programs.Program) Repo.delete_all(Groups.Group) Repo.delete_all(Users.User) Repo.delete_all(Batches.Batch) +Repo.delete_all(Curriculums.Curriculum) +Repo.delete_all(Grades.Grade) +Repo.delete_all(Subjects.Subject) +Repo.delete_all(Chapters.Chapter) +Repo.delete_all(Topics.Topic) +Repo.delete_all(Concepts.Concept) +Repo.delete_all(LearningObjectives.LearningObjective) +Repo.delete_all(Sources.Source) +Repo.delete_all(Purposes.Purpose) +Repo.delete_all(Resources.Resource) +Repo.delete_all(Tags.Tag) if Mix.env() == :dev do # create some users @@ -477,4 +739,59 @@ if Mix.env() == :dev do for count <- 1..100 do Seed.create_form_schema() end + + # create some tag + for count <- 1..100 do + Seed.create_tag() + end + + # create some curriculum + for count <- 1..100 do + Seed.create_curriculum() + end + + # create some grade + for count <- 1..100 do + Seed.create_grade() + end + + # create some subject + for count <- 1..100 do + Seed.create_subject() + end + + # create some chapter + for count <- 1..100 do + Seed.create_chapter() + end + + # create some topic + for count <- 1..100 do + Seed.create_topic() + end + + # create some concept + for count <- 1..100 do + Seed.create_concept() + end + + # create some learning_objective + for count <- 1..100 do + Seed.create_learning_objective() + end + + # create some source + for count <- 1..100 do + Seed.create_source() + end + + # create some purpose + for count <- 1..100 do + Seed.create_purpose() + end + + # create some resource + for count <- 1..100 do + Seed.create_resource() + end end From d3312a8fefa706556663bbce1a3593a219beaefc Mon Sep 17 00:00:00 2001 From: Bahugunajii Date: Fri, 6 Oct 2023 18:36:54 +0530 Subject: [PATCH 2/3] fixed formatting --- .../learning_objective_controller.ex | 23 ++++++++++++++----- lib/dbservice_web/router.ex | 2 +- .../20231004104309_create_resource_table.exs | 2 +- priv/repo/seeds.exs | 15 ++++++++---- 4 files changed, 29 insertions(+), 13 deletions(-) diff --git a/lib/dbservice_web/controllers/learning_objective_controller.ex b/lib/dbservice_web/controllers/learning_objective_controller.ex index 60b9f48a..981bd0ee 100644 --- a/lib/dbservice_web/controllers/learning_objective_controller.ex +++ b/lib/dbservice_web/controllers/learning_objective_controller.ex @@ -64,10 +64,14 @@ defmodule DbserviceWeb.LearningObjectiveController do end def create(conn, params) do - with {:ok, %LearningObjective{} = learning_objective} <- LearningObjectives.create_learning_objective(params) do + with {:ok, %LearningObjective{} = learning_objective} <- + LearningObjectives.create_learning_objective(params) do conn |> put_status(:created) - |> put_resp_header("location", Routes.learning_objective_path(conn, :show, learning_objective)) + |> put_resp_header( + "location", + Routes.learning_objective_path(conn, :show, learning_objective) + ) |> render("show.json", learning_objective: learning_objective) end end @@ -76,7 +80,9 @@ defmodule DbserviceWeb.LearningObjectiveController do get("/api/learning-objective/{learningObjectiveId}") parameters do - learningObjectiveId(:path, :integer, "The id of the learning_objective record", required: true) + learningObjectiveId(:path, :integer, "The id of the learning_objective record", + required: true + ) end response(200, "OK", Schema.ref(:LearningObjective)) @@ -91,7 +97,10 @@ defmodule DbserviceWeb.LearningObjectiveController do patch("/api/learning-objective/{learningObjectiveId}") parameters do - learningObjectiveId(:path, :integer, "The id of the learning objective record", required: true) + learningObjectiveId(:path, :integer, "The id of the learning objective record", + required: true + ) + body(:body, Schema.ref(:LearningObjective), "LearningObjective to create", required: true) end @@ -101,7 +110,8 @@ defmodule DbserviceWeb.LearningObjectiveController do def update(conn, params) do learning_objective = LearningObjectives.get_learning_objective!(params["id"]) - with {:ok, %LearningObjective{} = learning_objective} <- LearningObjectives.update_learning_objective(learning_objective, params) do + with {:ok, %LearningObjective{} = learning_objective} <- + LearningObjectives.update_learning_objective(learning_objective, params) do render(conn, "show.json", learning_objective: learning_objective) end end @@ -119,7 +129,8 @@ defmodule DbserviceWeb.LearningObjectiveController do def delete(conn, %{"id" => id}) do learning_objective = LearningObjectives.get_learning_objective!(id) - with {:ok, %LearningObjective{}} <- LearningObjectives.delete_learning_objective(learning_objective) do + with {:ok, %LearningObjective{}} <- + LearningObjectives.delete_learning_objective(learning_objective) do send_resp(conn, :no_content, "") end end diff --git a/lib/dbservice_web/router.ex b/lib/dbservice_web/router.ex index 1b05833b..191353bc 100644 --- a/lib/dbservice_web/router.ex +++ b/lib/dbservice_web/router.ex @@ -42,7 +42,7 @@ defmodule DbserviceWeb.Router do resources("/subject", SubjectController, except: [:new, :edit]) resources("/chapter", ChapterController, except: [:new, :edit]) resources("/topic", TopicController, except: [:new, :edit]) - resources("/concept", ConceptController, except: [:new, :edit]) + resources("/concept", ConceptController, except: [:new, :edit]) resources("/learning-objective", LearningObjectiveController, except: [:new, :edit]) resources("/source", SourceController, except: [:new, :edit]) resources("/purpose", PurposeController, except: [:new, :edit]) diff --git a/priv/repo/migrations/20231004104309_create_resource_table.exs b/priv/repo/migrations/20231004104309_create_resource_table.exs index 3728153e..9714569f 100644 --- a/priv/repo/migrations/20231004104309_create_resource_table.exs +++ b/priv/repo/migrations/20231004104309_create_resource_table.exs @@ -8,7 +8,7 @@ defmodule Dbservice.Repo.Migrations.CreateResourceTable do add(:type_params, :map) add(:difficulty_level, :string) add(:curriculum_id, references(:curriculum, on_delete: :nothing)) - add(:chapter_id, references(:chapter, on_delete: :nothing)) + add(:chapter_id, references(:chapter, on_delete: :nothing)) add(:topic_id, references(:topic, on_delete: :nothing)) add(:source_id, references(:source, on_delete: :nothing)) add(:purpose_id, references(:purpose, on_delete: :nothing)) diff --git a/priv/repo/seeds.exs b/priv/repo/seeds.exs index 8edcd7e1..ef57e565 100644 --- a/priv/repo/seeds.exs +++ b/priv/repo/seeds.exs @@ -228,7 +228,8 @@ defmodule Seed do "Unemployed" ]), mother_education_level: Enum.random(["UG", "PG", "NA"]), - time_of_device_availability: Enum.random(["Between 8am-10am", "Between 5pm-7pm", "Between 10pm-11pm"]), + time_of_device_availability: + Enum.random(["Between 8am-10am", "Between 5pm-7pm", "Between 10pm-11pm"]), has_internet_access: Enum.random(["yes", "no"]), primary_smartphone_owner: Enum.random(["Father", "Mother"]), primary_smartphone_owner_profession: Enum.random(["Employed", "Unemployed"]), @@ -238,7 +239,8 @@ defmodule Seed do guardian_relation: Enum.random(["Parent", "Sibling", "Aunt", "Uncle"]), guardian_phone: Phone.PtPt.number(), guardian_education_level: Enum.random(["UG", "PG", "NA"]), - guardian_profession: Enum.random(["Self-employed", "Unemployed", "Private employee", "Government employee"]), + guardian_profession: + Enum.random(["Self-employed", "Unemployed", "Private employee", "Government employee"]), has_category_certificate: Enum.random([true, false]), category_certificate: Seed.random_alphanumeric(), physically_handicapped_certificate: Seed.random_alphanumeric(), @@ -251,9 +253,12 @@ defmodule Seed do has_air_conditioner: Enum.random([true, false]), goes_for_tuition_or_other_coaching: Enum.random(["yes", "no"]), know_about_avanti: Enum.random(["yes", "no"]), - percentage_in_grade_10_science: Enum.random([">49%", "50-59%", "60-69%", "70-79%", "80-89%", ">90%"]), - percentage_in_grade_10_math: Enum.random([">49%", "50-59%", "60-69%", "70-79%", "80-89%", ">90%"]), - percentage_in_grade_10_english: Enum.random([">49%", "50-59%", "60-69%", "70-79%", "80-89%", ">90%"]), + percentage_in_grade_10_science: + Enum.random([">49%", "50-59%", "60-69%", "70-79%", "80-89%", ">90%"]), + percentage_in_grade_10_math: + Enum.random([">49%", "50-59%", "60-69%", "70-79%", "80-89%", ">90%"]), + percentage_in_grade_10_english: + Enum.random([">49%", "50-59%", "60-69%", "70-79%", "80-89%", ">90%"]), grade_10_marksheet: Seed.random_alphanumeric(), photo: Seed.random_alphanumeric() }) From 4a21a0fbfbcea5ec8f6a30cef412e4259723e19a Mon Sep 17 00:00:00 2001 From: Bahugunajii Date: Fri, 15 Dec 2023 14:46:47 +0530 Subject: [PATCH 3/3] added curriculum id to chapters --- lib/dbservice/chapters/chapter.ex | 5 ++++- lib/dbservice/curriculums/curriculum.ex | 2 ++ lib/dbservice_web/swagger_schemas/chapter.ex | 4 +++- lib/dbservice_web/views/chapter_view.ex | 3 ++- .../20231213124007_add_curriculum_id_to_chapter.exs | 9 +++++++++ 5 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 priv/repo/migrations/20231213124007_add_curriculum_id_to_chapter.exs diff --git a/lib/dbservice/chapters/chapter.ex b/lib/dbservice/chapters/chapter.ex index 57206d02..c50b753b 100644 --- a/lib/dbservice/chapters/chapter.ex +++ b/lib/dbservice/chapters/chapter.ex @@ -9,6 +9,7 @@ defmodule Dbservice.Chapters.Chapter do alias Dbservice.Tags.Tag alias Dbservice.Topics.Topic alias Dbservice.Resources.Resource + alias Dbservice.Curriculums.Curriculum schema "chapter" do field(:name, :string) @@ -21,6 +22,7 @@ defmodule Dbservice.Chapters.Chapter do belongs_to(:grade, Grade) belongs_to(:subject, Subject) belongs_to(:tag, Tag) + belongs_to(:curriculum, Curriculum) end @doc false @@ -31,7 +33,8 @@ defmodule Dbservice.Chapters.Chapter do :code, :grade_id, :subject_id, - :tag_id + :tag_id, + :curriculum_id ]) end end diff --git a/lib/dbservice/curriculums/curriculum.ex b/lib/dbservice/curriculums/curriculum.ex index c77dcd9d..ee5377d3 100644 --- a/lib/dbservice/curriculums/curriculum.ex +++ b/lib/dbservice/curriculums/curriculum.ex @@ -6,6 +6,7 @@ defmodule Dbservice.Curriculums.Curriculum do alias Dbservice.Tags.Tag alias Dbservice.Resources.Resource + alias Dbservice.Chapters.Chapter schema "curriculum" do field(:name, :string) @@ -14,6 +15,7 @@ defmodule Dbservice.Curriculums.Curriculum do timestamps() has_many(:resource, Resource) + has_many(:chapter, Chapter) belongs_to(:tag, Tag) end diff --git a/lib/dbservice_web/swagger_schemas/chapter.ex b/lib/dbservice_web/swagger_schemas/chapter.ex index dcce0cca..a11e59fb 100644 --- a/lib/dbservice_web/swagger_schemas/chapter.ex +++ b/lib/dbservice_web/swagger_schemas/chapter.ex @@ -16,6 +16,7 @@ defmodule DbserviceWeb.SwaggerSchema.Chapter do grade_id(:integer, "Grade id associated with the chapter") subject_id(:integer, "Subject id associated with the chapter") tag_id(:integer, "Tag id associated with the chapter") + curriculum_id(:integer, "Curriculum id associated with the chapter") end example(%{ @@ -23,7 +24,8 @@ defmodule DbserviceWeb.SwaggerSchema.Chapter do code: "9C01", grade_id: 1, subject_id: 1, - tag_id: 4 + tag_id: 4, + curriculum_id: 1 }) end } diff --git a/lib/dbservice_web/views/chapter_view.ex b/lib/dbservice_web/views/chapter_view.ex index 6380b2c7..ba691b37 100644 --- a/lib/dbservice_web/views/chapter_view.ex +++ b/lib/dbservice_web/views/chapter_view.ex @@ -17,7 +17,8 @@ defmodule DbserviceWeb.ChapterView do code: chapter.code, grade_id: chapter.grade_id, subject_id: chapter.subject_id, - tag_id: chapter.tag_id + tag_id: chapter.tag_id, + curriculum_id: chapter.curriculum_id } end end diff --git a/priv/repo/migrations/20231213124007_add_curriculum_id_to_chapter.exs b/priv/repo/migrations/20231213124007_add_curriculum_id_to_chapter.exs new file mode 100644 index 00000000..d44cbcac --- /dev/null +++ b/priv/repo/migrations/20231213124007_add_curriculum_id_to_chapter.exs @@ -0,0 +1,9 @@ +defmodule Dbservice.Repo.Migrations.AddCurriculumIdToChapter do + use Ecto.Migration + + def change do + alter table(:chapter) do + add(:curriculum_id, references(:curriculum, on_delete: :nothing)) + end + end +end