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` (
`TagId` int(10) unsigned NOT NULL AUTO_INCREMENT,
`Name` varchar(255) NOT NULL,
`Type` enum('artwork', 'ebook') DEFAULT 'artwork',
PRIMARY KEY (`TagId`),
UNIQUE KEY `idxUnique` (`Name`)
KEY `index1` (`Name`),
KEY `index2` (`Type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

View file

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

View file

@ -1,5 +1,9 @@
<?
class EbookTag extends Tag{
public function __construct(){
$this->Type = 'ebook';
}
// *******
// GETTERS
// *******
@ -33,6 +37,10 @@ class EbookTag extends Tag{
$error->Add(new Exceptions\StringTooLongException('Ebook tag: '. $this->Name));
}
if($this->Type != 'ebook'){
$error->Add(new Exceptions\InvalidEbookTagTypeException($this->Type));
}
if($error->HasExceptions){
throw $error;
}
@ -45,9 +53,10 @@ class EbookTag extends Tag{
$this->Validate();
Db::Query('
INSERT into Tags (Name)
values (?)
', [$this->Name]);
INSERT into Tags (Name, Type)
values (?,
?)
', [$this->Name, $this->Type]);
$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{
$tags = Db::Query('
SELECT distinct t.*
SELECT *
from Tags t
inner join EbookTags et using (TagId)
order by t.Name
where Type = "ebook"
order by Name
', [], EbookTag::class);
return $tags;

View file

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