diff --git a/migrations/Version20240612171415.php b/migrations/Version20240612212104.php similarity index 93% rename from migrations/Version20240612171415.php rename to migrations/Version20240612212104.php index 0fd4508..2af7f21 100644 --- a/migrations/Version20240612171415.php +++ b/migrations/Version20240612212104.php @@ -10,7 +10,7 @@ use Doctrine\Migrations\AbstractMigration; /** * Auto-generated Migration: Please modify to your needs! */ -final class Version20240612171415 extends AbstractMigration +final class Version20240612212104 extends AbstractMigration { public function getDescription(): string { @@ -23,7 +23,8 @@ final class Version20240612171415 extends AbstractMigration $this->addSql('CREATE TABLE commentary (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, post_id INTEGER NOT NULL, profil_id INTEGER NOT NULL, text VARCHAR(255) DEFAULT NULL, CONSTRAINT FK_1CAC12CA4B89032C FOREIGN KEY (post_id) REFERENCES post (id) NOT DEFERRABLE INITIALLY IMMEDIATE, CONSTRAINT FK_1CAC12CA275ED078 FOREIGN KEY (profil_id) REFERENCES profil (id) NOT DEFERRABLE INITIALLY IMMEDIATE)'); $this->addSql('CREATE INDEX IDX_1CAC12CA4B89032C ON commentary (post_id)'); $this->addSql('CREATE INDEX IDX_1CAC12CA275ED078 ON commentary (profil_id)'); - $this->addSql('CREATE TABLE post (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, profil_id INTEGER NOT NULL, title VARCHAR(255) DEFAULT NULL, text VARCHAR(512) DEFAULT NULL, is_dream BOOLEAN NOT NULL, up_vote INTEGER DEFAULT 0 NOT NULL, down_vote INTEGER DEFAULT 0 NOT NULL, CONSTRAINT FK_5A8A6C8D275ED078 FOREIGN KEY (profil_id) REFERENCES profil (id) NOT DEFERRABLE INITIALLY IMMEDIATE)'); + $this->addSql('CREATE TABLE post (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, profil_id INTEGER NOT NULL, title VARCHAR(255) DEFAULT NULL, text VARCHAR(512) DEFAULT NULL, is_dream BOOLEAN NOT NULL, up_vote INTEGER DEFAULT 0 NOT NULL, down_vote INTEGER DEFAULT 0 NOT NULL, created_at DATETIME NOT NULL --(DC2Type:datetime_immutable) + , CONSTRAINT FK_5A8A6C8D275ED078 FOREIGN KEY (profil_id) REFERENCES profil (id) NOT DEFERRABLE INITIALLY IMMEDIATE)'); $this->addSql('CREATE INDEX IDX_5A8A6C8D275ED078 ON post (profil_id)'); $this->addSql('CREATE TABLE post_tags (post_id INTEGER NOT NULL, tags_id INTEGER NOT NULL, PRIMARY KEY(post_id, tags_id), CONSTRAINT FK_A6E9F32D4B89032C FOREIGN KEY (post_id) REFERENCES post (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE, CONSTRAINT FK_A6E9F32D8D7B4FB4 FOREIGN KEY (tags_id) REFERENCES tags (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE)'); $this->addSql('CREATE INDEX IDX_A6E9F32D4B89032C ON post_tags (post_id)'); diff --git a/src/Controller/PostController.php b/src/Controller/PostController.php index 73a0396..345cda2 100644 --- a/src/Controller/PostController.php +++ b/src/Controller/PostController.php @@ -27,7 +27,8 @@ class PostController extends AbstractController $posts = $this->em->getRepository(Post::class)->findAll(); return $this->render('post/all.html.twig', [ - "posts" => $posts + "posts" => $posts, + "title" => "Derniers Posts" ]); } diff --git a/src/Controller/ProfilController.php b/src/Controller/ProfilController.php index e1be9a0..742a049 100644 --- a/src/Controller/ProfilController.php +++ b/src/Controller/ProfilController.php @@ -4,6 +4,7 @@ namespace App\Controller; use App\Entity\Profil; use App\Form\ProfilType; +use App\Repository\PostRepository; use Doctrine\ORM\EntityManager; use SebastianBergmann\Environment\Console; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; @@ -13,22 +14,22 @@ use Symfony\Component\HttpFoundation\Request; class ProfilController extends AbstractController { - - public function __construct(private EntityManager $mgr) + + public function __construct(private EntityManager $mgr, private PostRepository $postRepository) { } - #[Route(path:"/profil", name:"profil_perso", methods: ["GET"])] + #[Route(path: "/profil", name: "profil_perso", methods: ["GET"])] public function baseProfil(): Response { - try{ + try { $this->denyAccessUnlessGranted('IS_AUTHENTICATED'); - }catch (\Exception $e){ + } catch (\Exception $e) { return $this->redirectToRoute('app_login'); } return $this->redirectToRoute('profil_show', ['id' => $this->getUser()->getId()]); } - #[Route('/profil/{id}',name:'profil_show', requirements: ['page' => '\d+'])] + #[Route('/profil/{id}', name: 'profil_show', requirements: ['page' => '\d+'])] public function profil(int $id): Response { $connected = $this->isGranted('ROLE_USER'); @@ -45,23 +46,24 @@ class ProfilController extends AbstractController ]); } - #[Route('/profil/{id}/follow',name:'profil_follow', requirements: ['page' => '\d+'])] - public function followProfil(int $id): Response + + #[Route('/profil/post/follow', name: 'profil_post_follow')] + public function postProfilfollow(): Response { - $profil = $this->mgr->find(Profil::class, $id); - if ($profil instanceof Profil) { - $profil->addFollower($this->getUser()); - $this->mgr->persist($profil); - $this->mgr->flush(); - $this->addFlash('success',''); - return $this->redirectToRoute('profil_show', ['id' => $id]); - } else { - $this->addFlash('error',''); - return $this->render('error.html.twig', []); + try{ + $this->denyAccessUnlessGranted('IS_AUTHENTICATED'); + }catch (\Exception $e){ + return $this->redirectToRoute('app_login'); } + $profil = $this->getUser(); + $posts = $this->postRepository->getPostFromFollowed($profil); + return $this->render('post/all.html.twig', [ + "posts" => $posts, + "title" => "Abonnements" + ]); } - #[Route('/profil/{id}/unfollow',name:'profil_unfollow', requirements: ['page' => '\d+'])] + #[Route('/profil/{id}/unfollow', name: 'profil_unfollow', requirements: ['page' => '\d+'])] public function unfollowProfil(int $id): Response { $profil = $this->mgr->find(Profil::class, $id); @@ -69,34 +71,34 @@ class ProfilController extends AbstractController $profil->removeFollower($this->getUser()); $this->mgr->persist($profil); $this->mgr->flush(); - $this->addFlash('success',''); + $this->addFlash('success', ''); return $this->redirectToRoute('profil_show', ['id' => $id]); } else { - $this->addFlash('error',''); + $this->addFlash('error', ''); return $this->render('error.html.twig', []); } } - #[Route('/profil/{id}/followers',name:'profil_followers', requirements: ['page'=> '\d'])] + #[Route('/profil/{id}/followers', name: 'profil_followers', requirements: ['page' => '\d'])] public function getFollowers(int $id): Response { $profil = $this->mgr->find(Profil::class, $id); return $this->render('profil/follow-list.html.twig', [ - 'title'=> 'Followers', + 'title' => 'Followers', 'profils' => $profil->getFollowers() - ]); + ]); } - #[Route('/profil/{id}/following',name:'profil_following', requirements: ['page'=> '\d'])] + #[Route('/profil/{id}/following', name: 'profil_following', requirements: ['page' => '\d'])] public function getFollowing(int $id): Response { $profil = $this->mgr->find(Profil::class, $id); return $this->render('profil/follow-list.html.twig', [ - 'title'=> 'Following', + 'title' => 'Following', 'profils' => $profil->getFollowing() - ]); + ]); } // #[Route('/profil/new', name: 'profil_new')] @@ -107,26 +109,42 @@ class ProfilController extends AbstractController // return $this->redirectToRoute('profil_show', ['id' => $profil->getId()]); // } - #[Route('/profil/{id}/edit',name:'profil_edit', requirements: ['page'=> '\d'])] - public function editProfil(int $id,Request $request): Response + #[Route('/profil/{id}/edit', name: 'profil_edit', requirements: ['page' => '\d'])] + public function editProfil(int $id, Request $request): Response { $profil = $this->mgr->find(Profil::class, $id); $form = $this->createForm(ProfilType::class, $profil); $form->handleRequest($request); - if( $form->isSubmitted() && $form->isValid() ) { + if ($form->isSubmitted() && $form->isValid()) { $form->getData(); $this->mgr->persist($profil); $this->mgr->flush(); return $this->redirectToRoute('profil_show', ['id' => $id]); } - + return $this->render('profil/edit.html.twig', [ - 'form'=> $form, + 'form' => $form, 'profil' => $profil, - ]); + ]); + } + + #[Route('/profil/{id}/follow', name: 'profil_follow', requirements: ['page' => '\d+'])] + public function followProfil(int $id): Response + { + $profil = $this->mgr->find(Profil::class, $id); + if ($profil instanceof Profil) { + $profil->addFollower($this->getUser()); + $this->mgr->persist($profil); + $this->mgr->flush(); + $this->addFlash('success', ''); + return $this->redirectToRoute('profil_show', ['id' => $id]); + } else { + $this->addFlash('error', ''); + return $this->render('error.html.twig', []); + } } #[Route('/profil/{id}/delete', name: 'profil_delete', methods: ['POST'], requirements: ['id' => '\d+'])] @@ -138,7 +156,7 @@ class ProfilController extends AbstractController throw $this->createNotFoundException('The profile does not exist'); } - if ($this->isCsrfTokenValid('delete'.$profil->getId(), $request->request->get('_token'))) { + if ($this->isCsrfTokenValid('delete' . $profil->getId(), $request->request->get('_token'))) { $this->mgr->remove($profil); $this->mgr->flush(); $this->addFlash('success', 'Profile deleted successfully'); diff --git a/src/Entity/Post.php b/src/Entity/Post.php index 26143f4..d978eec 100644 --- a/src/Entity/Post.php +++ b/src/Entity/Post.php @@ -25,9 +25,6 @@ class Post #[ORM\Column] private ?bool $isDream = null; - // #[ORM\Column()] - // private ?DateTime $dateCreated = null; - #[ORM\Column(options: ["default" => 0])] private int $upVote = 0; @@ -50,10 +47,14 @@ class Post #[ORM\ManyToMany(targetEntity: Tags::class, inversedBy: 'posts')] private Collection $tags; + #[ORM\Column] + private ?\DateTimeImmutable $createdAt = null; + public function __construct() { $this->commentaries = new ArrayCollection(); $this->tags = new ArrayCollection(); + $this->createdAt = new \DateTimeImmutable(); } public function getId(): ?int @@ -97,17 +98,6 @@ class Post return $this; } - public function getDateCreated(): ?DateTime - { - return $this->dateCreated; - } - - public function setDateCreated(?DateTime $dateCreated): static - { - $this->dateCreated = $dateCreated; - - return $this; - } public function getUpVote(): ?int { @@ -198,4 +188,16 @@ class Post return $this; } + + public function getCreatedAt(): ?\DateTimeImmutable + { + return $this->createdAt; + } + + public function setCreatedAt(\DateTimeImmutable $createdAt): static + { + $this->createdAt = $createdAt; + + return $this; + } } diff --git a/src/Repository/PostRepository.php b/src/Repository/PostRepository.php index 3d76065..119a378 100644 --- a/src/Repository/PostRepository.php +++ b/src/Repository/PostRepository.php @@ -3,6 +3,7 @@ namespace App\Repository; use App\Entity\Post; +use App\Entity\Profil; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Persistence\ManagerRegistry; @@ -16,20 +17,21 @@ class PostRepository extends ServiceEntityRepository parent::__construct($registry, Post::class); } - // /** - // * @return Post[] Returns an array of Post objects - // */ - // public function findByExampleField($value): array - // { - // return $this->createQueryBuilder('p') - // ->andWhere('p.exampleField = :val') - // ->setParameter('val', $value) - // ->orderBy('p.id', 'ASC') - // ->setMaxResults(10) - // ->getQuery() - // ->getResult() - // ; - // } + /** + * @return Post[] Returns an array of Post objects + */ + public function getPostFromFollowed(Profil $profil): array + { + return $this->createQueryBuilder('p') + ->innerJoin('p.profil', 'a') + ->innerJoin('a.followers', 'f') + ->where('f.id = :userId') + ->setParameter('userId', $profil->getId()) + ->orderBy('p.createdAt', 'DESC') + ->getQuery() + ->getResult(); + ; + } // public function findOneBySomeField($value): ?Post // { diff --git a/templates/base.html.twig b/templates/base.html.twig index d639b4b..efb418c 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -21,7 +21,7 @@

FukaFukashita