Skip to content

Commit

Permalink
feat(api): added initial core logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreimus committed Aug 25, 2024
1 parent 35041a7 commit 67439cf
Show file tree
Hide file tree
Showing 21 changed files with 776 additions and 0 deletions.
19 changes: 19 additions & 0 deletions config/routes.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# routes.yml

prestasafe_prettyblocks_zone_list:
path: /prettyblocks/api/zones
methods: [ GET ]
defaults:
_controller: 'prestasafe.prettyblocks.controller.zone_controller:listZones'

prestasafe_prettyblocks_zone_get:
path: /prettyblocks/api/zones/{id}
methods: [ GET ]
defaults:
_controller: 'prestasafe.prettyblocks.controller.zone_controller:getZone'

prestasafe_prettyblocks_zone_update:
path: /prettyblocks/api/zones/{id}
methods: [ PUT ]
defaults:
_controller: 'prestasafe.prettyblocks.controller.zone_controller:updateZone'
49 changes: 49 additions & 0 deletions config/services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# config.yml

services:
default:
public: true
autoconfigure: true
autowire: true

# Registry pour enregistrer dynamiquement les types d'entités
prestasafe.prettyblocks.registry.entity_registry:
class: PrestaSafe\PrettyBlocks\Registry\EntityRegistry

# Factory pour la création dynamique des entités
prestasafe.prettyblocks.factory.entity_factory:
class: PrestaSafe\PrettyBlocks\Factory\EntityFactory
arguments:
- '@prestasafe.prettyblocks.registry.entity_registry'

# Repositories Doctrine
prestasafe.prettyblocks.repository.block_repository:
class: PrestaSafe\PrettyBlocks\Repository\BlockRepository
factory: [ '@doctrine', getRepository ]

prestasafe.prettyblocks.repository.component_repository:
class: PrestaSafe\PrettyBlocks\Repository\ComponentRepository
factory: [ '@doctrine', getRepository ]

prestasafe.prettyblocks.repository.primitive_field_repository:
class: PrestaSafe\PrettyBlocks\Repository\PrimitiveFieldRepository
factory: [ '@doctrine', getRepository ]

prestasafe.prettyblocks.repository.zone_repository:
class: PrestaSafe\PrettyBlocks\Repository\ZoneRepository
factory: [ '@doctrine', getRepository ]

# Service principal pour la gestion des zones
prestasafe.prettyblocks.service.zone_service:
class: PrestaSafe\PrettyBlocks\Service\ZoneService
arguments:
- '@prestasafe.prettyblocks.repository.zone_repository'
- '@prestasafe.prettyblocks.factory.entity_factory'

# Contrôleurs API
prestasafe.prettyblocks.controller.zone_controller:
class: PrestaSafe\PrettyBlocks\Controller\Api\ZoneController
arguments:
- '@prestasafe.prettyblocks.service.zone_service'
tags:
- { name: controller.service_arguments }
94 changes: 94 additions & 0 deletions src/Controller/Api/ZoneController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

declare(strict_types=1);

namespace PrestaSafe\PrettyBlocks\Controller\Api;

use PrestaSafe\PrettyBlocks\Service\ZoneService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;

class ZoneController extends AbstractController
{
private $zoneService;

public function __construct(ZoneService $zoneService)
{
$this->zoneService = $zoneService;
}

/**
* Endpoint to list all available zones
*/
public function listZones(): JsonResponse
{
$zones = $this->zoneService->getAllZones();
$data = [];

foreach ($zones as $zone) {
$data[] = [
'id' => $zone->getId(),
'label' => $zone->getLabel(),
// You can add more details if needed
];
}

return new JsonResponse($data);
}

/**
* Endpoint to get a specific zone by ID
*/
public function getZone(string $id): JsonResponse
{
$zone = $this->zoneService->getZoneById($id);

if (!$zone) {
return new JsonResponse(['error' => 'Zone not found'], JsonResponse::HTTP_NOT_FOUND);
}

$data = [
'id' => $zone->getId(),
'label' => $zone->getLabel(),
'blocks' => [],
];

foreach ($zone->getBlocks() as $block) {
$data['blocks'][] = [
'id' => $block->getId(),
'label' => $block->getLabel(),
'fields' => $block->getFields(),
// Include more block details if necessary
];
}

return new JsonResponse($data);
}

/**
* Endpoint to update a specific zone by ID with edited blocks
*/
public function updateZone(Request $request, string $id): JsonResponse
{
$zone = $this->zoneService->getZoneById($id);

if (!$zone) {
return new JsonResponse(['error' => 'Zone not found'], JsonResponse::HTTP_NOT_FOUND);
}

$data = json_decode($request->getContent(), true, 512, JSON_THROW_ON_ERROR);

if (!isset($data['blocks'])) {
return new JsonResponse(['error' => 'Invalid data'], JsonResponse::HTTP_BAD_REQUEST);
}

try {
$this->zoneService->updateZone($zone, $data['blocks']);
} catch (\Exception $e) {
return new JsonResponse(['error' => $e->getMessage()], JsonResponse::HTTP_INTERNAL_SERVER_ERROR);
}

return new JsonResponse(['status' => 'Zone updated successfully']);
}
}
57 changes: 57 additions & 0 deletions src/Entity/Block/AbstractBlock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace PrestaSafe\PrettyBlocks\Entity\Block;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\MappedSuperclass
*/
abstract class AbstractBlock implements BlockInterface
{
/**
* @ORM\Id
* @ORM\Column(type="string")
*/
protected $id;

/**
* @ORM\Column(type="string")
*/
protected $label;

/**
* @ORM\Column(type="json")
*/
protected $fields = [];

public function __construct(string $id, string $label)
{
$this->id = $id;
$this->label = $label;
}

public function getId(): string
{
return $this->id;
}

public function getLabel(): string
{
return $this->label;
}

public function getFields(): array
{
return $this->fields;
}

public function addField($field): void
{
$this->fields[] = $field;
}

abstract public function validate(): bool;
}
18 changes: 18 additions & 0 deletions src/Entity/Block/BlockInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace PrestaSafe\PrettyBlocks\Entity\Block;

interface BlockInterface
{
public function getId(): string;

public function getLabel(): string;

public function getFields(): array;

public function addField($field): void;

public function validate(): bool;
}
20 changes: 20 additions & 0 deletions src/Entity/Block/TextBlock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace PrestaSafe\PrettyBlocks\Entity\Block;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity(repositoryClass="PrestaSafe\PrettyBlocks\Repository\BlockRepository")
* @ORM\Table(name="text_blocks")
*/
class TextBlock extends AbstractBlock
{
public function validate(): bool
{
// Validation spécifique pour le TextBlock
return true;
}
}
53 changes: 53 additions & 0 deletions src/Entity/Component/AbstractComponent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace PrestaSafe\PrettyBlocks\Entity\Component;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\MappedSuperclass
*/
abstract class AbstractComponent implements ComponentInterface
{
/**
* @ORM\Id
* @ORM\Column(type="string")
*/
protected $id;

/**
* @ORM\Column(type="string")
*/
protected $label;

/**
* @ORM\Column(type="string")
*/
protected $type;

public function __construct(string $id, string $label, string $type)
{
$this->id = $id;
$this->label = $label;
$this->type = $type;
}

public function getId(): string
{
return $this->id;
}

public function getLabel(): string
{
return $this->label;
}

public function getType(): string
{
return $this->type;
}

abstract public function validate(): bool;
}
14 changes: 14 additions & 0 deletions src/Entity/Component/ComponentInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace PrestaSafe\PrettyBlocks\Entity\Component;

interface ComponentInterface
{
public function getId(): string;

public function getLabel(): string;

public function getType(): string;

public function validate(): bool;
}
20 changes: 20 additions & 0 deletions src/Entity/Component/TextComponent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace PrestaSafe\PrettyBlocks\Entity\Component;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity(repositoryClass="PrestaSafe\PrettyBlocks\Repository\ComponentRepository")
* @ORM\Table(name="text_components")
*/
class TextComponent extends AbstractComponent
{
public function validate(): bool
{
// Validation spécifique pour le TextComponent
return true;
}
}
40 changes: 40 additions & 0 deletions src/Entity/PrimitiveField/AbstractPrimitiveField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace PrestaSafe\PrettyBlocks\Entity\PrimitiveField;

abstract class AbstractPrimitiveField implements PrimitiveFieldInterface
{
protected string $id;
protected string $label;
protected mixed $value;

public function __construct(string $id, string $label)
{
$this->id = $id;
$this->label = $label;
}

public function getId(): string
{
return $this->id;
}

public function getLabel(): string
{
return $this->label;
}

public function getValue(): mixed
{
return $this->value;
}

public function setValue($value): void
{
$this->value = $value;
}

abstract public function validate(): bool;
}
Loading

0 comments on commit 67439cf

Please sign in to comment.