diff --git a/src/Command/CreateRoleCommand.php b/src/Command/CreateRoleCommand.php new file mode 100644 index 0000000..6155951 --- /dev/null +++ b/src/Command/CreateRoleCommand.php @@ -0,0 +1,64 @@ +entityManager = $entityManager; + } + + protected function configure(): void + { + $this + ->addArgument('name', InputArgument::REQUIRED, 'The name of the role'); // role name required + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $roleName = trim($input->getArgument('name')); + $roleName = strtoupper($roleName); // Normalize to uppercase + + // Ensure not empty + if ($roleName === '') { + $output->writeln('The role name cannot be empty'); + return Command::FAILURE; + } + + // Check if role already exists + $existing = $this->entityManager->getRepository(Roles::class) + ->findOneBy(['name' => $roleName]); + + if ($existing) { + $output->writeln("Role '{$roleName}' already exists."); + return Command::SUCCESS; // not failure, just redundant + } + + // Create and persist new role + $role = new Roles(); + $role->setName($roleName); + + $this->entityManager->persist($role); + $this->entityManager->flush(); + + $output->writeln("Role '{$roleName}' created successfully!"); + + return Command::SUCCESS; + } +} \ No newline at end of file diff --git a/src/Entity/Roles.php b/src/Entity/Roles.php index 47759b7..9147c55 100644 --- a/src/Entity/Roles.php +++ b/src/Entity/Roles.php @@ -19,6 +19,10 @@ class Roles #[ORM\Column(options: ['default' => 'CURRENT_TIMESTAMP'])] private ?\DateTimeImmutable $createdAt = null; + public function __construct() + { + $this->createdAt = new \DateTimeImmutable(); + } public function getId(): ?int { return $this->id;