Skip to content

Commit

Permalink
fixed some api theme storage standards (#36)
Browse files Browse the repository at this point in the history
added list of all themes
  • Loading branch information
Fabricio872 authored Dec 4, 2024
1 parent 5842fbd commit 70fba6e
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 7 deletions.
37 changes: 37 additions & 0 deletions migrations/Version20241204091157.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20241204091157 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->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');
}
}
29 changes: 22 additions & 7 deletions src/Controller/Api/ThemeStorageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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',
Expand All @@ -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'])]
Expand All @@ -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'])]
Expand Down
18 changes: 18 additions & 0 deletions src/Entity/Theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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,
Expand Down Expand Up @@ -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;
}
}
37 changes: 37 additions & 0 deletions src/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,19 @@ class User implements UserInterface
#[ORM\OneToMany(targetEntity: Conversation::class, mappedBy: 'owner', cascade: ['remove'], orphanRemoval: true)]
private Collection $conversations;

/**
* @var Collection<int, Theme>
*/
#[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
Expand Down Expand Up @@ -115,4 +122,34 @@ public function removeConversation(Conversation $conversation): static

return $this;
}

/**
* @return Collection<int, Theme>
*/
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;
}
}

0 comments on commit 70fba6e

Please sign in to comment.