From 3a1b30f1eac5cee200ff795737a97c44087c9a66 Mon Sep 17 00:00:00 2001 From: team2 Date: Fri, 27 Feb 2026 20:21:36 +0100 Subject: [PATCH] move controller service logics into a service --- .../Admin/SystemPromptController.php | 72 +++++++---------- .../Admin/SystemPromptAdminService.php | 80 +++++++++++++++++++ 2 files changed, 107 insertions(+), 45 deletions(-) create mode 100644 src/Service/Admin/SystemPromptAdminService.php diff --git a/src/Controller/Admin/SystemPromptController.php b/src/Controller/Admin/SystemPromptController.php index 019ca4b..ff9a13b 100644 --- a/src/Controller/Admin/SystemPromptController.php +++ b/src/Controller/Admin/SystemPromptController.php @@ -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'); } -} +} \ No newline at end of file diff --git a/src/Service/Admin/SystemPromptAdminService.php b/src/Service/Admin/SystemPromptAdminService.php new file mode 100644 index 0000000..3cc563b --- /dev/null +++ b/src/Service/Admin/SystemPromptAdminService.php @@ -0,0 +1,80 @@ + $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(); + } +} \ No newline at end of file