From 41794465405fef1d85c053b0518ebcd8776b8983 Mon Sep 17 00:00:00 2001 From: Richard Dominick <34370238+RichDom2185@users.noreply.github.com> Date: Sat, 3 Feb 2024 19:09:25 +0700 Subject: [PATCH] Set whether stories is enabled in course config (#1053) * Create migration file * Update API * Update some tests * Fix missed tests --- lib/cadet/courses/course.ex | 4 +++- .../admin_controllers/admin_courses_controller.ex | 1 + lib/cadet_web/controllers/courses_controller.ex | 3 +++ lib/cadet_web/controllers/user_controller.ex | 2 ++ lib/cadet_web/views/courses_view.ex | 1 + lib/cadet_web/views/user_view.ex | 1 + ...114642_add_stories_toggle_to_course_config.exs | 15 +++++++++++++++ test/cadet/courses/course_test.exs | 13 +++++++++++++ test/cadet/courses/courses_test.exs | 7 +++++++ .../admin_courses_controller_test.exs | 3 +++ .../controllers/courses_controller_test.exs | 6 ++++++ .../controllers/user_controller_test.exs | 2 ++ test/factories/courses/course_factory.ex | 1 + 13 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 priv/repo/migrations/20240116114642_add_stories_toggle_to_course_config.exs diff --git a/lib/cadet/courses/course.ex b/lib/cadet/courses/course.ex index 521ded027..c74d23bd7 100644 --- a/lib/cadet/courses/course.ex +++ b/lib/cadet/courses/course.ex @@ -13,6 +13,7 @@ defmodule Cadet.Courses.Course do enable_game: boolean(), enable_achievements: boolean(), enable_sourcecast: boolean(), + enable_stories: boolean(), source_chapter: integer(), source_variant: String.t(), module_help_text: String.t(), @@ -26,6 +27,7 @@ defmodule Cadet.Courses.Course do field(:enable_game, :boolean, default: true) field(:enable_achievements, :boolean, default: true) field(:enable_sourcecast, :boolean, default: true) + field(:enable_stories, :boolean, default: false) field(:source_chapter, :integer) field(:source_variant, :string) field(:module_help_text, :string) @@ -39,7 +41,7 @@ defmodule Cadet.Courses.Course do end @required_fields ~w(course_name viewable enable_game - enable_achievements enable_sourcecast source_chapter source_variant)a + enable_achievements enable_sourcecast enable_stories source_chapter source_variant)a @optional_fields ~w(course_short_name module_help_text)a def changeset(course, params) do diff --git a/lib/cadet_web/admin_controllers/admin_courses_controller.ex b/lib/cadet_web/admin_controllers/admin_courses_controller.ex index 61b97e38d..7220a4d80 100644 --- a/lib/cadet_web/admin_controllers/admin_courses_controller.ex +++ b/lib/cadet_web/admin_controllers/admin_courses_controller.ex @@ -107,6 +107,7 @@ defmodule CadetWeb.AdminCoursesController do enable_game(:body, :boolean, "Enable game") enable_achievements(:body, :boolean, "Enable achievements") enable_sourcecast(:body, :boolean, "Enable sourcecast") + enable_stories(:body, :boolean, "Enable stories") sublanguage(:body, Schema.ref(:AdminSublanguage), "sublanguage object") module_help_text(:body, :string, "Module help text") end diff --git a/lib/cadet_web/controllers/courses_controller.ex b/lib/cadet_web/controllers/courses_controller.ex index 8f2cf3e4f..e6555bd7a 100644 --- a/lib/cadet_web/controllers/courses_controller.ex +++ b/lib/cadet_web/controllers/courses_controller.ex @@ -55,6 +55,7 @@ defmodule CadetWeb.CoursesController do enable_game(:body, :boolean, "Enable game", required: true) enable_achievements(:body, :boolean, "Enable achievements", required: true) enable_sourcecast(:body, :boolean, "Enable sourcecast", required: true) + enable_stories(:body, :boolean, "Enable stories", required: true) source_chapter(:body, :number, "Default source chapter", required: true) source_variant(:body, Schema.ref(:SourceVariant), "Default source variant name", @@ -95,6 +96,7 @@ defmodule CadetWeb.CoursesController do enable_game(:boolean, "Enable game", required: true) enable_achievements(:boolean, "Enable achievements", required: true) enable_sourcecast(:boolean, "Enable sourcecast", required: true) + enable_stories(:boolean, "Enable stories", required: true) source_chapter(:integer, "Source Chapter number from 1 to 4", required: true) source_variant(Schema.ref(:SourceVariant), "Source Variant name", required: true) module_help_text(:string, "Module help text", required: true) @@ -108,6 +110,7 @@ defmodule CadetWeb.CoursesController do enable_game: true, enable_achievements: true, enable_sourcecast: true, + enable_stories: false, source_chapter: 1, source_variant: "default", module_help_text: "Help text", diff --git a/lib/cadet_web/controllers/user_controller.ex b/lib/cadet_web/controllers/user_controller.ex index 6a457136d..cfc162652 100644 --- a/lib/cadet_web/controllers/user_controller.ex +++ b/lib/cadet_web/controllers/user_controller.ex @@ -316,6 +316,7 @@ defmodule CadetWeb.UserController do enable_game(:boolean, "Enable game", required: true) enable_achievements(:boolean, "Enable achievements", required: true) enable_sourcecast(:boolean, "Enable sourcecast", required: true) + enable_stories(:boolean, "Enable stories", required: true) source_chapter(:integer, "Source Chapter number from 1 to 4", required: true) source_variant(Schema.ref(:SourceVariant), "Source Variant name", required: true) module_help_text(:string, "Module help text", required: true) @@ -330,6 +331,7 @@ defmodule CadetWeb.UserController do enable_game: true, enable_achievements: true, enable_sourcecast: true, + enable_stories: false, source_chapter: 1, source_variant: "default", module_help_text: "Help text", diff --git a/lib/cadet_web/views/courses_view.ex b/lib/cadet_web/views/courses_view.ex index 6ab298c73..a6ae9c4fa 100644 --- a/lib/cadet_web/views/courses_view.ex +++ b/lib/cadet_web/views/courses_view.ex @@ -11,6 +11,7 @@ defmodule CadetWeb.CoursesView do enableGame: :enable_game, enableAchievements: :enable_achievements, enableSourcecast: :enable_sourcecast, + enableStories: :enable_stories, sourceChapter: :source_chapter, sourceVariant: :source_variant, moduleHelpText: :module_help_text, diff --git a/lib/cadet_web/views/user_view.ex b/lib/cadet_web/views/user_view.ex index f28599024..458a15980 100644 --- a/lib/cadet_web/views/user_view.ex +++ b/lib/cadet_web/views/user_view.ex @@ -104,6 +104,7 @@ defmodule CadetWeb.UserView do enableGame: :enable_game, enableAchievements: :enable_achievements, enableSourcecast: :enable_sourcecast, + enableStories: :enable_stories, sourceChapter: :source_chapter, sourceVariant: :source_variant, moduleHelpText: :module_help_text, diff --git a/priv/repo/migrations/20240116114642_add_stories_toggle_to_course_config.exs b/priv/repo/migrations/20240116114642_add_stories_toggle_to_course_config.exs new file mode 100644 index 000000000..dd7423f1a --- /dev/null +++ b/priv/repo/migrations/20240116114642_add_stories_toggle_to_course_config.exs @@ -0,0 +1,15 @@ +defmodule Cadet.Repo.Migrations.AddStoriesToggleToCourseConfig do + use Ecto.Migration + + def up do + alter table(:courses) do + add(:enable_stories, :boolean, null: false, default: false) + end + end + + def down do + alter table(:courses) do + remove(:enable_stories) + end + end +end diff --git a/test/cadet/courses/course_test.exs b/test/cadet/courses/course_test.exs index fc3b5dfa3..4e852596e 100644 --- a/test/cadet/courses/course_test.exs +++ b/test/cadet/courses/course_test.exs @@ -96,6 +96,19 @@ defmodule Cadet.Courses.CourseTest do :valid ) + assert_changeset( + %{ + enable_game: true, + enable_achievements: true, + enable_sourcecast: true, + enable_stories: false, + course_name: "Data Structures and Algorithms", + source_chapter: 1, + source_variant: "default" + }, + :valid + ) + assert_changeset( %{ source_chapter: 1, diff --git a/test/cadet/courses/courses_test.exs b/test/cadet/courses/courses_test.exs index 7d955fe0b..fc8880f79 100644 --- a/test/cadet/courses/courses_test.exs +++ b/test/cadet/courses/courses_test.exs @@ -20,6 +20,7 @@ defmodule Cadet.CoursesTest do enable_game: true, enable_achievements: true, enable_sourcecast: true, + enable_stories: false, source_chapter: 1, source_variant: "default", module_help_text: "Help Text" @@ -55,6 +56,7 @@ defmodule Cadet.CoursesTest do assert course.enable_game == true assert course.enable_achievements == true assert course.enable_sourcecast == true + assert course.enable_stories == false assert course.source_chapter == 1 assert course.source_variant == "default" assert course.module_help_text == "Help Text" @@ -81,6 +83,7 @@ defmodule Cadet.CoursesTest do enable_game: false, enable_achievements: false, enable_sourcecast: false, + enable_stories: true, module_help_text: "" }) @@ -90,6 +93,7 @@ defmodule Cadet.CoursesTest do assert updated_course.enable_game == false assert updated_course.enable_achievements == false assert updated_course.enable_sourcecast == false + assert updated_course.enable_stories == true assert updated_course.source_chapter == 1 assert updated_course.source_variant == "default" assert updated_course.module_help_text == nil @@ -107,6 +111,7 @@ defmodule Cadet.CoursesTest do enable_game: false, enable_achievements: false, enable_sourcecast: false, + enable_stories: true, source_chapter: new_chapter, source_variant: "default", module_help_text: "help" @@ -118,6 +123,7 @@ defmodule Cadet.CoursesTest do assert updated_course.enable_game == false assert updated_course.enable_achievements == false assert updated_course.enable_sourcecast == false + assert updated_course.enable_stories == true assert updated_course.source_chapter == new_chapter assert updated_course.source_variant == "default" assert updated_course.module_help_text == "help" @@ -135,6 +141,7 @@ defmodule Cadet.CoursesTest do enable_game: false, enable_achievements: false, enable_sourcecast: false, + enable_stories: false, module_help_text: "help" }) diff --git a/test/cadet_web/admin_controllers/admin_courses_controller_test.exs b/test/cadet_web/admin_controllers/admin_courses_controller_test.exs index b7099c713..c5f630eaf 100644 --- a/test/cadet_web/admin_controllers/admin_courses_controller_test.exs +++ b/test/cadet_web/admin_controllers/admin_courses_controller_test.exs @@ -43,6 +43,7 @@ defmodule CadetWeb.AdminCoursesControllerTest do "enableGame" => false, "enableAchievements" => false, "enableSourcecast" => true, + "enableStories" => false, "sourceChapter" => 1, "sourceVariant" => "default", "moduleHelpText" => "help" @@ -65,6 +66,7 @@ defmodule CadetWeb.AdminCoursesControllerTest do "courseName" => "Data Structures and Algorithms", "courseShortName" => "CS2040S", "enableGame" => false, + "enableStories" => false, "enableAchievements" => false, "enableSourcecast" => true, "moduleHelpText" => "help" @@ -132,6 +134,7 @@ defmodule CadetWeb.AdminCoursesControllerTest do "enableGame" => false, "enableAchievements" => false, "enableSourcecast" => true, + "enableStories" => false, "moduleHelpText" => "help", "sourceVariant" => "default" }) diff --git a/test/cadet_web/controllers/courses_controller_test.exs b/test/cadet_web/controllers/courses_controller_test.exs index 1055045dd..1a24caebf 100644 --- a/test/cadet_web/controllers/courses_controller_test.exs +++ b/test/cadet_web/controllers/courses_controller_test.exs @@ -27,6 +27,7 @@ defmodule CadetWeb.CoursesControllerTest do "enable_game" => "true", "enable_achievements" => "true", "enable_sourcecast" => "true", + "enable_stories" => "true", "source_chapter" => "1", "source_variant" => "default", "module_help_text" => "Help Text" @@ -49,6 +50,7 @@ defmodule CadetWeb.CoursesControllerTest do "viewable" => "true", "enable_achievements" => "true", "enable_sourcecast" => "true", + "enable_stories" => "true", "source_variant" => "default", "module_help_text" => "Help Text" } @@ -70,6 +72,7 @@ defmodule CadetWeb.CoursesControllerTest do "enable_game" => "true", "enable_achievements" => "true", "enable_sourcecast" => "true", + "enable_stories" => "true", "source_chapter" => "1", "source_variant" => "default", "module_help_text" => "Help Text" @@ -92,6 +95,7 @@ defmodule CadetWeb.CoursesControllerTest do "enable_game" => "true", "enable_achievements" => "true", "enable_sourcecast" => "true", + "enable_stories" => "true", "source_chapter" => "1", "source_variant" => "default", "module_help_text" => "Help Text" @@ -115,6 +119,7 @@ defmodule CadetWeb.CoursesControllerTest do "enable_game" => "true", "enable_achievements" => "true", "enable_sourcecast" => "true", + "enable_stories" => "true", "source_chapter" => "1", "source_variant" => "default", "module_help_text" => "Help Text" @@ -155,6 +160,7 @@ defmodule CadetWeb.CoursesControllerTest do "enableGame" => true, "enableAchievements" => true, "enableSourcecast" => true, + "enableStories" => false, "sourceChapter" => 1, "sourceVariant" => "default", "moduleHelpText" => "Help Text", diff --git a/test/cadet_web/controllers/user_controller_test.exs b/test/cadet_web/controllers/user_controller_test.exs index a57294fc9..0020c05c2 100644 --- a/test/cadet_web/controllers/user_controller_test.exs +++ b/test/cadet_web/controllers/user_controller_test.exs @@ -106,6 +106,7 @@ defmodule CadetWeb.UserControllerTest do "enableAchievements" => true, "enableGame" => true, "enableSourcecast" => true, + "enableStories" => false, "courseShortName" => "CS1101S", "moduleHelpText" => "Help Text", "courseName" => "Programming Methodology", @@ -306,6 +307,7 @@ defmodule CadetWeb.UserControllerTest do "enableGame" => true, "enableSourcecast" => true, "courseShortName" => "CS1101S", + "enableStories" => false, "moduleHelpText" => "Help Text", "courseName" => "Programming Methodology", "sourceChapter" => 1, diff --git a/test/factories/courses/course_factory.ex b/test/factories/courses/course_factory.ex index db9ccb0b8..0ca2c3ec6 100644 --- a/test/factories/courses/course_factory.ex +++ b/test/factories/courses/course_factory.ex @@ -15,6 +15,7 @@ defmodule Cadet.Courses.CourseFactory do enable_game: true, enable_achievements: true, enable_sourcecast: true, + enable_stories: false, source_chapter: 1, source_variant: "default", module_help_text: "Help Text"