From 8e08803a3c83ff98f075955eb4cce6c5ecb54d80 Mon Sep 17 00:00:00 2001 From: Didweb Date: Sat, 22 May 2021 18:47:18 +0200 Subject: [PATCH 1/2] Repositories and handler --- .../Application/Find/FindLastVideoQuery.php | 11 ++++++++++ .../Find/FindLastVideoQueryHandler.php | 21 +++++++++++++++++++ src/Mooc/Videos/Domain/VideoRepository.php | 2 ++ .../Persistence/VideoRepositoryMySql.php | 6 ++++++ 4 files changed, 40 insertions(+) create mode 100644 src/Mooc/Videos/Application/Find/FindLastVideoQuery.php create mode 100644 src/Mooc/Videos/Application/Find/FindLastVideoQueryHandler.php diff --git a/src/Mooc/Videos/Application/Find/FindLastVideoQuery.php b/src/Mooc/Videos/Application/Find/FindLastVideoQuery.php new file mode 100644 index 000000000..407ecb5f2 --- /dev/null +++ b/src/Mooc/Videos/Application/Find/FindLastVideoQuery.php @@ -0,0 +1,11 @@ +videoRepository->findLastVideo(); + } +} \ No newline at end of file diff --git a/src/Mooc/Videos/Domain/VideoRepository.php b/src/Mooc/Videos/Domain/VideoRepository.php index 7e68907a8..fa64fcede 100644 --- a/src/Mooc/Videos/Domain/VideoRepository.php +++ b/src/Mooc/Videos/Domain/VideoRepository.php @@ -12,5 +12,7 @@ public function save(Video $video): void; public function search(VideoId $id): ?Video; + public function findLastVideo(): ?Video; + public function searchByCriteria(Criteria $criteria): Videos; } diff --git a/src/Mooc/Videos/Infrastructure/Persistence/VideoRepositoryMySql.php b/src/Mooc/Videos/Infrastructure/Persistence/VideoRepositoryMySql.php index 933277438..5b58f7a0a 100644 --- a/src/Mooc/Videos/Infrastructure/Persistence/VideoRepositoryMySql.php +++ b/src/Mooc/Videos/Infrastructure/Persistence/VideoRepositoryMySql.php @@ -32,6 +32,12 @@ public function search(VideoId $id): ?Video return $this->repository(Video::class)->find($id); } + public function findLastVideo(): ?Video + { + return $this->repository(Video::class)->findOneBy([], ['id' => 'DESC']); + } + + public function searchByCriteria(Criteria $criteria): Videos { $doctrineCriteria = DoctrineCriteriaConverter::convert($criteria, self::$criteriaToDoctrineFields); From a16d1593f401b2176a364a21f4a88fa2b77b7efb Mon Sep 17 00:00:00 2001 From: Didweb Date: Sat, 22 May 2021 20:36:51 +0200 Subject: [PATCH 2/2] test --- .../Application/Find/FindLastVideoQuery.php | 8 ++- .../VideoRepositoryInMemory.php | 45 ++++++++++++++ .../FindLastVideoQueryHandlerTest.php | 60 +++++++++++++++++++ 3 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 src/Mooc/Videos/Infrastructure/VideoRepositoryInMemory.php create mode 100644 tests/Mooc/Videos/Application/FindLastVideoQueryHandlerTest.php diff --git a/src/Mooc/Videos/Application/Find/FindLastVideoQuery.php b/src/Mooc/Videos/Application/Find/FindLastVideoQuery.php index 407ecb5f2..301bfa1e2 100644 --- a/src/Mooc/Videos/Application/Find/FindLastVideoQuery.php +++ b/src/Mooc/Videos/Application/Find/FindLastVideoQuery.php @@ -5,7 +5,13 @@ namespace CodelyTv\Mooc\Videos\Application\Find; -final class FindLastVideoQuery +use CodelyTv\Shared\Domain\Bus\Query\Query; + +final class FindLastVideoQuery implements Query { + public function __construct() + { + } + } \ No newline at end of file diff --git a/src/Mooc/Videos/Infrastructure/VideoRepositoryInMemory.php b/src/Mooc/Videos/Infrastructure/VideoRepositoryInMemory.php new file mode 100644 index 000000000..57b2c65b3 --- /dev/null +++ b/src/Mooc/Videos/Infrastructure/VideoRepositoryInMemory.php @@ -0,0 +1,45 @@ +videos[] = $video; + } + + public function search(VideoId $id): ?Video + { + foreach ($this->videos as $video) { + if ($video->id == $id) { + return $video; + } + } + return null; + } + + public function findLastVideo(): ?Video + { + $lastVideo = max($this->videos); + + return ($lastVideo) ? $lastVideo : null; + } + + public function searchByCriteria(Criteria $criteria): Videos + { + // TODO Criteria + return new Videos($this->videos); + } +} \ No newline at end of file diff --git a/tests/Mooc/Videos/Application/FindLastVideoQueryHandlerTest.php b/tests/Mooc/Videos/Application/FindLastVideoQueryHandlerTest.php new file mode 100644 index 000000000..823e8c1f1 --- /dev/null +++ b/tests/Mooc/Videos/Application/FindLastVideoQueryHandlerTest.php @@ -0,0 +1,60 @@ +createVideos($videoRepositoryInMemory); + + $query = new FindLastVideoQuery(); + $handler = new FindLastVideoQueryHandler($videoRepositoryInMemory); + + $videoReturned = $handler->__invoke($query); + + $this->assertEquals($videos[1]->id(), $videoReturned->id()); + } + + public function createVideos(VideoRepositoryInMemory $videoRepositoryInMemory): array + { + $videos[0] = Video::create( + new VideoId(RamseyUuid::uuid4()->toString()), + new VideoType(VideoType::SCREENCAST), + new VideoTitle('Video 1'), + new VideoUrl('http://www.videos.com/video-1'), + new CourseId(RamseyUuid::uuid4()->toString()) + ); + $videoRepositoryInMemory->save($videos[0]); + + $videos[1] = Video::create( + new VideoId(RamseyUuid::uuid4()->toString()), + new VideoType(VideoType::SCREENCAST), + new VideoTitle('Video 2'), + new VideoUrl('http://www.videos.com/video-2'), + new CourseId(RamseyUuid::uuid4()->toString()) + ); + $videoRepositoryInMemory->save($videos[1]); + + return $videos; + } +} \ No newline at end of file