diff --git a/migrations/Version20210105205256.php b/migrations/Version20210105205256.php new file mode 100644 index 0000000..dd975c0 --- /dev/null +++ b/migrations/Version20210105205256.php @@ -0,0 +1,50 @@ +addSql('CREATE TABLE video (id BLOB NOT NULL, uploader_id BLOB NOT NULL, uploaded DATETIME NOT NULL --(DC2Type:datetime_immutable) + , name VARCHAR(255) NOT NULL, description VARCHAR(1024) NOT NULL, tags CLOB NOT NULL --(DC2Type:array) + , PRIMARY KEY(id))'); + $this->addSql('CREATE INDEX IDX_7CC7DA2C16678C77 ON video (uploader_id)'); + $this->addSql('DROP INDEX UNIQ_8D93D6495E237E06'); + $this->addSql('CREATE TEMPORARY TABLE __temp__user AS SELECT id, password, name, roles FROM user'); + $this->addSql('DROP TABLE user'); + $this->addSql('CREATE TABLE user (id BLOB NOT NULL, password VARCHAR(255) NOT NULL COLLATE BINARY, name VARCHAR(180) NOT NULL COLLATE BINARY, roles CLOB NOT NULL COLLATE BINARY --(DC2Type:json) + , PRIMARY KEY(id))'); + $this->addSql('INSERT INTO user (id, password, name, roles) SELECT id, password, name, roles FROM __temp__user'); + $this->addSql('DROP TABLE __temp__user'); + $this->addSql('CREATE UNIQUE INDEX UNIQ_8D93D6495E237E06 ON user (name)'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('DROP TABLE video'); + $this->addSql('DROP INDEX UNIQ_8D93D6495E237E06'); + $this->addSql('CREATE TEMPORARY TABLE __temp__user AS SELECT id, name, roles, password FROM user'); + $this->addSql('DROP TABLE user'); + $this->addSql('CREATE TABLE user (id BLOB NOT NULL, name VARCHAR(180) NOT NULL, roles CLOB NOT NULL --(DC2Type:json) + , password VARCHAR(255) NOT NULL, PRIMARY KEY(id))'); + $this->addSql('INSERT INTO user (id, name, roles, password) SELECT id, name, roles, password FROM __temp__user'); + $this->addSql('DROP TABLE __temp__user'); + $this->addSql('CREATE UNIQUE INDEX UNIQ_8D93D6495E237E06 ON user (name)'); + } +} diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index e78cacd..4531ef6 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -4,6 +4,8 @@ namespace App\Controller; +use App\Repository\UserRepository; +use App\Repository\VideoRepository; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; @@ -11,6 +13,15 @@ use Symfony\Component\Routing\Annotation\Route; class HomeController extends AbstractController { + private $userRepository; + private $videoRepository; + + public function __construct(UserRepository $userRepository, VideoRepository $videoRepository) + { + $this->userRepository = $userRepository; + $this->videoRepository = $videoRepository; + } + /** * @Route("/", name="app_home") */ @@ -21,6 +32,11 @@ class HomeController extends AbstractController return $this->redirectToRoute("app_login"); } + $user = $this->userRepository->findOneByName($this->getUser()->getUsername()); + $videos = $this->videoRepository->findByUploader($user); + + dump($videos); + return $this->render("home/dashboard.html.twig"); } } \ No newline at end of file diff --git a/src/DataFixtures/UserFixtures.php b/src/DataFixtures/UserFixtures.php index 4bcada6..b73fa67 100644 --- a/src/DataFixtures/UserFixtures.php +++ b/src/DataFixtures/UserFixtures.php @@ -24,8 +24,8 @@ class UserFixtures extends Fixture $admin = new User(); $admin->setName("admin"); $admin->setPassword($this->passwordEncoder->encodePassword($admin, "password")); - $manager->persist($admin); + $manager->flush(); } } diff --git a/src/Entity/User.php b/src/Entity/User.php index 66bf4f1..d968460 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -3,6 +3,8 @@ namespace App\Entity; use App\Repository\UserRepository; +use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; use Ramsey\Uuid\Doctrine\UuidGenerator; use Ramsey\Uuid\UuidInterface; @@ -37,6 +39,16 @@ class User implements UserInterface */ private $password; + /** + * @ORM\OneToMany(targetEntity=Video::class, mappedBy="uploader", orphanRemoval=true) + */ + private $videos; + + public function __construct() + { + $this->videos = new ArrayCollection(); + } + public function getId(): ?UuidInterface { return $this->id; @@ -114,4 +126,34 @@ class User implements UserInterface // If you store any temporary, sensitive data on the user, clear it here // $this->plainPassword = null; } + + /** + * @return Collection|Video[] + */ + public function getVideos(): Collection + { + return $this->videos; + } + + public function addVideo(Video $video): self + { + if (!$this->videos->contains($video)) { + $this->videos[] = $video; + $video->setUploader($this); + } + + return $this; + } + + public function removeVideo(Video $video): self + { + if ($this->videos->removeElement($video)) { + // set the owning side to null (unless already changed) + if ($video->getUploader() === $this) { + $video->setUploader(null); + } + } + + return $this; + } } diff --git a/src/Entity/Video.php b/src/Entity/Video.php new file mode 100644 index 0000000..4d6e47d --- /dev/null +++ b/src/Entity/Video.php @@ -0,0 +1,112 @@ +uploaded = new DateTimeImmutable(); + } + + public function getId(): ?UuidInterface + { + return $this->id; + } + + public function getUploader(): ?User + { + return $this->uploader; + } + + public function setUploader(?User $uploader): self + { + $this->uploader = $uploader; + + return $this; + } + + public function getUploaded(): ?DateTimeImmutable + { + return $this->uploaded; + } + + public function getName(): ?string + { + return $this->name; + } + + public function setName(string $name): self + { + $this->name = $name; + + return $this; + } + + public function getDescription(): ?string + { + return $this->description; + } + + public function setDescription(string $description): self + { + $this->description = $description; + + return $this; + } + + public function getTags(): ?array + { + return $this->tags; + } + + public function setTags(array $tags): self + { + $this->tags = $tags; + + return $this; + } +} diff --git a/src/Repository/UserRepository.php b/src/Repository/UserRepository.php index 432c414..5948d73 100644 --- a/src/Repository/UserRepository.php +++ b/src/Repository/UserRepository.php @@ -54,15 +54,12 @@ class UserRepository extends ServiceEntityRepository implements PasswordUpgrader } */ - /* - public function findOneBySomeField($value): ?User + public function findOneByName($value): ?User { return $this->createQueryBuilder('u') - ->andWhere('u.exampleField = :val') + ->andWhere('u.name = :val') ->setParameter('val', $value) ->getQuery() - ->getOneOrNullResult() - ; + ->getOneOrNullResult(); } - */ } diff --git a/src/Repository/VideoRepository.php b/src/Repository/VideoRepository.php new file mode 100644 index 0000000..6616dea --- /dev/null +++ b/src/Repository/VideoRepository.php @@ -0,0 +1,48 @@ +createQueryBuilder('v') + ->andWhere('v.exampleField = :val') + ->setParameter('val', $value) + ->orderBy('v.id', 'ASC') + ->setMaxResults(10) + ->getQuery() + ->getResult() + ; + } + */ + + /*public function findByUploader(User $user): array + { + return $this->createQueryBuilder('v') + ->andWhere('v.uploader_id = :val') + ->setParameter('val', $user->getId()) + ->getQuery() + ->getArrayResult() + ; + }*/ +}