Use static class names instead of strings when getting objects from the DB

This commit is contained in:
Alex Cabal 2024-05-13 13:44:04 -05:00
parent a442f92e28
commit acb6b2949f
16 changed files with 36 additions and 34 deletions

View file

@ -130,7 +130,7 @@ class Artist{
SELECT *
from Artists
where ArtistId = ?
', [$artistId], 'Artist');
', [$artistId], Artist::class);
return $result[0] ?? throw new Exceptions\ArtistNotFoundException();;
}
@ -161,7 +161,7 @@ class Artist{
where a.UrlName = ?
or aan.UrlName = ?
limit 1
', [$artist->UrlName, $artist->UrlName], 'Artist');
', [$artist->UrlName, $artist->UrlName], Artist::class);
if(isset($result[0])){
return $result[0];
@ -178,6 +178,7 @@ class Artist{
from Artists
where ArtistId = ?
', [$this->ArtistId]);
Db::Query('
DELETE
from ArtistAlternateNames

View file

@ -158,7 +158,7 @@ class Artwork{
from Tags t
inner join ArtworkTags at using (TagId)
where ArtworkId = ?
', [$this->ArtworkId], 'ArtworkTag');
', [$this->ArtworkId], ArtworkTag::class);
}
return $this->_Tags;
@ -849,7 +849,7 @@ class Artwork{
SELECT *
from Artworks
where ArtworkId = ?
', [$artworkId], 'Artwork');
', [$artworkId], Artwork::class);
return $result[0] ?? throw new Exceptions\ArtworkNotFoundException();
}
@ -867,7 +867,7 @@ class Artwork{
from Artworks
inner join Artists using (ArtistId)
where Artists.UrlName = ? and Artworks.UrlName = ?
', [$artistUrlName, $artworkUrlName], 'Artwork');
', [$artistUrlName, $artworkUrlName], Artwork::class);
return $result[0] ?? throw new Exceptions\ArtworkNotFoundException();
}

View file

@ -67,7 +67,7 @@ class ArtworkTag extends Tag{
SELECT *
from Tags
where Name = ?
', [$artworkTag->Name], 'ArtworkTag');
', [$artworkTag->Name], ArtworkTag::class);
if(isset($result[0])){
return $result[0];

View file

@ -48,7 +48,7 @@ class HttpInput{
}
/**
* @return int The maximum size for an HTTP POST request, in bytes
* @return int The maximum size for an HTTP POST request, in bytes.
*/
public static function GetMaxPostSize(): int{
$post_max_size = ini_get('post_max_size');

View file

@ -171,7 +171,7 @@ class Library{
* @param string $query
* @param string $status
* @param ArtworkSort $sort
* @return Array<mixed>
* @return array<string, array<Artwork>|int>
*/
public static function FilterArtwork(string $query = null, string $status = null, ArtworkSort $sort = null, int $submitterUserId = null, int $page = 1, int $perPage = ARTWORK_PER_PAGE): array{
// Returns an array of:
@ -253,7 +253,7 @@ class Library{
where ' . $statusCondition . '
order by ' . $orderBy . '
limit ?
offset ?', $params, 'Artwork');
offset ?', $params, Artwork::class);
}
else{
// Split the query on word boundaries followed by spaces. This keeps words with apostrophes intact.
@ -308,13 +308,12 @@ class Library{
group by art.ArtworkId
order by ' . $orderBy . '
limit ?
offset ?', $params, 'Artwork');
offset ?', $params, Artwork::class);
}
return ['artworks' => $artworks, 'artworksCount' => $artworksCount];
}
/**
* @return array<Artwork>
* @throws Exceptions\ArtistNotFoundException
@ -354,7 +353,7 @@ class Library{
inner join Artists a using (ArtistId)
where ' . $statusCondition . '
and a.UrlName = ?
order by art.Created desc', $params, 'Artwork');
order by art.Created desc', $params, Artwork::class);
return $artworks;
}
@ -819,6 +818,6 @@ class Library{
return Db::Query('
SELECT *
from Artists
order by Name asc', [], 'Artist');
order by Name asc', [], Artist::class);
}
}

View file

@ -592,7 +592,7 @@ class Museum{
from Museums
where ? like concat("%", Domain, "%")
limit 1;
', [$parsedUrl['host']], 'Museum');
', [$parsedUrl['host']], Museum::class);
return $result[0] ?? throw new Exceptions\MuseumNotFoundException();
}

View file

@ -166,7 +166,7 @@ class NewsletterSubscription{
from NewsletterSubscriptions ns
inner join Users u using(UserId)
where u.Uuid = ?
', [$uuid], 'NewsletterSubscription');
', [$uuid], NewsletterSubscription::class);
return $result[0] ?? throw new Exceptions\NewsletterSubscriptionNotFoundException();
}

View file

@ -93,7 +93,7 @@ class Patron{
SELECT *
from Patrons
where UserId = ?
', [$userId], 'Patron');
', [$userId], Patron::class);
return $result[0] ?? throw new Exceptions\PatronNotFoundException();;
}
@ -111,7 +111,7 @@ class Patron{
from Patrons p
inner join Users u using(UserId)
where u.Email = ?
', [$email], 'Patron');
', [$email], Patron::class);
return $result[0] ?? throw new Exceptions\PatronNotFoundException();
}

View file

@ -61,7 +61,7 @@ class Poll{
from PollItems
where PollId = ?
order by SortOrder asc
', [$this->PollId], 'PollItem');
', [$this->PollId], PollItem::class);
}
return $this->_PollItems;
@ -113,7 +113,7 @@ class Poll{
SELECT *
from Polls
where PollId = ?
', [$pollId], 'Poll');
', [$pollId], Poll::class);
return $result[0] ?? throw new Exceptions\PollNotFoundException();
}
@ -130,7 +130,7 @@ class Poll{
SELECT *
from Polls
where UrlName = ?
', [$urlName], 'Poll');
', [$urlName], Poll::class);
return $result[0] ?? throw new Exceptions\PollNotFoundException();
}

View file

@ -51,7 +51,7 @@ class PollItem{
SELECT *
from PollItems
where PollItemId = ?
', [$pollItemId], 'PollItem');
', [$pollItemId], PollItem::class);
return $result[0] ?? throw new Exceptions\PollItemNotFoundException();
}

View file

@ -131,7 +131,7 @@ class PollVote{
inner join Polls p using (PollId)
where p.UrlName = ? ) x using (PollItemId)
where pv.UserId = ?
', [$pollUrlName, $userId], 'PollVote');
', [$pollUrlName, $userId], PollVote::class);
return $result[0] ?? throw new Exceptions\PollVoteNotFoundException();
}

View file

@ -86,7 +86,7 @@ class Session{
from Users u
inner join Sessions s using (UserId)
where s.SessionId = ?
', [$sessionId], 'User');
', [$sessionId], User::class);
if(sizeof($result) > 0){
self::SetSessionCookie($sessionId);
@ -113,7 +113,7 @@ class Session{
SELECT *
from Sessions
where SessionId = ?
', [$sessionId], 'Session');
', [$sessionId], Session::class);
return $result[0] ?? throw new Exceptions\SessionNotFoundException();
}

View file

@ -38,7 +38,7 @@ class User{
from Payments
where UserId = ?
order by Created desc
', [$this->UserId], 'Payment');
', [$this->UserId], Payment::class);
}
return $this->_Payments;
@ -50,7 +50,7 @@ class User{
SELECT *
from Benefits
where UserId = ?
', [$this->UserId], 'Benefits');
', [$this->UserId], Benefits::class);
if(sizeof($result) == 0){
$this->_Benefits = new Benefits();
@ -129,7 +129,7 @@ class User{
SELECT *
from Users
where UserId = ?
', [$userId], 'User');
', [$userId], User::class);
return $result[0] ?? throw new Exceptions\UserNotFoundException();
}
@ -146,7 +146,7 @@ class User{
SELECT *
from Users
where Email = ?
', [$email], 'User');
', [$email], User::class);
return $result[0] ?? throw new Exceptions\UserNotFoundException();
}
@ -169,7 +169,7 @@ class User{
inner join Benefits using (UserId)
where u.Email = ?
or u.Uuid = ?
', [$identifier, $identifier], 'User');
', [$identifier, $identifier], User::class);
if(sizeof($result) == 0){
throw new Exceptions\UserNotFoundException();

View file

@ -26,7 +26,7 @@ $expiredPatrons = Db::Query('
(IsRecurring = false and Amount >= 100 and Created > ? - interval 1 year)
)
)
', [$now, $now], 'Patron');
', [$now, $now], Patron::class);
if(sizeof($expiredPatrons) > 0){
$ebooksThisYear = 0;
@ -70,7 +70,7 @@ if(sizeof($expiredPatrons) > 0){
where UserId = ?
order by Created desc
limit 1
', [$patron->UserId], 'Payment');
', [$patron->UserId], Payment::class);
if(sizeof($lastPayment) > 0 && $patron->User->Email !== null){
$em = new Email();

View file

@ -63,12 +63,14 @@ try{
}
if($queryEbookUrl !== null){
$artworks = Db::Query('SELECT * from Artworks where EbookUrl = ? and Status = ? limit 1', [$queryEbookUrl, ArtworkStatus::Approved], 'Artwork');
$artworks = Db::Query('SELECT * from Artworks where EbookUrl = ? and Status = ? limit 1', [$queryEbookUrl, ArtworkStatus::Approved], Artwork::class);
$totalArtworkCount = sizeof($artworks);
}
else{
$result = Library::FilterArtwork($query, $filterArtworkStatus, $sort, $submitterUserId, $page, $perPage);
/** @var array<Artwork> $artworks */
$artworks = $result['artworks'];
/** @var int $totalArtworkCount */
$totalArtworkCount = $result['artworksCount'];
}

View file

@ -4,7 +4,7 @@ $pastPolls = Db::Query('
from Polls
where utc_timestamp() >= end
order by Start desc
', [], 'Poll');
', [], Poll::class);
$openPolls = Db::Query('
SELECT *
@ -15,7 +15,7 @@ $openPolls = Db::Query('
(Start is null
or Start <= utc_timestamp())
order by Start desc
', [], 'Poll');
', [], Poll::class);
?><?= Template::Header(['title' => 'Polls', 'highlight' => '', 'description' => 'The various polls active at Standard Ebooks.']) ?>
<main>