diff --git a/migrations/Version20250511050008.php b/migrations/Version20250511050008.php new file mode 100644 index 0000000..e5106aa --- /dev/null +++ b/migrations/Version20250511050008.php @@ -0,0 +1,47 @@ +addSql(<<<'SQL' + ALTER TABLE download ADD user_id INT DEFAULT NULL + SQL); + $this->addSql(<<<'SQL' + ALTER TABLE download ADD CONSTRAINT FK_781A8270A76ED395 FOREIGN KEY (user_id) REFERENCES user (id) + SQL); + $this->addSql(<<<'SQL' + CREATE INDEX IDX_781A8270A76ED395 ON download (user_id) + SQL); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql(<<<'SQL' + ALTER TABLE download DROP FOREIGN KEY FK_781A8270A76ED395 + SQL); + $this->addSql(<<<'SQL' + DROP INDEX IDX_781A8270A76ED395 ON download + SQL); + $this->addSql(<<<'SQL' + ALTER TABLE download DROP user_id + SQL); + } +} diff --git a/src/Controller/IndexController.php b/src/Controller/IndexController.php index c7326d4..b85a4eb 100644 --- a/src/Controller/IndexController.php +++ b/src/Controller/IndexController.php @@ -18,10 +18,10 @@ final class IndexController extends AbstractController #[Route('/', name: 'app_index')] public function index(): Response { -// dd($this->getUser()); +// dd($this->getUser()->getActiveDownloads()); return $this->render('index/index.html.twig', [ - 'active_downloads' => $this->downloadRepository->getActivePaginated(), - 'recent_downloads' => $this->downloadRepository->latest(5), + 'active_downloads' => $this->getUser()->getActiveDownloads(), + 'recent_downloads' => $this->getUser()->getDownloads(), 'popular_movies' => $this->tmdb->popularMovies(1, 6), 'popular_tvshows' => $this->tmdb->popularTvShows(1, 6), ]); diff --git a/src/Download/Framework/Entity/Download.php b/src/Download/Framework/Entity/Download.php index e802dce..73b6317 100644 --- a/src/Download/Framework/Entity/Download.php +++ b/src/Download/Framework/Entity/Download.php @@ -3,6 +3,7 @@ namespace App\Download\Framework\Entity; use App\Download\Framework\Repository\DownloadRepository; +use App\User\Framework\Entity\User; use Doctrine\ORM\Mapping as ORM; use Symfony\UX\Turbo\Attribute\Broadcast; @@ -39,6 +40,9 @@ class Download #[ORM\Column(length: 255, nullable: true)] private ?string $batchId = null; + #[ORM\ManyToOne(inversedBy: 'downloads')] + private ?User $user = null; + public function getId(): ?int { return $this->id; @@ -146,4 +150,16 @@ class Download return $this; } + + public function getUser(): ?User + { + return $this->user; + } + + public function setUser(?User $user): static + { + $this->user = $user; + + return $this; + } } diff --git a/src/Twig/Components/ActiveDownloadList.php b/src/Twig/Components/ActiveDownloadList.php index 0856afc..273354b 100644 --- a/src/Twig/Components/ActiveDownloadList.php +++ b/src/Twig/Components/ActiveDownloadList.php @@ -3,12 +3,13 @@ namespace App\Twig\Components; use App\Download\Framework\Repository\DownloadRepository; +use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\UX\LiveComponent\Attribute\AsLiveComponent; use Symfony\UX\LiveComponent\Attribute\LiveAction; use Symfony\UX\LiveComponent\DefaultActionTrait; #[AsLiveComponent] -final class ActiveDownloadList +final class ActiveDownloadList extends AbstractController { use DefaultActionTrait; @@ -19,6 +20,6 @@ final class ActiveDownloadList #[LiveAction] public function getDownloads() { - return $this->downloadRepository->getActivePaginated(); + return $this->getUser()->getActiveDownloads(); } } diff --git a/src/User/Framework/Entity/User.php b/src/User/Framework/Entity/User.php index 0e2b842..2226705 100644 --- a/src/User/Framework/Entity/User.php +++ b/src/User/Framework/Entity/User.php @@ -3,6 +3,7 @@ namespace App\User\Framework\Entity; use Aimeos\Map; +use App\Download\Framework\Entity\Download; use App\Monitor\Framework\Entity\Monitor; use App\User\Framework\Repository\UserRepository; use Doctrine\Common\Collections\ArrayCollection; @@ -49,10 +50,17 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface #[ORM\OneToMany(targetEntity: Monitor::class, mappedBy: 'user', orphanRemoval: true)] private Collection $yes; + /** + * @var Collection + */ + #[ORM\OneToMany(targetEntity: Download::class, mappedBy: 'user')] + private Collection $downloads; + public function __construct() { $this->userPreferences = new ArrayCollection(); $this->yes = new ArrayCollection(); + $this->downloads = new ArrayCollection(); } public function getId(): ?int @@ -256,4 +264,42 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface return $this; } + + /** + * @return Collection + */ + public function getDownloads(): Collection + { + return $this->downloads; + } + + /** + * @return Collection + */ + public function getActiveDownloads(): Collection + { + return $this->downloads->filter(fn(Download $download) => in_array($download->getStatus(), ['New', 'In Progress'])); + } + + public function addDownload(Download $download): static + { + if (!$this->downloads->contains($download)) { + $this->downloads->add($download); + $download->setUser($this); + } + + return $this; + } + + public function removeDownload(Download $download): static + { + if ($this->downloads->removeElement($download)) { + // set the owning side to null (unless already changed) + if ($download->getUser() === $this) { + $download->setUser(null); + } + } + + return $this; + } }