Add a Type enum to Tags for artwork/ebook

This commit is contained in:
Mike Colagrosso 2024-06-25 18:25:31 -06:00 committed by Alex Cabal
parent ee29c526f8
commit 402dae95ff
7 changed files with 62 additions and 10 deletions

View file

@ -1,6 +1,8 @@
CREATE TABLE IF NOT EXISTS `Tags` ( CREATE TABLE IF NOT EXISTS `Tags` (
`TagId` int(10) unsigned NOT NULL AUTO_INCREMENT, `TagId` int(10) unsigned NOT NULL AUTO_INCREMENT,
`Name` varchar(255) NOT NULL, `Name` varchar(255) NOT NULL,
`Type` enum('artwork', 'ebook') DEFAULT 'artwork',
PRIMARY KEY (`TagId`), PRIMARY KEY (`TagId`),
UNIQUE KEY `idxUnique` (`Name`) KEY `index1` (`Name`),
KEY `index2` (`Type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

View file

@ -3,6 +3,10 @@ use function Safe\preg_match;
use function Safe\preg_replace; use function Safe\preg_replace;
class ArtworkTag extends Tag{ class ArtworkTag extends Tag{
public function __construct(){
$this->Type = 'artwork';
}
// ******* // *******
// GETTERS // GETTERS
// ******* // *******
@ -41,6 +45,10 @@ class ArtworkTag extends Tag{
$error->Add(new Exceptions\InvalidArtworkTagNameException()); $error->Add(new Exceptions\InvalidArtworkTagNameException());
} }
if($this->Type != 'artwork'){
$error->Add(new Exceptions\InvalidArtworkTagTypeException($this->Type));
}
if($error->HasExceptions){ if($error->HasExceptions){
throw $error; throw $error;
} }
@ -53,9 +61,10 @@ class ArtworkTag extends Tag{
$this->Validate(); $this->Validate();
Db::Query(' Db::Query('
INSERT into Tags (Name) INSERT into Tags (Name, Type)
values (?) values (?,
', [$this->Name]); ?)
', [$this->Name, $this->Type]);
$this->TagId = Db::GetLastInsertedId(); $this->TagId = Db::GetLastInsertedId();
} }
@ -67,6 +76,7 @@ class ArtworkTag extends Tag{
SELECT * SELECT *
from Tags from Tags
where Name = ? where Name = ?
and Type = "artwork"
', [$artworkTag->Name], ArtworkTag::class); ', [$artworkTag->Name], ArtworkTag::class);
if(isset($result[0])){ if(isset($result[0])){

View file

@ -1,5 +1,9 @@
<? <?
class EbookTag extends Tag{ class EbookTag extends Tag{
public function __construct(){
$this->Type = 'ebook';
}
// ******* // *******
// GETTERS // GETTERS
// ******* // *******
@ -33,6 +37,10 @@ class EbookTag extends Tag{
$error->Add(new Exceptions\StringTooLongException('Ebook tag: '. $this->Name)); $error->Add(new Exceptions\StringTooLongException('Ebook tag: '. $this->Name));
} }
if($this->Type != 'ebook'){
$error->Add(new Exceptions\InvalidEbookTagTypeException($this->Type));
}
if($error->HasExceptions){ if($error->HasExceptions){
throw $error; throw $error;
} }
@ -45,9 +53,10 @@ class EbookTag extends Tag{
$this->Validate(); $this->Validate();
Db::Query(' Db::Query('
INSERT into Tags (Name) INSERT into Tags (Name, Type)
values (?) values (?,
', [$this->Name]); ?)
', [$this->Name, $this->Type]);
$this->TagId = Db::GetLastInsertedId(); $this->TagId = Db::GetLastInsertedId();
} }

View file

@ -0,0 +1,15 @@
<?
namespace Exceptions;
class InvalidArtworkTagTypeException extends AppException{
/** @var string $message */
protected $message = 'Type should be `artwork`.';
public function __construct(?string $tagType){
if($tagType !== null && trim($tagType) != ''){
$this->message .= ' Type provided: ' . $tagType;
}
parent::__construct($this->message);
}
}

View file

@ -0,0 +1,15 @@
<?
namespace Exceptions;
class InvalidEbookTagTypeException extends AppException{
/** @var string $message */
protected $message = 'Type should be `ebook`.';
public function __construct(?string $tagType){
if($tagType !== null && trim($tagType) != ''){
$this->message .= ' Type provided: ' . $tagType;
}
parent::__construct($this->message);
}
}

View file

@ -178,10 +178,10 @@ class Library{
*/ */
public static function GetTags(): array{ public static function GetTags(): array{
$tags = Db::Query(' $tags = Db::Query('
SELECT distinct t.* SELECT *
from Tags t from Tags t
inner join EbookTags et using (TagId) where Type = "ebook"
order by t.Name order by Name
', [], EbookTag::class); ', [], EbookTag::class);
return $tags; return $tags;

View file

@ -8,6 +8,7 @@ class Tag{
public int $TagId; public int $TagId;
public string $Name; public string $Name;
public string $Type;
protected ?string $_UrlName = null; protected ?string $_UrlName = null;
protected ?string $_Url = null; protected ?string $_Url = null;
} }