Segelparade / www / symfonyproject / src / Controller / ConversationsController.php
ConversationsController.php
Raw
<?php

namespace App\Controller;

use App\Entity\Image;
use App\Entity\Conversation;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;

class ConversationsController extends AbstractController
{
    #
    #   This Controller manages the Conversations in a CRUD-manner
    #   Supporting GET (all conv. of given status or specific image), POST (creating aconv.), PATCH (mark conv. as closed)
    #   Currently not supporting Deletion of conv.
    #




    /**
     * Method: GET
     * Endpoint: /backend/conversations/status/{status}
     * listing all conversations of given Status !Returning Template render! status-> open | closed
     */
    public function list(EntityManagerInterface $entityManager, $status): Response
    {
        $conversations = $entityManager->getRepository(Conversation::class)->findAllByStatus($status);

        return $this->render('backend/conversations.twig', [
            'conversations' => $conversations,
            'status' => $status,
        ]);
    }

    /**
     * Method: POST
     * Endpoint: /backend/conversations (Parameters id-> ImageID, message->Conversation topic)
     * Creating a conversation with given Topic for given ID(pictureID)
     */
    public function create(Request $request, EntityManagerInterface $entityManager): JsonResponse
    {
        $data = json_decode($request->getContent(), true);

        $id = $data['id'] ?? null;
        $message = $data['message'] ?? null;

        if ($id && $message) {
            $image = $entityManager->getRepository(Image::class)->find($id);
            if (!$image) {
                return new JsonResponse(['error' => 'Image not found'], Response::HTTP_NOT_FOUND);
            }

            $newConversation = new Conversation();
            $newConversation->setType("open");
            $newConversation->setMessage($message);
            $newConversation->setSend(new \DateTime());

            $image->addConversation($newConversation);

            $entityManager->persist($newConversation);
            $entityManager->flush();

            return new JsonResponse(true);
        } else {
            return new JsonResponse(['error' => 'Invalid parameters'], Response::HTTP_BAD_REQUEST);
        }
    }

    /**
     * Method: PATCH
     * Endpoint: /backend/conversations/{id}
     * partial Deletion of Conversation by setting it closed based on given ID (ConversationID)
     */
    public function close($id, EntityManagerInterface $entityManager): JsonResponse
    {
        $conversation = $entityManager->getRepository(Conversation::class)->find($id);
        if (!$conversation) {
            return new JsonResponse(['error' => 'Conversation not found'], Response::HTTP_NOT_FOUND);
        }

        $conversation->setType("closed");
        $entityManager->persist($conversation);
        $entityManager->flush();

        return new JsonResponse(true);
    }

    /**
     * Method: GET
     * Endpoint: /backend/conversations/image/{id}
     * Return an json object representing conversations of image with given id (ImageID), send -> Creation date, message -> topic, type-> open | closed
     */
    public function read($id, EntityManagerInterface $entityManager): JsonResponse
    {
        $image = $entityManager->getRepository(Image::class)->find($id);
        if (!$image) {
            return new JsonResponse(['error' => 'Image not found'], Response::HTTP_NOT_FOUND);
        }

        $conversations = $image->getConversation();
        $conversationsData = [];
        foreach ($conversations as $conversation) {
            $conversationsData[] = [
                'id' => $conversation->getId(),
                'send' => $conversation->getSend(),
                'message' => $conversation->getMessage(),
                'type' => $conversation->getType(),
            ];
        }

        return new JsonResponse($conversationsData);
    }
}