move controller service logics into a service
This commit is contained in:
@@ -5,8 +5,7 @@ declare(strict_types=1);
|
||||
namespace App\Controller\Admin;
|
||||
|
||||
use App\Entity\SystemPrompt;
|
||||
use App\Repository\SystemPromptRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use App\Service\Admin\SystemPromptAdminService;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
@@ -16,79 +15,62 @@ use Symfony\Component\Security\Http\Attribute\IsGranted;
|
||||
#[IsGranted('ROLE_SUPER_ADMIN')]
|
||||
final class SystemPromptController extends AbstractController
|
||||
{
|
||||
#[Route('/admin/system/prompt', name: 'admin_system_prompt')]
|
||||
#[Route('/admin/system/prompt', name: 'admin_system_prompt', methods: ['GET', 'POST'])]
|
||||
public function index(
|
||||
Request $request,
|
||||
SystemPromptRepository $repo,
|
||||
EntityManagerInterface $em
|
||||
SystemPromptAdminService $svc
|
||||
): Response {
|
||||
|
||||
if ($request->isMethod('POST')) {
|
||||
$content = trim($request->request->get('content', ''));
|
||||
$comment = trim($request->request->get('comment', ''));
|
||||
|
||||
if ($content !== '') {
|
||||
|
||||
$active = $repo->findActive();
|
||||
if ($active) {
|
||||
$active->deactivate();
|
||||
}
|
||||
|
||||
$new = new SystemPrompt(
|
||||
version: $repo->getNextVersion(),
|
||||
content: $content,
|
||||
comment: $comment ?: null,
|
||||
active: true
|
||||
try {
|
||||
$svc->create(
|
||||
$request->request->get('content', ''),
|
||||
$request->request->get('comment')
|
||||
);
|
||||
|
||||
$em->persist($new);
|
||||
$em->flush();
|
||||
|
||||
$this->addFlash('success', 'Neue Version gespeichert.');
|
||||
return $this->redirectToRoute('admin_system_prompt');
|
||||
|
||||
} catch (\Throwable $e) {
|
||||
$this->addFlash('danger', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
return $this->render('admin/system/prompt.html.twig', [
|
||||
'active' => $repo->findActive(),
|
||||
'all' => $repo->findBy([], ['version' => 'DESC']),
|
||||
]);
|
||||
return $this->render(
|
||||
'admin/system/prompt.html.twig',
|
||||
$svc->getIndexData()
|
||||
);
|
||||
}
|
||||
|
||||
#[Route('/admin/system/prompt/{id}/activate', name: 'admin_system_prompt_activate', methods: ['POST'])]
|
||||
public function activate(
|
||||
SystemPrompt $prompt,
|
||||
SystemPromptRepository $repo,
|
||||
EntityManagerInterface $em
|
||||
SystemPromptAdminService $svc
|
||||
): Response {
|
||||
|
||||
foreach ($repo->findBy(['active' => true]) as $p) {
|
||||
$p->deactivate();
|
||||
try {
|
||||
$svc->activate($prompt);
|
||||
$this->addFlash('success', 'Version aktiviert.');
|
||||
} catch (\Throwable $e) {
|
||||
$this->addFlash('danger', $e->getMessage());
|
||||
}
|
||||
|
||||
$prompt->activate();
|
||||
|
||||
$em->flush();
|
||||
|
||||
$this->addFlash('success', 'Version aktiviert.');
|
||||
return $this->redirectToRoute('admin_system_prompt');
|
||||
}
|
||||
|
||||
#[Route('/admin/system/prompt/{id}/delete', name: 'admin_system_prompt_delete', methods: ['POST'])]
|
||||
public function delete(
|
||||
SystemPrompt $prompt,
|
||||
EntityManagerInterface $em
|
||||
SystemPromptAdminService $svc
|
||||
): Response {
|
||||
|
||||
if ($prompt->isActive()) {
|
||||
$this->addFlash('danger', 'Aktive Version kann nicht gelöscht werden.');
|
||||
return $this->redirectToRoute('admin_system_prompt');
|
||||
try {
|
||||
$svc->delete($prompt);
|
||||
$this->addFlash('success', 'Version gelöscht.');
|
||||
} catch (\Throwable $e) {
|
||||
$this->addFlash('danger', $e->getMessage());
|
||||
}
|
||||
|
||||
$em->remove($prompt);
|
||||
$em->flush();
|
||||
|
||||
$this->addFlash('success', 'Version gelöscht.');
|
||||
return $this->redirectToRoute('admin_system_prompt');
|
||||
}
|
||||
}
|
||||
}
|
||||
80
src/Service/Admin/SystemPromptAdminService.php
Normal file
80
src/Service/Admin/SystemPromptAdminService.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Admin;
|
||||
|
||||
use App\Entity\SystemPrompt;
|
||||
use App\Repository\SystemPromptRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
final readonly class SystemPromptAdminService
|
||||
{
|
||||
public function __construct(
|
||||
private SystemPromptRepository $repo,
|
||||
private EntityManagerInterface $em,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @return array{
|
||||
* active:?SystemPrompt,
|
||||
* all:array
|
||||
* }
|
||||
*/
|
||||
public function getIndexData(): array
|
||||
{
|
||||
return [
|
||||
'active' => $this->repo->findActive(),
|
||||
'all' => $this->repo->findBy([], ['version' => 'DESC']),
|
||||
];
|
||||
}
|
||||
|
||||
public function create(string $content, ?string $comment): SystemPrompt
|
||||
{
|
||||
$content = trim($content);
|
||||
|
||||
if ($content === '') {
|
||||
throw new \InvalidArgumentException('System Prompt darf nicht leer sein.');
|
||||
}
|
||||
|
||||
// aktive Version deaktivieren
|
||||
$active = $this->repo->findActive();
|
||||
if ($active instanceof SystemPrompt) {
|
||||
$active->deactivate();
|
||||
}
|
||||
|
||||
$new = new SystemPrompt(
|
||||
version: $this->repo->getNextVersion(),
|
||||
content: $content,
|
||||
comment: $comment ? trim($comment) : null,
|
||||
active: true
|
||||
);
|
||||
|
||||
$this->em->persist($new);
|
||||
$this->em->flush();
|
||||
|
||||
return $new;
|
||||
}
|
||||
|
||||
public function activate(SystemPrompt $prompt): void
|
||||
{
|
||||
// alle aktiven deaktivieren (sicherheitsrobust)
|
||||
foreach ($this->repo->findBy(['active' => true]) as $p) {
|
||||
$p->deactivate();
|
||||
}
|
||||
|
||||
$prompt->activate();
|
||||
|
||||
$this->em->flush();
|
||||
}
|
||||
|
||||
public function delete(SystemPrompt $prompt): void
|
||||
{
|
||||
if ($prompt->isActive()) {
|
||||
throw new \RuntimeException('Aktive Version kann nicht gelöscht werden.');
|
||||
}
|
||||
|
||||
$this->em->remove($prompt);
|
||||
$this->em->flush();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user