From 21b1fb6d172a5d470f3aa4934a398a2e201d6efe Mon Sep 17 00:00:00 2001
From: Kevin Ditscheid <kevin.ditscheid@sgalinski.de>
Date: Tue, 22 Mar 2022 14:18:51 +0100
Subject: [PATCH] [TASK] Add comments to the findRelated functionality

---
 Classes/Domain/Repository/NewsRepository.php | 23 +++++++++++++-------
 1 file changed, 15 insertions(+), 8 deletions(-)

diff --git a/Classes/Domain/Repository/NewsRepository.php b/Classes/Domain/Repository/NewsRepository.php
index d12d51a..c854896 100644
--- a/Classes/Domain/Repository/NewsRepository.php
+++ b/Classes/Domain/Repository/NewsRepository.php
@@ -518,6 +518,7 @@ class NewsRepository extends AbstractRepository {
 	public function findRelated(News $news, int $limit = 0) {
 		$connection = $this->getConnection();
 		$qb = $connection->createQueryBuilder();
+		// We need to build the constraint for the tags/categories
 		$constraints = [];
 		$tags = $news->getTags();
 		if ($tags->count() > 0) {
@@ -533,6 +534,7 @@ class NewsRepository extends AbstractRepository {
 			$constraints[] = $qb->expr()->eq('pid', $news->getPid());
 		}
 
+		// here we fetch the lastUpdated of the $limit amount of news with newer lastUpdated dates
 		$result = $qb->select('lastUpdated')
 			->from('pages')
 			->where(
@@ -541,10 +543,11 @@ class NewsRepository extends AbstractRepository {
 				$qb->expr()->andX(...$constraints)
 			)
 			->setMaxResults($limit)
-			->orderBy('lastUpdated', 'asc')
+			->orderBy('lastUpdated', 'desc')
 			->execute();
-		$max = $result->fetchOne();
+		$newest = $result->fetchOne();
 
+		// Here we fetch the lastUpdated of the $limit amount of news with older lastUpdated dates
 		$result = $qb->select('lastUpdated')
 			->from('pages')
 			->where(
@@ -553,9 +556,9 @@ class NewsRepository extends AbstractRepository {
 				$qb->expr()->andX(...$constraints)
 			)
 			->setMaxResults($limit)
-			->orderBy('lastUpdated', 'desc')
+			->orderBy('lastUpdated', 'asc')
 			->execute();
-		$min = $result->fetchOne();
+		$oldest = $result->fetchOne();
 
 		$query = $this->createQuery();
 		$query->getQuerySettings()->setRespectStoragePage(FALSE);
@@ -563,13 +566,17 @@ class NewsRepository extends AbstractRepository {
 			'crdate' => QueryInterface::ORDER_DESCENDING
 		]);
 		$constraints = [];
-		if ($max) {
-			$constraints[] = $query->lessThan('lastUpdated', $max);
+		if ($newest) {
+			$constraints[] = $query->lessThanOrEqual('lastUpdated', $newest);
 		}
-		if ($min) {
-			$constraints[] = $query->greaterThan('lastUpdated', $min);
+		if ($oldest) {
+			$constraints[] = $query->greaterThanOrEqual('lastUpdated', $oldest);
 		}
 
+		// Now we fetch the $limit amount of news via extbase query and limit them to the newest and oldest date
+		// remember, we fetched the oldest and newest date from the $limit amount of news newer and older then
+		// the given news. If we limit the result of the following query to $limit, we get $limit amount of news
+		// "around" the given news, where newer news are preferred due to the ordering.
 		$tags = $news->getTags();
 		if ($tags->count() > 0) {
 			foreach ($tags as $tag) {
-- 
GitLab