move controller service logics into a service
This commit is contained in:
@@ -4,12 +4,8 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace App\Controller\Admin;
|
namespace App\Controller\Admin;
|
||||||
|
|
||||||
use App\Entity\Document;
|
|
||||||
use App\Entity\Tag;
|
|
||||||
use App\Entity\TagRebuildJob;
|
use App\Entity\TagRebuildJob;
|
||||||
use App\Service\TagRebuildJobService;
|
use App\Service\Admin\DocumentTagAdminService;
|
||||||
use App\Tag\TagService;
|
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||||
@@ -21,31 +17,15 @@ use Symfony\Component\Routing\Attribute\Route;
|
|||||||
final class DocumentTagController extends AbstractController
|
final class DocumentTagController extends AbstractController
|
||||||
{
|
{
|
||||||
#[Route('/{id}/tags', name: 'admin_document_tags_edit', methods: ['GET'])]
|
#[Route('/{id}/tags', name: 'admin_document_tags_edit', methods: ['GET'])]
|
||||||
public function edit(
|
public function edit(string $id, DocumentTagAdminService $svc): Response
|
||||||
string $id,
|
{
|
||||||
EntityManagerInterface $em,
|
$data = $svc->getEditData($id);
|
||||||
TagRebuildJobService $jobs
|
|
||||||
): Response {
|
|
||||||
|
|
||||||
$document = $em->getRepository(Document::class)->find($id);
|
|
||||||
if (!$document instanceof Document) {
|
|
||||||
throw $this->createNotFoundException('Document not found');
|
|
||||||
}
|
|
||||||
|
|
||||||
// 🔹 Alle verfügbaren Tags laden (fehlte!)
|
|
||||||
$allTags = $em->createQueryBuilder()
|
|
||||||
->select('t')
|
|
||||||
->from(Tag::class, 't')
|
|
||||||
->orderBy('t.label', 'ASC')
|
|
||||||
->getQuery()
|
|
||||||
->getResult();
|
|
||||||
|
|
||||||
$latestJob = $jobs->getLatestJob();
|
|
||||||
|
|
||||||
return $this->render('admin/document_tags/edit.html.twig', [
|
return $this->render('admin/document_tags/edit.html.twig', [
|
||||||
'document' => $document,
|
'document' => $data['document'],
|
||||||
'allTags' => $allTags, // ✅ jetzt vorhanden
|
'allTags' => $data['allTags'],
|
||||||
'latestJob' => $latestJob,
|
'latestJob' => $data['latestJob'],
|
||||||
|
|
||||||
'statusRunning' => TagRebuildJob::STATUS_RUNNING,
|
'statusRunning' => TagRebuildJob::STATUS_RUNNING,
|
||||||
'statusQueued' => TagRebuildJob::STATUS_QUEUED,
|
'statusQueued' => TagRebuildJob::STATUS_QUEUED,
|
||||||
'statusCompleted' => TagRebuildJob::STATUS_COMPLETED,
|
'statusCompleted' => TagRebuildJob::STATUS_COMPLETED,
|
||||||
@@ -54,22 +34,12 @@ final class DocumentTagController extends AbstractController
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[Route('/{id}/tags/save', name: 'admin_document_tags_save', methods: ['POST'])]
|
#[Route('/{id}/tags/save', name: 'admin_document_tags_save', methods: ['POST'])]
|
||||||
public function save(
|
public function save(string $id, Request $request, DocumentTagAdminService $svc): RedirectResponse
|
||||||
string $id,
|
{
|
||||||
Request $request,
|
|
||||||
EntityManagerInterface $em,
|
|
||||||
TagService $tagService
|
|
||||||
): RedirectResponse {
|
|
||||||
|
|
||||||
$document = $em->getRepository(Document::class)->find($id);
|
|
||||||
if (!$document instanceof Document) {
|
|
||||||
return $this->redirectToRoute('admin_documents');
|
|
||||||
}
|
|
||||||
|
|
||||||
$selected = $request->request->all('tag_ids') ?? [];
|
$selected = $request->request->all('tag_ids') ?? [];
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$tagService->syncDocumentTags($document, $selected);
|
$svc->saveTags($id, $selected);
|
||||||
$this->addFlash('success', 'Tags wurden aktualisiert. Rebuild läuft im Hintergrund.');
|
$this->addFlash('success', 'Tags wurden aktualisiert. Rebuild läuft im Hintergrund.');
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
$this->addFlash('danger', $e->getMessage());
|
$this->addFlash('danger', $e->getMessage());
|
||||||
@@ -78,13 +48,15 @@ final class DocumentTagController extends AbstractController
|
|||||||
return $this->redirectToRoute('admin_document_tags_edit', ['id' => $id]);
|
return $this->redirectToRoute('admin_document_tags_edit', ['id' => $id]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[Route('/admin/tags/status', name: 'admin_tags_status', methods: ['GET'])]
|
/**
|
||||||
public function status(TagRebuildJobService $jobs): JsonResponse
|
* Wichtig: Ohne extra "admin/" im Pfad, weil Prefix schon /admin/documents ist.
|
||||||
|
* Ergebnis: /admin/documents/tags/status
|
||||||
|
*/
|
||||||
|
#[Route('/tags/status', name: 'admin_tags_status', methods: ['GET'])]
|
||||||
|
public function status(DocumentTagAdminService $svc): JsonResponse
|
||||||
{
|
{
|
||||||
$job = $jobs->getLatestJob();
|
|
||||||
|
|
||||||
return $this->json([
|
return $this->json([
|
||||||
'status' => $job?->getStatus(),
|
'status' => $svc->getLatestRebuildStatus(),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
72
src/Service/Admin/DocumentTagAdminService.php
Normal file
72
src/Service/Admin/DocumentTagAdminService.php
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Service\Admin;
|
||||||
|
|
||||||
|
use App\Entity\Document;
|
||||||
|
use App\Entity\Tag;
|
||||||
|
use App\Service\TagRebuildJobService;
|
||||||
|
use App\Tag\TagService;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
|
||||||
|
final class DocumentTagAdminService
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private readonly EntityManagerInterface $em,
|
||||||
|
private readonly TagService $tagService,
|
||||||
|
private readonly TagRebuildJobService $jobs,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array{
|
||||||
|
* document: Document,
|
||||||
|
* allTags: list<Tag>,
|
||||||
|
* latestJob: mixed
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
public function getEditData(string $documentId): array
|
||||||
|
{
|
||||||
|
$document = $this->em->getRepository(Document::class)->find($documentId);
|
||||||
|
if (!$document instanceof Document) {
|
||||||
|
throw new \RuntimeException('Document not found');
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @var list<Tag> $allTags */
|
||||||
|
$allTags = $this->em->createQueryBuilder()
|
||||||
|
->select('t')
|
||||||
|
->from(Tag::class, 't')
|
||||||
|
->orderBy('t.label', 'ASC')
|
||||||
|
->getQuery()
|
||||||
|
->getResult();
|
||||||
|
|
||||||
|
$latestJob = $this->jobs->getLatestJob();
|
||||||
|
|
||||||
|
return [
|
||||||
|
'document' => $document,
|
||||||
|
'allTags' => $allTags,
|
||||||
|
'latestJob' => $latestJob,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Speichert die Tag-Auswahl für ein Dokument (inkl. Sync-Logik).
|
||||||
|
*/
|
||||||
|
public function saveTags(string $documentId, array $selectedTagIds): void
|
||||||
|
{
|
||||||
|
$document = $this->em->getRepository(Document::class)->find($documentId);
|
||||||
|
if (!$document instanceof Document) {
|
||||||
|
throw new \RuntimeException('Document not found');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delegation an deine Domain-Logik (bleibt dort, wo sie hingehört)
|
||||||
|
$this->tagService->syncDocumentTags($document, $selectedTagIds);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getLatestRebuildStatus(): ?string
|
||||||
|
{
|
||||||
|
$job = $this->jobs->getLatestJob();
|
||||||
|
|
||||||
|
return $job?->getStatus();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user