Adding custom opération
continuous-integration/drone/push Build is passing Details

master
Corentin RICHARD 10 months ago
parent 6141f4e2ca
commit 4b151bc52f

@ -2,3 +2,4 @@ api_platform:
resource: . resource: .
type: api_platform type: api_platform
prefix: /api prefix: /api
# use_symfony_listeners: true

@ -22,3 +22,7 @@ services:
# add more service definitions when explicit configuration is needed # add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones # please note that last definitions always *replace* previous ones
App\Controller\ProfilController:
arguments:
$entityManager: '@doctrine.orm.entity_manager'

@ -0,0 +1,28 @@
<?php
namespace App\Controller;
use App\Repository\PostRepository;
use Doctrine\ORM\EntityManager;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use App\Entity\Profil;
use Symfony\Component\HttpKernel\Attribute\AsController;
#[AsController]
class ProfilController extends AbstractController
{
public function __construct(private PostRepository $postRepository,private EntityManager $entityManager) {}
#[Route('/api/profils/{id}/post/follow', name: 'app_profil_followed')]
public function index(int $id): Response
{
$profil = $this->entityManager->find(Profil::class,$id);
if ($profil instanceof Profil) {
return new Response('',200,$this->postRepository->getPostFromFollowed($profil));
}
return new Response('',400);
}
}

@ -48,10 +48,14 @@ class Post
#[ORM\ManyToMany(targetEntity: Tags::class, inversedBy: 'posts')] #[ORM\ManyToMany(targetEntity: Tags::class, inversedBy: 'posts')]
private Collection $tags; private Collection $tags;
#[ORM\Column]
private ?\DateTimeImmutable $createdAt = null;
public function __construct() public function __construct()
{ {
$this->commentaries = new ArrayCollection(); $this->commentaries = new ArrayCollection();
$this->tags = new ArrayCollection(); $this->tags = new ArrayCollection();
$this->createdAt = new \DateTimeImmutable();
} }
public function getId(): ?int public function getId(): ?int
@ -184,4 +188,16 @@ class Post
return $this; return $this;
} }
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeImmutable $createdAt): static
{
$this->createdAt = $createdAt;
return $this;
}
} }

@ -3,6 +3,7 @@
namespace App\Repository; namespace App\Repository;
use App\Entity\Post; use App\Entity\Post;
use App\Entity\Profil;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry; use Doctrine\Persistence\ManagerRegistry;
@ -16,6 +17,22 @@ class PostRepository extends ServiceEntityRepository
parent::__construct($registry, Post::class); 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 // * @return Post[] Returns an array of Post objects
// */ // */

@ -0,0 +1,20 @@
{% extends 'base.html.twig' %}
{% block title %}Hello ProfilController!{% endblock %}
{% block body %}
<style>
.example-wrapper { margin: 1em auto; max-width: 800px; width: 95%; font: 18px/1.5 sans-serif; }
.example-wrapper code { background: #F5F5F5; padding: 2px 6px; }
</style>
<div class="example-wrapper">
<h1>Hello {{ controller_name }}! ✅</h1>
This friendly message is coming from:
<ul>
<li>Your controller at <code>/home/Koroh/Repos/Fukashitapi/src/Controller/ProfilController.php</code></li>
<li>Your template at <code>/home/Koroh/Repos/Fukashitapi/templates/profil/index.html.twig</code></li>
</ul>
</div>
{% endblock %}
Loading…
Cancel
Save