diff --git a/lib/Ebook.php b/lib/Ebook.php index f4b58026..bd82097d 100644 --- a/lib/Ebook.php +++ b/lib/Ebook.php @@ -607,15 +607,14 @@ class Ebook{ } public function CreateOrUpdate(): void{ - $existingEbook = Library::GetEbookByIdentifier($this->Identifier); - - if($existingEbook === null){ - $this->Create(); - return; + try{ + $existingEbook = Ebook::GetByIdentifier($this->Identifier); + $this->EbookId = $existingEbook->EbookId; + $this->Save(); + } + catch(Exceptions\EbookNotFoundException){ + $this->Create(); } - - $this->EbookId = $existingEbook->EbookId; - $this->Save(); } private function InsertTagStrings(): void{ @@ -936,6 +935,24 @@ class Ebook{ // ORM METHODS // *********** + public static function GetByIdentifier(?string $identifier): ?Ebook{ + if($identifier === null){ + throw new Exceptions\EbookNotFoundException('Invalid identifier: ' . $identifier); + } + + $result = Db::Query(' + SELECT * + from Ebooks + where Identifier = ? + ', [$identifier], 'Ebook'); + + if(sizeof($result) == 0){ + throw new Exceptions\EbookNotFoundException('Invalid identifier: ' . $identifier); + } + + return $result[0]; + } + public function Create(): void{ $this->Validate(); diff --git a/lib/Library.php b/lib/Library.php index ef5b06fb..281ffb3f 100644 --- a/lib/Library.php +++ b/lib/Library.php @@ -673,24 +673,6 @@ class Library{ } } - public static function GetEbookByIdentifier(?string $identifier): ?Ebook{ - if($identifier === null){ - return null; - } - - $result = Db::Query(' - SELECT * - from Ebooks - where Identifier = ? - ', [$identifier], 'Ebook'); - - if(sizeof($result) == 0){ - return null; - } - - return $result[0]; - } - /** * @throws Exceptions\AppException */