Handling the progress sent from the frontend to the controller.
This commit is contained in:
34
src/Service/ProgressService.php
Normal file
34
src/Service/ProgressService.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Entity\Progress;
|
||||
use App\Repository\BookRepository;
|
||||
use App\Repository\ProgressRepository;
|
||||
|
||||
class ProgressService
|
||||
{
|
||||
private ProgressRepository $progressRepository;
|
||||
private BookRepository $bookRepository;
|
||||
|
||||
public function __construct(ProgressRepository $progressRepository, BookRepository $bookRepository)
|
||||
{
|
||||
$this->progressRepository = $progressRepository;
|
||||
$this->bookRepository = $bookRepository;
|
||||
}
|
||||
|
||||
public function updateProgress(int $bookId, int $progressPages): Progress
|
||||
{
|
||||
/** @var Progress $progress */
|
||||
$progress = $this->progressRepository->findOneBy(['book' => $bookId, 'date' => date_create()]);
|
||||
if (!$progress) {
|
||||
$progress = new Progress();
|
||||
$progress->setBook($this->bookRepository->find($bookId));
|
||||
$progress->setDate(date_create());
|
||||
}
|
||||
$progress->setPages($progressPages);
|
||||
$this->progressRepository->add($progress, true);
|
||||
return $progress;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user