mirror of
https://github.com/standardebooks/web.git
synced 2025-07-06 14:50:39 -04:00
Remove primary key EbookSourceId
There is a one to many relationship between `Ebook` and `EbookSource`, and we don't query by `EbookSourceId`. We add a `SortOrder` column to keep the sources in the same order as specified in `content.opf`.
This commit is contained in:
parent
4e8ba5ddaa
commit
1076c9a77d
4 changed files with 19 additions and 9 deletions
|
@ -1,8 +1,7 @@
|
||||||
CREATE TABLE `EbookSources` (
|
CREATE TABLE `EbookSources` (
|
||||||
`EbookSourceId` int(10) unsigned NOT NULL AUTO_INCREMENT,
|
|
||||||
`EbookId` int(10) unsigned NOT NULL,
|
`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',
|
`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,
|
`Url` varchar(255) NOT NULL,
|
||||||
PRIMARY KEY (`EbookSourceId`),
|
`SortOrder` tinyint(3) unsigned NOT NULL,
|
||||||
KEY `index1` (`EbookId`)
|
KEY `index1` (`EbookId`)
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||||
|
|
|
@ -193,7 +193,7 @@ class Ebook{
|
||||||
SELECT *
|
SELECT *
|
||||||
from EbookSources
|
from EbookSources
|
||||||
where EbookId = ?
|
where EbookId = ?
|
||||||
order by EbookSourceId
|
order by SortOrder asc
|
||||||
', [$this->EbookId], EbookSource::class);
|
', [$this->EbookId], EbookSource::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1790,8 +1790,9 @@ class Ebook{
|
||||||
* @throws Exceptions\ValidationException
|
* @throws Exceptions\ValidationException
|
||||||
*/
|
*/
|
||||||
private function AddSources(): void{
|
private function AddSources(): void{
|
||||||
foreach($this->Sources as $source){
|
foreach($this->Sources as $sortOrder => $source){
|
||||||
$source->EbookId = $this->EbookId;
|
$source->EbookId = $this->EbookId;
|
||||||
|
$source->SortOrder = $sortOrder;
|
||||||
$source->Create();
|
$source->Create();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,10 +3,10 @@
|
||||||
use Safe\DateTimeImmutable;
|
use Safe\DateTimeImmutable;
|
||||||
|
|
||||||
class EbookSource{
|
class EbookSource{
|
||||||
public ?int $EbookSourceId = null;
|
|
||||||
public ?int $EbookId = null;
|
public ?int $EbookId = null;
|
||||||
public EbookSourceType $Type;
|
public EbookSourceType $Type;
|
||||||
public string $Url;
|
public string $Url;
|
||||||
|
public ?int $SortOrder = null;
|
||||||
|
|
||||||
public static function FromTypeAndUrl(EbookSourceType $type, string $url): EbookSource{
|
public static function FromTypeAndUrl(EbookSourceType $type, string $url): EbookSource{
|
||||||
$instance = new EbookSource();
|
$instance = new EbookSource();
|
||||||
|
@ -39,6 +39,10 @@ class EbookSource{
|
||||||
$error->Add(new Exceptions\EbookSourceUrlRequiredException());
|
$error->Add(new Exceptions\EbookSourceUrlRequiredException());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(!isset($this->SortOrder)){
|
||||||
|
$error->Add(new Exceptions\EbookSourceSortOrderRequiredException());
|
||||||
|
}
|
||||||
|
|
||||||
if($error->HasExceptions){
|
if($error->HasExceptions){
|
||||||
throw $error;
|
throw $error;
|
||||||
}
|
}
|
||||||
|
@ -50,12 +54,11 @@ class EbookSource{
|
||||||
public function Create(): void{
|
public function Create(): void{
|
||||||
$this->Validate();
|
$this->Validate();
|
||||||
Db::Query('
|
Db::Query('
|
||||||
INSERT into EbookSources (EbookId, Type, Url)
|
INSERT into EbookSources (EbookId, Type, Url, SortOrder)
|
||||||
values (?,
|
values (?,
|
||||||
|
?,
|
||||||
?,
|
?,
|
||||||
?)
|
?)
|
||||||
', [$this->EbookId, $this->Type, $this->Url]);
|
', [$this->EbookId, $this->Type, $this->Url, $this->SortOrder]);
|
||||||
|
|
||||||
$this->EbookSourceId = Db::GetLastInsertedId();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
7
lib/Exceptions/EbookSourceSortOrderRequiredException.php
Normal file
7
lib/Exceptions/EbookSourceSortOrderRequiredException.php
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
<?
|
||||||
|
namespace Exceptions;
|
||||||
|
|
||||||
|
class EbookSourceSortOrderRequiredException extends AppException{
|
||||||
|
/** @var string $message */
|
||||||
|
protected $message = 'EbookSource SortOrder required.';
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue