move controller service logics into a service

This commit is contained in:
team2
2026-02-27 20:05:50 +01:00
parent d01a0d1464
commit f0dfdaf4a8
2 changed files with 90 additions and 46 deletions

View File

@@ -4,12 +4,8 @@ declare(strict_types=1);
namespace App\Controller\Admin;
use App\Entity\Document;
use App\Entity\Tag;
use App\Entity\TagRebuildJob;
use App\Service\TagRebuildJobService;
use App\Tag\TagService;
use Doctrine\ORM\EntityManagerInterface;
use App\Service\Admin\DocumentTagAdminService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
@@ -21,31 +17,15 @@ use Symfony\Component\Routing\Attribute\Route;
final class DocumentTagController extends AbstractController
{
#[Route('/{id}/tags', name: 'admin_document_tags_edit', methods: ['GET'])]
public function edit(
string $id,
EntityManagerInterface $em,
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();
public function edit(string $id, DocumentTagAdminService $svc): Response
{
$data = $svc->getEditData($id);
return $this->render('admin/document_tags/edit.html.twig', [
'document' => $document,
'allTags' => $allTags, // ✅ jetzt vorhanden
'latestJob' => $latestJob,
'document' => $data['document'],
'allTags' => $data['allTags'],
'latestJob' => $data['latestJob'],
'statusRunning' => TagRebuildJob::STATUS_RUNNING,
'statusQueued' => TagRebuildJob::STATUS_QUEUED,
'statusCompleted' => TagRebuildJob::STATUS_COMPLETED,
@@ -54,22 +34,12 @@ final class DocumentTagController extends AbstractController
}
#[Route('/{id}/tags/save', name: 'admin_document_tags_save', methods: ['POST'])]
public function save(
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');
}
public function save(string $id, Request $request, DocumentTagAdminService $svc): RedirectResponse
{
$selected = $request->request->all('tag_ids') ?? [];
try {
$tagService->syncDocumentTags($document, $selected);
$svc->saveTags($id, $selected);
$this->addFlash('success', 'Tags wurden aktualisiert. Rebuild läuft im Hintergrund.');
} catch (\Throwable $e) {
$this->addFlash('danger', $e->getMessage());
@@ -78,13 +48,15 @@ final class DocumentTagController extends AbstractController
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([
'status' => $job?->getStatus(),
'status' => $svc->getLatestRebuildStatus(),
]);
}
}