diff --git a/apps/backoffice/backend/config/routes/courses.yaml b/apps/backoffice/backend/config/routes/courses.yaml index 06ae46ee..9d19a978 100644 --- a/apps/backoffice/backend/config/routes/courses.yaml +++ b/apps/backoffice/backend/config/routes/courses.yaml @@ -3,3 +3,9 @@ courses_get: controller: CodelyTv\Apps\Backoffice\Backend\Controller\Courses\CoursesGetController defaults: { auth: false } methods: [GET] + +course_last: + path: /last-course + controller: CodelyTv\Apps\Backoffice\Backend\Controller\Courses\LastVideoController + defaults: { auth: false } + methods: [GET] \ No newline at end of file diff --git a/apps/backoffice/backend/config/services.yaml b/apps/backoffice/backend/config/services.yaml index d0e25db6..2b6a3b7b 100644 --- a/apps/backoffice/backend/config/services.yaml +++ b/apps/backoffice/backend/config/services.yaml @@ -96,3 +96,4 @@ services: # -- IMPLEMENTATIONS SELECTOR -- CodelyTv\Shared\Domain\Bus\Event\EventBus: '@CodelyTv\Shared\Infrastructure\Bus\Event\WithMonitoring\WithPrometheusMonitoringEventBus' CodelyTv\Backoffice\Courses\Domain\BackofficeCourseRepository: '@CodelyTv\Backoffice\Courses\Infrastructure\Persistence\ElasticsearchBackofficeCourseRepository' + CodelyTv\Shared\Domain\Bus\Query\QueryHandler: '@CodelyTv\Backoffice\Courses\Application\LastVideo\SearchLastBackofficeCoursesQueryHandler' \ No newline at end of file diff --git a/apps/backoffice/backend/src/Controller/Courses/LastVideoController.php b/apps/backoffice/backend/src/Controller/Courses/LastVideoController.php new file mode 100644 index 00000000..507ca292 --- /dev/null +++ b/apps/backoffice/backend/src/Controller/Courses/LastVideoController.php @@ -0,0 +1,21 @@ +queryBus->__invoke() + ); + } +} diff --git a/src/Backoffice/Courses/Application/LastVideo/LastBackofficeCourseSearcher.php b/src/Backoffice/Courses/Application/LastVideo/LastBackofficeCourseSearcher.php new file mode 100644 index 00000000..95fc6084 --- /dev/null +++ b/src/Backoffice/Courses/Application/LastVideo/LastBackofficeCourseSearcher.php @@ -0,0 +1,28 @@ +repository->lastCourse(); + + if (!$lastCourse) { + return null; + } + + return new BackofficeCourseResponse( + $lastCourse->id(), + $lastCourse->name(), + $lastCourse->duration() + ); + } +} diff --git a/src/Backoffice/Courses/Application/LastVideo/SearchLastBackofficeCoursesQueryHandler.php b/src/Backoffice/Courses/Application/LastVideo/SearchLastBackofficeCoursesQueryHandler.php new file mode 100644 index 00000000..398ff974 --- /dev/null +++ b/src/Backoffice/Courses/Application/LastVideo/SearchLastBackofficeCoursesQueryHandler.php @@ -0,0 +1,20 @@ +searcher->__invoke(); + } +} diff --git a/src/Backoffice/Courses/Domain/BackofficeCourseRepository.php b/src/Backoffice/Courses/Domain/BackofficeCourseRepository.php index 1f8d82c4..1c4ab0cc 100644 --- a/src/Backoffice/Courses/Domain/BackofficeCourseRepository.php +++ b/src/Backoffice/Courses/Domain/BackofficeCourseRepository.php @@ -4,6 +4,7 @@ namespace CodelyTv\Backoffice\Courses\Domain; +use CodelyTv\Mooc\Courses\Domain\Course; use CodelyTv\Shared\Domain\Criteria\Criteria; interface BackofficeCourseRepository @@ -13,4 +14,6 @@ public function save(BackofficeCourse $course): void; public function searchAll(): array; public function matching(Criteria $criteria): array; + + public function lastCourse(): ?Course; } diff --git a/src/Backoffice/Courses/Infrastructure/Persistence/ElasticsearchBackofficeCourseRepository.php b/src/Backoffice/Courses/Infrastructure/Persistence/ElasticsearchBackofficeCourseRepository.php index bc06eced..082e19e9 100644 --- a/src/Backoffice/Courses/Infrastructure/Persistence/ElasticsearchBackofficeCourseRepository.php +++ b/src/Backoffice/Courses/Infrastructure/Persistence/ElasticsearchBackofficeCourseRepository.php @@ -6,6 +6,7 @@ use CodelyTv\Backoffice\Courses\Domain\BackofficeCourse; use CodelyTv\Backoffice\Courses\Domain\BackofficeCourseRepository; +use CodelyTv\Mooc\Courses\Domain\Course; use CodelyTv\Shared\Domain\Criteria\Criteria; use CodelyTv\Shared\Infrastructure\Persistence\Elasticsearch\ElasticsearchRepository; use function Lambdish\Phunctional\map; @@ -27,6 +28,12 @@ public function matching(Criteria $criteria): array return map($this->toCourse(), $this->searchByCriteria($criteria)); } + public function lastCourse(): ?Course + { + $courseInPrimitive = $this->searchlastInelastic(); + return $courseInPrimitive ? BackofficeCourse::fromPrimitives($courseInPrimitive) : null; + } + protected function aggregateName(): string { return 'courses'; diff --git a/src/Backoffice/Courses/Infrastructure/Persistence/InMemoryCacheBackofficeCourseRepository.php b/src/Backoffice/Courses/Infrastructure/Persistence/InMemoryCacheBackofficeCourseRepository.php index 70498e14..4ad27d7b 100644 --- a/src/Backoffice/Courses/Infrastructure/Persistence/InMemoryCacheBackofficeCourseRepository.php +++ b/src/Backoffice/Courses/Infrastructure/Persistence/InMemoryCacheBackofficeCourseRepository.php @@ -6,6 +6,7 @@ use CodelyTv\Backoffice\Courses\Domain\BackofficeCourse; use CodelyTv\Backoffice\Courses\Domain\BackofficeCourseRepository; +use CodelyTv\Mooc\Courses\Domain\Course; use CodelyTv\Shared\Domain\Criteria\Criteria; use function Lambdish\Phunctional\get; @@ -33,6 +34,14 @@ public function matching(Criteria $criteria): array return get($criteria->serialize(), self::$matchingCache) ?: $this->searchMatchingAndFillCache($criteria); } + public function lastCourse(): Course + { + $lastCourse = end(self::$allCoursesCache); + reset(self::$allCoursesCache); + + return $lastCourse; + } + private function searchAllAndFillCache(): array { return self::$allCoursesCache = $this->repository->searchAll(); diff --git a/src/Backoffice/Courses/Infrastructure/Persistence/MySqlBackofficeCourseRepository.php b/src/Backoffice/Courses/Infrastructure/Persistence/MySqlBackofficeCourseRepository.php index 8203a7fe..35bc9286 100644 --- a/src/Backoffice/Courses/Infrastructure/Persistence/MySqlBackofficeCourseRepository.php +++ b/src/Backoffice/Courses/Infrastructure/Persistence/MySqlBackofficeCourseRepository.php @@ -6,6 +6,7 @@ use CodelyTv\Backoffice\Courses\Domain\BackofficeCourse; use CodelyTv\Backoffice\Courses\Domain\BackofficeCourseRepository; +use CodelyTv\Mooc\Courses\Domain\Course; use CodelyTv\Shared\Domain\Criteria\Criteria; use CodelyTv\Shared\Infrastructure\Persistence\Doctrine\DoctrineCriteriaConverter; use CodelyTv\Shared\Infrastructure\Persistence\Doctrine\DoctrineRepository; @@ -28,4 +29,10 @@ public function matching(Criteria $criteria): array return $this->repository(BackofficeCourse::class)->matching($doctrineCriteria)->toArray(); } + + public function lastCourse(): ?Course + { + return $this->repository(BackofficeCourse::class) + ->findOneBy([], ['id' => 'DESC']); + } } diff --git a/src/Shared/Infrastructure/Persistence/Elasticsearch/ElasticsearchRepository.php b/src/Shared/Infrastructure/Persistence/Elasticsearch/ElasticsearchRepository.php index c6b5a5cd..2cdb7705 100644 --- a/src/Shared/Infrastructure/Persistence/Elasticsearch/ElasticsearchRepository.php +++ b/src/Shared/Infrastructure/Persistence/Elasticsearch/ElasticsearchRepository.php @@ -32,9 +32,15 @@ protected function persist(string $id, array $plainBody): void $this->client->persist($this->aggregateName(), $id, $plainBody); } - protected function searchAllInElastic(): array + protected function searchlastInelastic(): array { - return $this->searchRawElasticsearchQuery([]); + $courses = $this->searchRawElasticsearchQuery([ + 'body' => [ + 'size' => 1 + ] + ]); + + return $courses ? end($courses): []; } protected function searchRawElasticsearchQuery(array $params): array