From 534f96041d100792830e07f2626f8ddc6e679fa5 Mon Sep 17 00:00:00 2001 From: Marcos Antonio Barreche Salguero Date: Fri, 15 Dec 2023 18:35:26 +0100 Subject: [PATCH 1/3] feat: create listCourses using CQRS --- .gitignore | 2 + .../ListCourses/ListCoursesGetController.php | 44 +++++++++++++++++++ .../Courses/Application/List/ListCourses.php | 22 ++++++++++ .../Application/List/ListCoursesQuery.php | 9 ++++ .../List/ListCoursesQueryHandler.php | 18 ++++++++ .../Application/List/ListCoursesResponse.php | 17 +++++++ src/Mooc/Courses/Domain/CourseCollection.php | 19 ++++++++ src/Mooc/Courses/Domain/CourseRepository.php | 3 ++ src/Mooc/Courses/Domain/ListCourses.php | 15 +++++++ .../Persistence/DoctrineCourseRepository.php | 7 +++ 10 files changed, 156 insertions(+) create mode 100644 apps/mooc/backend/src/Controller/ListCourses/ListCoursesGetController.php create mode 100644 src/Mooc/Courses/Application/List/ListCourses.php create mode 100644 src/Mooc/Courses/Application/List/ListCoursesQuery.php create mode 100644 src/Mooc/Courses/Application/List/ListCoursesQueryHandler.php create mode 100644 src/Mooc/Courses/Application/List/ListCoursesResponse.php create mode 100644 src/Mooc/Courses/Domain/CourseCollection.php create mode 100644 src/Mooc/Courses/Domain/ListCourses.php diff --git a/.gitignore b/.gitignore index a4b4f343..2089defd 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,5 @@ /build .php-cs-fixer.cache +.idea/workspace.xml +.idea diff --git a/apps/mooc/backend/src/Controller/ListCourses/ListCoursesGetController.php b/apps/mooc/backend/src/Controller/ListCourses/ListCoursesGetController.php new file mode 100644 index 00000000..673643c8 --- /dev/null +++ b/apps/mooc/backend/src/Controller/ListCourses/ListCoursesGetController.php @@ -0,0 +1,44 @@ +ask(new ListCoursesQuery()); + + return new JsonResponse(self::transformer($response->courses())); + } + + protected function exceptions(): array + { + return []; + } + + public static function transformer(CourseCollection $courseCollection): array + { + $res = []; + + /** @var Course $course */ + foreach ($courseCollection->courses() as $course) + { + $res[] = [ + 'id' => $course->id()->value(), + 'name' => $course->name()->value(), + 'duration' => $course->duration()->value(), + ]; + } + return $res; + } +} \ No newline at end of file diff --git a/src/Mooc/Courses/Application/List/ListCourses.php b/src/Mooc/Courses/Application/List/ListCourses.php new file mode 100644 index 00000000..6edc1f31 --- /dev/null +++ b/src/Mooc/Courses/Application/List/ListCourses.php @@ -0,0 +1,22 @@ +listCourses = new DomainListCourses($repository); + } + + public function __invoke(): CourseCollection + { + return $this->listCourses->listCourses(); + } +} \ No newline at end of file diff --git a/src/Mooc/Courses/Application/List/ListCoursesQuery.php b/src/Mooc/Courses/Application/List/ListCoursesQuery.php new file mode 100644 index 00000000..a63d7461 --- /dev/null +++ b/src/Mooc/Courses/Application/List/ListCoursesQuery.php @@ -0,0 +1,9 @@ +listCourses->__invoke(); + return new ListCoursesResponse($courses); + } +} \ No newline at end of file diff --git a/src/Mooc/Courses/Application/List/ListCoursesResponse.php b/src/Mooc/Courses/Application/List/ListCoursesResponse.php new file mode 100644 index 00000000..a9ce6bb4 --- /dev/null +++ b/src/Mooc/Courses/Application/List/ListCoursesResponse.php @@ -0,0 +1,17 @@ +courses; + } +} \ No newline at end of file diff --git a/src/Mooc/Courses/Domain/CourseCollection.php b/src/Mooc/Courses/Domain/CourseCollection.php new file mode 100644 index 00000000..83ed1a1f --- /dev/null +++ b/src/Mooc/Courses/Domain/CourseCollection.php @@ -0,0 +1,19 @@ +courses = $courses; + } + + public function courses(): array + { + return $this->courses; + } +} \ No newline at end of file diff --git a/src/Mooc/Courses/Domain/CourseRepository.php b/src/Mooc/Courses/Domain/CourseRepository.php index 338ad950..b96ea997 100644 --- a/src/Mooc/Courses/Domain/CourseRepository.php +++ b/src/Mooc/Courses/Domain/CourseRepository.php @@ -11,4 +11,7 @@ interface CourseRepository public function save(Course $course): void; public function search(CourseId $id): ?Course; + + public function list(): CourseCollection; + } diff --git a/src/Mooc/Courses/Domain/ListCourses.php b/src/Mooc/Courses/Domain/ListCourses.php new file mode 100644 index 00000000..0f6e8f86 --- /dev/null +++ b/src/Mooc/Courses/Domain/ListCourses.php @@ -0,0 +1,15 @@ +repository->list(); + } +} \ No newline at end of file diff --git a/src/Mooc/Courses/Infrastructure/Persistence/DoctrineCourseRepository.php b/src/Mooc/Courses/Infrastructure/Persistence/DoctrineCourseRepository.php index 376c4703..feba7603 100644 --- a/src/Mooc/Courses/Infrastructure/Persistence/DoctrineCourseRepository.php +++ b/src/Mooc/Courses/Infrastructure/Persistence/DoctrineCourseRepository.php @@ -5,6 +5,7 @@ namespace CodelyTv\Mooc\Courses\Infrastructure\Persistence; use CodelyTv\Mooc\Courses\Domain\Course; +use CodelyTv\Mooc\Courses\Domain\CourseCollection; use CodelyTv\Mooc\Courses\Domain\CourseRepository; use CodelyTv\Mooc\Shared\Domain\Courses\CourseId; use CodelyTv\Shared\Infrastructure\Persistence\Doctrine\DoctrineRepository; @@ -20,4 +21,10 @@ public function search(CourseId $id): ?Course { return $this->repository(Course::class)->find($id); } + + public function list(): CourseCollection + { + $courses = $this->repository(Course::class)->findAll(); + return new CourseCollection(...$courses); + } } From a3bcd695b2bc96ee75961cd911012400c5dd6827 Mon Sep 17 00:00:00 2001 From: Marcos Antonio Barreche Salguero Date: Fri, 15 Dec 2023 18:48:44 +0100 Subject: [PATCH 2/3] test: create acceptance test --- .../tests/features/courses_all/course_all.feature | 8 ++++++++ tests/Shared/Infrastructure/Behat/ApiContext.php | 12 ++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 apps/mooc/backend/tests/features/courses_all/course_all.feature diff --git a/apps/mooc/backend/tests/features/courses_all/course_all.feature b/apps/mooc/backend/tests/features/courses_all/course_all.feature new file mode 100644 index 00000000..0565be64 --- /dev/null +++ b/apps/mooc/backend/tests/features/courses_all/course_all.feature @@ -0,0 +1,8 @@ +Feature: List all courses + In order to list courses on the platform + I want to list all courses + + Scenario: List all courses + Given I send a GET request to "/courses" + Then the response status code should be 200 + And the response should not be empty diff --git a/tests/Shared/Infrastructure/Behat/ApiContext.php b/tests/Shared/Infrastructure/Behat/ApiContext.php index eeddfca3..82638ee2 100644 --- a/tests/Shared/Infrastructure/Behat/ApiContext.php +++ b/tests/Shared/Infrastructure/Behat/ApiContext.php @@ -69,6 +69,18 @@ public function theResponseShouldBeEmpty(): void } } + /** + * @Then the response should not be empty + */ + public function theResponseShouldNotBeEmpty(): void + { + $actual = trim($this->sessionHelper->getResponse()); + + if (empty($actual)) { + throw new RuntimeException(sprintf("The outputs is empty, Actual:\n%s", $actual)); + } + } + /** * @Then print last api response */ From f7f22ac54989c416827012e924f32bc63192f063 Mon Sep 17 00:00:00 2001 From: Marcos Antonio Barreche Salguero Date: Fri, 15 Dec 2023 18:51:30 +0100 Subject: [PATCH 3/3] feat: add router ListCoursesGetController --- apps/mooc/backend/config/routes/list_courses.yaml | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 apps/mooc/backend/config/routes/list_courses.yaml diff --git a/apps/mooc/backend/config/routes/list_courses.yaml b/apps/mooc/backend/config/routes/list_courses.yaml new file mode 100644 index 00000000..537a3900 --- /dev/null +++ b/apps/mooc/backend/config/routes/list_courses.yaml @@ -0,0 +1,4 @@ +courses_list: + path: /courses/ + controller: CodelyTv\Apps\Mooc\Backend\Controller\ListCourses\ListCoursesGetController + methods: [GET] \ No newline at end of file