Adapter la création d'une discussion

On créé un repository pour DiscussionUserRepository

<?php

namespace App\Repository;

use App\Entity\DiscussionUser;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

/**
 * @method DiscussionUser|null find($id, $lockMode = null, $lockVersion = null)
 * @method DiscussionUser|null findOneBy(array $criteria, array $orderBy = null)
 * @method DiscussionUser[]    findAll()
 * @method DiscussionUser[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class DiscussionUserRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, DiscussionUser::class);
    }

    // /**
    //  * @return DiscussionUser[] Returns an array of DiscussionUser objects
    //  */
    /*
    public function findByExampleField($value)
    {
        return $this->createQueryBuilder('d')
            ->andWhere('d.exampleField = :val')
            ->setParameter('val', $value)
            ->orderBy('d.id', 'ASC')
            ->setMaxResults(10)
            ->getQuery()
            ->getResult()
        ;
    }
    */

    /*
    public function findOneBySomeField($value): ?DiscussionUser
    {
        return $this->createQueryBuilder('d')
            ->andWhere('d.exampleField = :val')
            ->setParameter('val', $value)
            ->getQuery()
            ->getOneOrNullResult()
        ;
    }
    */
}

On l'indique dans notre entité Discussion

/**
 * @ApiResource(collectionOperations={}, itemOperations={"get"})
 * @ORM\Table(name="discussion_user")
 * @ORM\Entity(repositoryClass=DiscussionUserRepository::class)
 */
class DiscussionUser

Puis on adapte notre CreateDiscussionOutputDataTransformer

<?php

namespace App\DataTransformer;

use ApiPlatform\Core\DataTransformer\DataTransformerInterface;
use App\Dto\CheeseListingOutput;
use App\DTO\CreateDiscussionOutput;
use App\Entity\CheeseListing;
use App\Entity\Discussion;
use App\Repository\DiscussionUserRepository;
use App\Repository\MessageRepository;

class CreateDiscussionOutputDataTransformer implements DataTransformerInterface
{
    private $messageRepository;
    private $discussionUserRepository;

    public function __construct(
        MessageRepository $messageRepository,
        DiscussionUserRepository $discussionUserRepository
    ) {
        $this->messageRepository = $messageRepository;
        $this->discussionUserRepository = $discussionUserRepository;
    }

    /**
     * @param Discussion $object
     * @param string $to
     * @param array $context
     * @return CreateDiscussionOutput
     */
    public function transform($object, string $to, array $context = []): CreateDiscussionOutput
    {
        $output = new CreateDiscussionOutput();

        $output->contentMessage = $this->messageRepository->findOneBy(['discussion' => $object->getId()])->getContent();
        $output->participants = $this->discussionUserRepository->findBy(['discussion' => $object->getId()]);

        return $output;
    }

    public function supportsTransformation($data, string $to, array $context = []): bool
    {
        if (!$data instanceof Discussion) {
            return false;
        }

        return CreateDiscussionOutput::class === $to;
    }

}

Maintenant quand on créé une nouvelle discussion avec POST /discussions et un payload

{
    "participants": [
        "/api/users/10",
        "/api/users/11", 
        "/api/users/12"
    ],
    "contentMessage" : "{{$randomLoremSentences}}"
}

on à comme réponse

{
    "@context": {
        "@vocab": "https://127.0.0.1:8080/api/docs.jsonld#",
        "hydra": "http://www.w3.org/ns/hydra/core#",
        "participants": "CreateDiscussionOutput/participants",
        "contentMessage": "CreateDiscussionOutput/contentMessage"
    },
    "@type": "Discussion",
    "@id": "/api/discussions/308",
    "participants": [
        {
            "@context": "/api/contexts/DiscussionUser",
            "@id": "/api/discussion_users/discussion=308;participants=10",
            "@type": "DiscussionUser",
            "participants": "/api/users/10"
        },
        {
            "@context": "/api/contexts/DiscussionUser",
            "@id": "/api/discussion_users/discussion=308;participants=11",
            "@type": "DiscussionUser",
            "participants": "/api/users/11"
        },
        {
            "@context": "/api/contexts/DiscussionUser",
            "@id": "/api/discussion_users/discussion=308;participants=12",
            "@type": "DiscussionUser",
            "participants": "/api/users/12"
        }
    ],
    "contentMessage": "Nihil animi voluptate iste quisquam qui magni fuga. Excepturi sunt aut quia veritatis omnis omnis quo. Repudiandae exercitationem autem incidunt dolorem non ipsa est sunt. Doloribus ad aliquam fugit occaecati necessitatibus quia. Commodi iusto quia officiis fugiat. Fuga est est quaerat."
}

Commentaires

Connectez-vous pour laisser un commentaire