Query Contributors table for GetEbooksByAuthor()

This commit is contained in:
Mike Colagrosso 2024-06-30 21:50:52 -06:00 committed by Alex Cabal
parent 2d5e66f2f2
commit 44dc65005d
2 changed files with 32 additions and 5 deletions

View file

@ -119,11 +119,38 @@ class Library{
/** /**
* @return array<Ebook> * @return array<Ebook>
* @throws Exceptions\AppException
*/ */
public static function GetEbooksByAuthor(string $wwwFilesystemPath): array{ public static function GetEbooksByAuthor(string $urlPath): array{
/** @var array<Ebook> */ if(mb_strpos($urlPath, '_') === false){
return self::GetFromApcu('author-' . $wwwFilesystemPath); // Single author
return Db::Query('
SELECT e.*
from Ebooks e
inner join Contributors con using (EbookId)
where con.MarcRole = "aut"
and con.UrlName = ?
order by e.EbookCreated desc
', [$urlPath], Ebook::class);
}
else{
// Multiple authors, e.g., karl-marx_friedrich-engels
$authors = explode('_', $urlPath);
$queryPlaceholder = '(' . implode(', ', array_fill(0, count($authors), '?')) . ')'; // For example, (?, ?) for two authors
$params = $authors;
$params[] = sizeof($authors); // The number of authors in the URL must match the number of Contributor records.
return Db::Query('
SELECT e.*
from Ebooks e
inner join Contributors con using (EbookId)
where con.MarcRole = "aut"
and con.UrlName in ' . $queryPlaceholder . '
group by e.EbookId
having count(distinct con.UrlName) = ?
order by e.EbookCreated desc
', $params, Ebook::class);
}
} }
/** /**

View file

@ -12,7 +12,7 @@ try{
throw new Exceptions\AuthorNotFoundException(); throw new Exceptions\AuthorNotFoundException();
} }
$ebooks = Library::GetEbooksByAuthor($wwwFilesystemPath); $ebooks = Library::GetEbooksByAuthor($urlPath);
if(sizeof($ebooks) == 0){ if(sizeof($ebooks) == 0){
throw new Exceptions\AuthorNotFoundException(); throw new Exceptions\AuthorNotFoundException();