Record and summarize Ebook downloads in the DB (#498)

This commit is contained in:
Mike Colagrosso 2025-05-22 10:23:24 -06:00 committed by GitHub
parent 61b8ca27b1
commit 475c437126
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 289 additions and 4 deletions

View file

@ -0,0 +1,52 @@
<?
use Safe\DateTimeImmutable;
class EbookDownloadSummary{
public int $EbookId;
public DateTimeImmutable $Date;
public int $DownloadCount = 0;
public int $BotDownloadCount = 0;
public function __construct(int $ebookId, DateTimeImmutable $date){
$this->EbookId = $ebookId;
$this->Date = $date;
}
/**
* @throws Exceptions\InvalidEbookDownloadSummaryException
*/
public function Validate(): void{
$error = new Exceptions\InvalidEbookDownloadSummaryException();
if($this->DownloadCount < 0){
$error->Add(new Exceptions\InvalidEbookDownloadCountException('Invalid EbookDownloadSummary DownloadCount: ' . $this->DownloadCount));
}
if($this->BotDownloadCount < 0){
$error->Add(new Exceptions\InvalidEbookDownloadCountException('Invalid EbookDownloadSummary BotDownloadCount: ' . $this->BotDownloadCount));
}
if($error->HasExceptions){
throw $error;
}
}
/**
* @throws Exceptions\InvalidEbookDownloadSummaryException
*/
public function Create(): void{
$this->Validate();
Db::Query('
INSERT into EbookDownloadSummaries (EbookId, Date, DownloadCount, BotDownloadCount)
values (?,
?,
?,
?)
on duplicate key update
DownloadCount = value(DownloadCount),
BotDownloadCount = value(BotDownloadCount)
', [$this->EbookId, $this->Date, $this->DownloadCount, $this->BotDownloadCount]);
}
}