From 70fba6eb010bb13162193799ab66ff3677c48370 Mon Sep 17 00:00:00 2001 From: Fabricio872 Date: Wed, 4 Dec 2024 10:45:10 +0100 Subject: [PATCH] fixed some api theme storage standards (#36) added list of all themes --- migrations/Version20241204091157.php | 37 +++++++++++++++++++ src/Controller/Api/ThemeStorageController.php | 29 +++++++++++---- src/Entity/Theme.php | 18 +++++++++ src/Entity/User.php | 37 +++++++++++++++++++ 4 files changed, 114 insertions(+), 7 deletions(-) create mode 100644 migrations/Version20241204091157.php diff --git a/migrations/Version20241204091157.php b/migrations/Version20241204091157.php new file mode 100644 index 0000000..43f76c8 --- /dev/null +++ b/migrations/Version20241204091157.php @@ -0,0 +1,37 @@ +addSql('ALTER TABLE theme ADD owner_id UUID NOT NULL'); + $this->addSql('COMMENT ON COLUMN theme.owner_id IS \'(DC2Type:uuid)\''); + $this->addSql('ALTER TABLE theme ADD CONSTRAINT FK_9775E7087E3C61F9 FOREIGN KEY (owner_id) REFERENCES "user" (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); + $this->addSql('CREATE INDEX IDX_9775E7087E3C61F9 ON theme (owner_id)'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('CREATE SCHEMA public'); + $this->addSql('ALTER TABLE theme DROP CONSTRAINT FK_9775E7087E3C61F9'); + $this->addSql('DROP INDEX IDX_9775E7087E3C61F9'); + $this->addSql('ALTER TABLE theme DROP owner_id'); + } +} diff --git a/src/Controller/Api/ThemeStorageController.php b/src/Controller/Api/ThemeStorageController.php index bb3718d..519d4ab 100644 --- a/src/Controller/Api/ThemeStorageController.php +++ b/src/Controller/Api/ThemeStorageController.php @@ -4,16 +4,18 @@ use App\DataTransferObject\Bootstrap53\CustomizerFormBootstrap53Dto; use App\Entity\Theme; +use App\Entity\User; use Doctrine\ORM\EntityManagerInterface; use OpenApi\Attributes as OA; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\MapRequestPayload; use Symfony\Component\Routing\Attribute\Route; +use Symfony\Component\Security\Http\Attribute\CurrentUser; use Symfony\Component\Security\Http\Attribute\IsGranted; #[OA\Tag(name: 'Theme Storage')] -#[Route("/theme")] +#[Route("/themes")] #[IsGranted('IS_AUTHENTICATED_FULLY')] class ThemeStorageController extends AbstractController { @@ -38,18 +40,33 @@ public function __construct( #[OA\Response( response: 201, description: 'Returns stored variables json', - content: new OA\MediaType(mediaType: 'text/css|text/x-scss') )] #[OA\Post(security: [['BearerAuth' => []]])] #[Route("/{title}", methods: ['POST'])] - public function post(string $title, #[MapRequestPayload(serializationContext: ['groups' => ['compile']])] CustomizerFormBootstrap53Dto $bootstrap53Dto): Response + public function post( + string $title, + #[MapRequestPayload(serializationContext: ['groups' => ['compile']])] CustomizerFormBootstrap53Dto $bootstrap53Dto, + #[CurrentUser] User $user + ): Response { - $theme = new Theme($title, $bootstrap53Dto); - $this->em->persist($theme); + $theme = new Theme(name: $title, dto: $bootstrap53Dto); + + $this->em->persist($user->addTheme($theme)); $this->em->flush(); return $this->json($theme, Response::HTTP_CREATED); } + #[OA\Response( + response: 200, + description: 'Returns list of themes', + )] + #[OA\Get(security: [['BearerAuth' => []]])] + #[Route("", methods: ['GET'])] + public function getAll(#[CurrentUser] User $user): Response + { + return $this->json($user->getThemes()); + } + #[OA\Parameter( parameter: 'id', name: 'id', @@ -60,7 +77,6 @@ public function post(string $title, #[MapRequestPayload(serializationContext: [' #[OA\Response( response: 200, description: 'Returns stored variables json', - content: new OA\MediaType(mediaType: 'text/css|text/x-scss') )] #[OA\Get(security: [['BearerAuth' => []]])] #[Route("/{id}", methods: ['GET'])] @@ -84,7 +100,6 @@ public function get(Theme $theme): Response #[OA\Response( response: 202, description: 'Returns updated variables json', - content: new OA\MediaType(mediaType: 'text/css|text/x-scss') )] #[OA\Patch(security: [['BearerAuth' => []]])] #[Route("/{id}", methods: ['PATCH'])] diff --git a/src/Entity/Theme.php b/src/Entity/Theme.php index 0ebf8b9..ef64078 100644 --- a/src/Entity/Theme.php +++ b/src/Entity/Theme.php @@ -9,6 +9,7 @@ use Doctrine\ORM\Mapping as ORM; use Symfony\Bridge\Doctrine\Types\UuidType; use Symfony\Component\Serializer\Attribute\Groups; +use Symfony\Component\Serializer\Attribute\Ignore; use Symfony\Component\Uid\Uuid; #[ORM\Entity(repositoryClass: ThemeRepository::class)] @@ -27,6 +28,11 @@ class Theme #[ORM\Column] private DateTimeImmutable $createdAt; + #[ORM\ManyToOne(inversedBy: 'themes')] + #[ORM\JoinColumn(nullable: false)] + #[Ignore] + private User $owner; + public function __construct( #[ORM\Column(length: 255)] private ?string $name, @@ -77,4 +83,16 @@ public function setDto(object $dto): static return $this; } + + public function getOwner(): ?User + { + return $this->owner; + } + + public function setOwner(?User $owner): static + { + $this->owner = $owner; + + return $this; + } } diff --git a/src/Entity/User.php b/src/Entity/User.php index f48f176..4b407c6 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -38,12 +38,19 @@ class User implements UserInterface #[ORM\OneToMany(targetEntity: Conversation::class, mappedBy: 'owner', cascade: ['remove'], orphanRemoval: true)] private Collection $conversations; + /** + * @var Collection + */ + #[ORM\OneToMany(targetEntity: Theme::class, mappedBy: 'owner', cascade: ['persist', 'remove'], orphanRemoval: true)] + private Collection $themes; + public function __construct( #[ORM\Column(length: 255)] private ?string $email ) { $this->createdAt = new DateTimeImmutable(); $this->conversations = new ArrayCollection(); + $this->themes = new ArrayCollection(); } public function getId(): ?Uuid @@ -115,4 +122,34 @@ public function removeConversation(Conversation $conversation): static return $this; } + + /** + * @return Collection + */ + public function getThemes(): Collection + { + return $this->themes; + } + + public function addTheme(Theme $theme): static + { + if (!$this->themes->contains($theme)) { + $this->themes->add($theme); + $theme->setOwner($this); + } + + return $this; + } + + public function removeTheme(Theme $theme): static + { + if ($this->themes->removeElement($theme)) { + // set the owning side to null (unless already changed) + if ($theme->getOwner() === $this) { + $theme->setOwner(null); + } + } + + return $this; + } }