diff --git a/config/sql/se/EbookSources.sql b/config/sql/se/EbookSources.sql index 3446a598..dba3e0f5 100644 --- a/config/sql/se/EbookSources.sql +++ b/config/sql/se/EbookSources.sql @@ -1,8 +1,7 @@ CREATE TABLE `EbookSources` ( - `EbookSourceId` int(10) unsigned NOT NULL AUTO_INCREMENT, `EbookId` int(10) unsigned NOT NULL, `Type` enum('project_gutenberg', 'project_gutenberg_australia', 'project_gutenberg_canada', 'internet_archive', 'hathi_trust', 'wikisource', 'google_books', 'faded_page', 'other') DEFAULT 'other', `Url` varchar(255) NOT NULL, - PRIMARY KEY (`EbookSourceId`), + `SortOrder` tinyint(3) unsigned NOT NULL, KEY `index1` (`EbookId`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; diff --git a/lib/Ebook.php b/lib/Ebook.php index d9c224f9..f66775e9 100644 --- a/lib/Ebook.php +++ b/lib/Ebook.php @@ -193,7 +193,7 @@ class Ebook{ SELECT * from EbookSources where EbookId = ? - order by EbookSourceId + order by SortOrder asc ', [$this->EbookId], EbookSource::class); } @@ -1790,8 +1790,9 @@ class Ebook{ * @throws Exceptions\ValidationException */ private function AddSources(): void{ - foreach($this->Sources as $source){ + foreach($this->Sources as $sortOrder => $source){ $source->EbookId = $this->EbookId; + $source->SortOrder = $sortOrder; $source->Create(); } } diff --git a/lib/EbookSource.php b/lib/EbookSource.php index fcbc098e..34b475a4 100644 --- a/lib/EbookSource.php +++ b/lib/EbookSource.php @@ -3,10 +3,10 @@ use Safe\DateTimeImmutable; class EbookSource{ - public ?int $EbookSourceId = null; public ?int $EbookId = null; public EbookSourceType $Type; public string $Url; + public ?int $SortOrder = null; public static function FromTypeAndUrl(EbookSourceType $type, string $url): EbookSource{ $instance = new EbookSource(); @@ -39,6 +39,10 @@ class EbookSource{ $error->Add(new Exceptions\EbookSourceUrlRequiredException()); } + if(!isset($this->SortOrder)){ + $error->Add(new Exceptions\EbookSourceSortOrderRequiredException()); + } + if($error->HasExceptions){ throw $error; } @@ -50,12 +54,11 @@ class EbookSource{ public function Create(): void{ $this->Validate(); Db::Query(' - INSERT into EbookSources (EbookId, Type, Url) + INSERT into EbookSources (EbookId, Type, Url, SortOrder) values (?, + ?, ?, ?) - ', [$this->EbookId, $this->Type, $this->Url]); - - $this->EbookSourceId = Db::GetLastInsertedId(); + ', [$this->EbookId, $this->Type, $this->Url, $this->SortOrder]); } } diff --git a/lib/Exceptions/EbookSourceSortOrderRequiredException.php b/lib/Exceptions/EbookSourceSortOrderRequiredException.php new file mode 100644 index 00000000..fc73385e --- /dev/null +++ b/lib/Exceptions/EbookSourceSortOrderRequiredException.php @@ -0,0 +1,7 @@ +