pull/30/head
parent
d4d2bad691
commit
0d36ca39dd
|
@ -0,0 +1,31 @@
|
|||
<?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 Version20250506094654 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 pxe_template ADD is_default TINYINT(1) DEFAULT NULL');
|
||||
}
|
||||
|
||||
public function down(Schema $schema): void
|
||||
{
|
||||
// this down() migration is auto-generated, please modify it to your needs
|
||||
$this->addSql('ALTER TABLE pxe_template DROP is_default');
|
||||
}
|
||||
}
|
|
@ -5,9 +5,12 @@ namespace App\Dto\Input;
|
|||
use ApiPlatform\Metadata\ApiProperty;
|
||||
use App\Dto\Output\RemoteCalendarOutput;
|
||||
use App\Entity\RemoteCalendarRule;
|
||||
use App\Validator\Constraints\RemoteCalendarRuleUnique;
|
||||
use Symfony\Component\Serializer\Annotation\Groups;
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
|
||||
|
||||
#[RemoteCalendarRuleUnique]
|
||||
final class RemoteCalendarRuleInput
|
||||
{
|
||||
#[Assert\NotNull()]
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
|
||||
namespace App\Validator\Constraints;
|
||||
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
|
||||
#[\Attribute]
|
||||
class RemoteCalendarRuleUnique extends Constraint
|
||||
{
|
||||
public string $message;
|
||||
|
||||
public function __construct(mixed $options = null, ?array $groups = null, mixed $payload = null)
|
||||
{
|
||||
parent::__construct($options, $groups, $payload);
|
||||
|
||||
$this->message = 'Ya hay una regla de calendario marcada como no disponible o reservada.';
|
||||
}
|
||||
|
||||
public function getTargets(): string
|
||||
{
|
||||
return self::CLASS_CONSTRAINT;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
<?php
|
||||
|
||||
namespace App\Validator\Constraints;
|
||||
|
||||
use App\Dto\Input\RemoteCalendarRuleInput;
|
||||
use App\Entity\RemoteCalendarRule;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidator;
|
||||
|
||||
class RemoteCalendarRuleUniqueValidator extends ConstraintValidator
|
||||
{
|
||||
public function __construct(
|
||||
private readonly EntityManagerInterface $entityManager,
|
||||
private readonly RequestStack $requestStack
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
public function validate(mixed $value, Constraint $constraint): void
|
||||
{
|
||||
$request = $this->requestStack->getCurrentRequest();
|
||||
|
||||
if (!$value instanceof RemoteCalendarRuleInput) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($value->isRemoteAvailable) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($request && $request->getMethod() !== 'POST') {
|
||||
return;
|
||||
}
|
||||
|
||||
$isRemoteAvailable = $this->entityManager->getRepository(RemoteCalendarRule::class)
|
||||
->findOneBy([
|
||||
'remoteCalendar' => $value->remoteCalendar->getEntity(),
|
||||
'isRemoteAvailable' => false,
|
||||
]);
|
||||
|
||||
if ($isRemoteAvailable) {
|
||||
$this->context->buildViolation($constraint->message)->addViolation();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue