Config Variables - Model & Easyadmin.

This commit is contained in:
2024-04-27 18:46:35 +02:00
parent 84d9c81afd
commit 7a56158563
10 changed files with 260 additions and 6 deletions
+34
View File
@@ -0,0 +1,34 @@
<?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 Version20240427153125 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('CREATE SEQUENCE config_variable_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
$this->addSql('CREATE TABLE config_variable (id INT NOT NULL, key VARCHAR(32) NOT NULL, value VARCHAR(255) NOT NULL, section VARCHAR(64) DEFAULT NULL, PRIMARY KEY(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('DROP SEQUENCE config_variable_id_seq CASCADE');
$this->addSql('DROP TABLE config_variable');
}
}
@@ -0,0 +1,24 @@
<?php
namespace App\Controller\Admin;
use App\Entity\ConfigVariable;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
class ConfigVariableCrudController extends AbstractCrudController
{
public static function getEntityFqcn(): string
{
return ConfigVariable::class;
}
public function configureFields(string $pageName): iterable
{
yield TextField::new('section');
yield TextField::new('key');
yield TextField::new('value');
}
}
@@ -28,7 +28,7 @@ class RoomCategoryCrudController extends AbstractCrudController
{ {
return [ return [
TextField::new('name'), TextField::new('name'),
TextEditorField::new('description'), TextField::new('description'),
TextField::new('bedType'), TextField::new('bedType'),
IntegerField::new('maxGuestNum'), IntegerField::new('maxGuestNum'),
CollectionField::new('roomCategoryDetails')->useEntryCrudForm(), CollectionField::new('roomCategoryDetails')->useEntryCrudForm(),
@@ -10,6 +10,7 @@ use Symfony\Component\Routing\Annotation\Route;
use App\Entity\RoomCategory; use App\Entity\RoomCategory;
use App\Entity\User; use App\Entity\User;
use App\Entity\ConfigVariable;
class SomniaDashboardController extends AbstractDashboardController class SomniaDashboardController extends AbstractDashboardController
{ {
@@ -33,6 +34,8 @@ class SomniaDashboardController extends AbstractDashboardController
yield MenuItem::linkToCrud('Category', 'fas fa-list', RoomCategory::class); yield MenuItem::linkToCrud('Category', 'fas fa-list', RoomCategory::class);
yield MenuItem::Section('App Management'); yield MenuItem::Section('App Management');
yield MenuItem::linkToCrud('Users', 'fas fa-user', User::class); yield MenuItem::linkToCrud('Config Vars', 'fas fa-gear', ConfigVariable::class);
yield MenuItem::linkToCrud('Users', 'fas fa-user', User::class)
->setPermission("ROLE_SUPERADMIN");
} }
} }
+1 -1
View File
@@ -31,7 +31,7 @@ class UserCrudController extends AbstractCrudController
yield TextField::new ('username'); yield TextField::new ('username');
yield TextField::new ('username')->onlyWhenUpdating()->setDisabled(true); yield TextField::new ('username')->onlyWhenUpdating()->setDisabled(true);
$availableRoles = ['User' => 'ROLE_USER', 'Admin' => 'ROLE_ADMIN', 'Test' => 'ROLE_TEST']; $availableRoles = ['User' => 'ROLE_USER', 'Admin' => 'ROLE_ADMIN', 'Test' => 'ROLE_TEST', 'SuperAdmin' => 'ROLE_SUPERADMIN'];
yield ChoiceField::new ('roles') yield ChoiceField::new ('roles')
->setChoices($availableRoles) ->setChoices($availableRoles)
->allowMultipleChoices(true); ->allowMultipleChoices(true);
@@ -0,0 +1,52 @@
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use App\Repository\ConfigVariableRepository;
class ConfigVariableController extends AbstractController
{
#[Route('/config', name: 'app_config_variable')]
public function getAllVariables(ConfigVariableRepository $repo): Response
{
$varList = $repo->findAllVariables();
return $this->json(['results' => $varList]);
}
#[Route('/config/section', name: 'app_config_sections')]
public function getSections(ConfigVariableRepository $repo): Response
{
$sectionList = $repo->getAllSections();
$returnList = [];
foreach ($sectionList as $section) {
$sectionValue = $section["section"];
$returnList[] = $sectionValue;
}
return $this->json(['results' => $returnList]);
}
#[Route('/config/section/{section}', name: 'app_config_variableBySection')]
public function getVariablesBySection(ConfigVariableRepository $repo, string $section): Response
{
if(\is_null($section) || $section == '') {
return new Response("Invalid section value", 404);
}
$varList = $repo->findAllBySection($section);
return $this->json(['results' => $varList]);
}
#[Route('/config/var/{key}', name: 'app_config_variable')]
public function getByKey(ConfigVariableRepository $repo, string $key): Response
{
$var = $repo->findVariableByKey($key);
return $this->json($var);
}
}
+2 -2
View File
@@ -18,7 +18,7 @@ class RoomCategoryController extends AbstractController
{ {
#[Route('/room/category', name: 'app_room_AllCategories')] #[Route('/room/category', name: 'app_room_AllCategories')]
public function getAll(RoomCategoryRepository $catRepo, SerializerInterface $serializer): Response public function getAll(RoomCategoryRepository $catRepo): Response
{ {
$catList = $catRepo->findAll(); $catList = $catRepo->findAll();
@@ -26,7 +26,7 @@ class RoomCategoryController extends AbstractController
} }
#[Route('/room/category/{id}', name: 'app_room_category')] #[Route('/room/category/{id}', name: 'app_room_category')]
public function getById(RoomCategory $category, SerializerInterface $serializer): Response public function getById(RoomCategory $category): Response
{ {
return $this->json($category); return $this->json($category);
} }
+65
View File
@@ -0,0 +1,65 @@
<?php
namespace App\Entity;
use App\Repository\ConfigVariableRepository;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: ConfigVariableRepository::class)]
class ConfigVariable
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 32, unique: true)]
private ?string $key = null;
#[ORM\Column(length: 255)]
private ?string $value = null;
#[ORM\Column(length: 64, nullable: true)]
private ?string $section = null;
public function getId(): ?int
{
return $this->id;
}
public function getKey(): ?string
{
return $this->key;
}
public function setKey(string $key): static
{
$this->key = $key;
return $this;
}
public function getValue(): ?string
{
return $this->value;
}
public function setValue(string $value): static
{
$this->value = $value;
return $this;
}
public function getSection(): ?string
{
return $this->section;
}
public function setSection(?string $section): static
{
$this->section = $section;
return $this;
}
}
+1 -1
View File
@@ -104,7 +104,7 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
} }
public function getPlainPassword(): string public function getPlainPassword(): ?string
{ {
return $this->plainPassword; return $this->plainPassword;
} }
@@ -0,0 +1,76 @@
<?php
namespace App\Repository;
use App\Entity\ConfigVariable;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<ConfigVariable>
*
* @method ConfigVariable|null find($id, $lockMode = null, $lockVersion = null)
* @method ConfigVariable|null findOneBy(array $criteria, array $orderBy = null)
* @method ConfigVariable[] findAll()
* @method ConfigVariable[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class ConfigVariableRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, ConfigVariable::class);
}
/**
* Get all config variables for API.
*/
public function findAllVariables(): array
{
return $this->createQueryBuilder('c')
->select('c.key, c.value, c.section')
->getQuery()
->getResult();
}
/**
* @param string Config section.
* @return ConfigVariable[] Returns an array of ConfigVariable objects
*/
public function findAllBySection(string $section): array
{
return $this->createQueryBuilder('c')
->select('c.key, c.value')
->andWhere('c.section = :val')
->setParameter('val', $section)
->getQuery()
->getResult();
}
/**
* Get Config Variable by it's key.
* @param string Variable Key
* @return ConfigVariable|null Config Variable.
*/
public function findVariableByKey($varKey)
{
return $this->createQueryBuilder('c')
->select('c.key, c.value, c.section')
->andWhere('c.key = :val')
->setParameter('val', $varKey)
->getQuery()
->getOneOrNullResult();
}
/**
* Get all section names that contains config variables.
* @return string[] Returns an array of strings.
*/
public function getAllSections(): array
{
return $this->createQueryBuilder('c')
->select('c.section')
->distinct()
->getQuery()
->getResult();
}
}