From 4b151bc52f649b8808ac9af02d7737e0e6957b6e Mon Sep 17 00:00:00 2001 From: Corentin R Date: Fri, 14 Jun 2024 00:14:39 +0200 Subject: [PATCH] =?UTF-8?q?Adding=20custom=20op=C3=A9ration?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/routes/api_platform.yaml | 1 + config/services.yaml | 4 ++++ src/Controller/ProfilController.php | 28 ++++++++++++++++++++++++++++ src/Entity/Post.php | 16 ++++++++++++++++ src/Repository/PostRepository.php | 17 +++++++++++++++++ templates/profil/index.html.twig | 20 ++++++++++++++++++++ 6 files changed, 86 insertions(+) create mode 100644 src/Controller/ProfilController.php create mode 100644 templates/profil/index.html.twig diff --git a/config/routes/api_platform.yaml b/config/routes/api_platform.yaml index 38f11cb..8600e0e 100644 --- a/config/routes/api_platform.yaml +++ b/config/routes/api_platform.yaml @@ -2,3 +2,4 @@ api_platform: resource: . type: api_platform prefix: /api + # use_symfony_listeners: true diff --git a/config/services.yaml b/config/services.yaml index 2d6a76f..b5bb9c1 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -22,3 +22,7 @@ services: # add more service definitions when explicit configuration is needed # please note that last definitions always *replace* previous ones + + App\Controller\ProfilController: + arguments: + $entityManager: '@doctrine.orm.entity_manager' \ No newline at end of file diff --git a/src/Controller/ProfilController.php b/src/Controller/ProfilController.php new file mode 100644 index 0000000..58cda66 --- /dev/null +++ b/src/Controller/ProfilController.php @@ -0,0 +1,28 @@ +entityManager->find(Profil::class,$id); + if ($profil instanceof Profil) { + return new Response('',200,$this->postRepository->getPostFromFollowed($profil)); + } + return new Response('',400); + } +} diff --git a/src/Entity/Post.php b/src/Entity/Post.php index 54cfa46..6d8ae80 100644 --- a/src/Entity/Post.php +++ b/src/Entity/Post.php @@ -48,10 +48,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 @@ -184,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..5ad447a 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,6 +17,22 @@ class PostRepository extends ServiceEntityRepository parent::__construct($registry, Post::class); } + /** + * @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(); + ; + } + // /** // * @return Post[] Returns an array of Post objects // */ diff --git a/templates/profil/index.html.twig b/templates/profil/index.html.twig new file mode 100644 index 0000000..775e812 --- /dev/null +++ b/templates/profil/index.html.twig @@ -0,0 +1,20 @@ +{% extends 'base.html.twig' %} + +{% block title %}Hello ProfilController!{% endblock %} + +{% block body %} + + +
+

Hello {{ controller_name }}! ✅

+ + This friendly message is coming from: + +
+{% endblock %}