wip: scopes downloads to usrs

This commit is contained in:
2025-05-11 00:12:55 -05:00
parent 854177a121
commit 6817bd8c80
5 changed files with 115 additions and 5 deletions

View File

@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20250511050008 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->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);
}
}

View File

@@ -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),
]);

View File

@@ -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;
}
}

View File

@@ -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();
}
}

View File

@@ -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<int, Download>
*/
#[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<int, Download>
*/
public function getDownloads(): Collection
{
return $this->downloads;
}
/**
* @return Collection<int, Download>
*/
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;
}
}