move controller service logics into a service
This commit is contained in:
@@ -5,8 +5,7 @@ declare(strict_types=1);
|
|||||||
namespace App\Controller\Admin;
|
namespace App\Controller\Admin;
|
||||||
|
|
||||||
use App\Entity\SystemPrompt;
|
use App\Entity\SystemPrompt;
|
||||||
use App\Repository\SystemPromptRepository;
|
use App\Service\Admin\SystemPromptAdminService;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
@@ -16,79 +15,62 @@ use Symfony\Component\Security\Http\Attribute\IsGranted;
|
|||||||
#[IsGranted('ROLE_SUPER_ADMIN')]
|
#[IsGranted('ROLE_SUPER_ADMIN')]
|
||||||
final class SystemPromptController extends AbstractController
|
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(
|
public function index(
|
||||||
Request $request,
|
Request $request,
|
||||||
SystemPromptRepository $repo,
|
SystemPromptAdminService $svc
|
||||||
EntityManagerInterface $em
|
|
||||||
): Response {
|
): Response {
|
||||||
|
|
||||||
if ($request->isMethod('POST')) {
|
if ($request->isMethod('POST')) {
|
||||||
$content = trim($request->request->get('content', ''));
|
try {
|
||||||
$comment = trim($request->request->get('comment', ''));
|
$svc->create(
|
||||||
|
$request->request->get('content', ''),
|
||||||
if ($content !== '') {
|
$request->request->get('comment')
|
||||||
|
|
||||||
$active = $repo->findActive();
|
|
||||||
if ($active) {
|
|
||||||
$active->deactivate();
|
|
||||||
}
|
|
||||||
|
|
||||||
$new = new SystemPrompt(
|
|
||||||
version: $repo->getNextVersion(),
|
|
||||||
content: $content,
|
|
||||||
comment: $comment ?: null,
|
|
||||||
active: true
|
|
||||||
);
|
);
|
||||||
|
|
||||||
$em->persist($new);
|
|
||||||
$em->flush();
|
|
||||||
|
|
||||||
$this->addFlash('success', 'Neue Version gespeichert.');
|
$this->addFlash('success', 'Neue Version gespeichert.');
|
||||||
return $this->redirectToRoute('admin_system_prompt');
|
return $this->redirectToRoute('admin_system_prompt');
|
||||||
|
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
$this->addFlash('danger', $e->getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->render('admin/system/prompt.html.twig', [
|
return $this->render(
|
||||||
'active' => $repo->findActive(),
|
'admin/system/prompt.html.twig',
|
||||||
'all' => $repo->findBy([], ['version' => 'DESC']),
|
$svc->getIndexData()
|
||||||
]);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[Route('/admin/system/prompt/{id}/activate', name: 'admin_system_prompt_activate', methods: ['POST'])]
|
#[Route('/admin/system/prompt/{id}/activate', name: 'admin_system_prompt_activate', methods: ['POST'])]
|
||||||
public function activate(
|
public function activate(
|
||||||
SystemPrompt $prompt,
|
SystemPrompt $prompt,
|
||||||
SystemPromptRepository $repo,
|
SystemPromptAdminService $svc
|
||||||
EntityManagerInterface $em
|
|
||||||
): Response {
|
): Response {
|
||||||
|
|
||||||
foreach ($repo->findBy(['active' => true]) as $p) {
|
try {
|
||||||
$p->deactivate();
|
$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');
|
return $this->redirectToRoute('admin_system_prompt');
|
||||||
}
|
}
|
||||||
|
|
||||||
#[Route('/admin/system/prompt/{id}/delete', name: 'admin_system_prompt_delete', methods: ['POST'])]
|
#[Route('/admin/system/prompt/{id}/delete', name: 'admin_system_prompt_delete', methods: ['POST'])]
|
||||||
public function delete(
|
public function delete(
|
||||||
SystemPrompt $prompt,
|
SystemPrompt $prompt,
|
||||||
EntityManagerInterface $em
|
SystemPromptAdminService $svc
|
||||||
): Response {
|
): Response {
|
||||||
|
|
||||||
if ($prompt->isActive()) {
|
try {
|
||||||
$this->addFlash('danger', 'Aktive Version kann nicht gelöscht werden.');
|
$svc->delete($prompt);
|
||||||
return $this->redirectToRoute('admin_system_prompt');
|
|
||||||
}
|
|
||||||
|
|
||||||
$em->remove($prompt);
|
|
||||||
$em->flush();
|
|
||||||
|
|
||||||
$this->addFlash('success', 'Version gelöscht.');
|
$this->addFlash('success', 'Version gelöscht.');
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
$this->addFlash('danger', $e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
return $this->redirectToRoute('admin_system_prompt');
|
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