pp-planer/app/Http/Controllers/TranslationController.php
Thorsten Bus 04d271f96a style: apply Laravel Pint formatting across codebase
Auto-formatted by Laravel Pint (default Laravel preset): string
concatenation spacing, anonymous class brace placement, constructor
body shorthand, import ordering, and assertion indentation.
2026-03-02 23:02:03 +01:00

121 lines
3.3 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Song;
use App\Services\TranslationService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;
class TranslationController extends Controller
{
public function __construct(
private readonly TranslationService $translationService,
) {}
public function page(Song $song): Response
{
$song->load([
'groups' => fn ($query) => $query
->orderBy('order')
->with([
'slides' => fn ($slideQuery) => $slideQuery->orderBy('order'),
]),
]);
return Inertia::render('Songs/Translate', [
'song' => [
'id' => $song->id,
'title' => $song->title,
'ccli_id' => $song->ccli_id,
'has_translation' => $song->has_translation,
'groups' => $song->groups->map(fn ($group) => [
'id' => $group->id,
'name' => $group->name,
'color' => $group->color,
'order' => $group->order,
'slides' => $group->slides->map(fn ($slide) => [
'id' => $slide->id,
'order' => $slide->order,
'text_content' => $slide->text_content,
'text_content_translated' => $slide->text_content_translated,
])->values(),
])->values(),
],
]);
}
/**
* URL abrufen und Text zum Prüfen zurückgeben.
*
* Der Text wird NICHT automatisch gespeichert — der Benutzer
* prüft ihn zuerst und importiert dann explizit.
*/
public function fetchUrl(Request $request): JsonResponse
{
$request->validate([
'url' => ['required', 'url'],
]);
$text = $this->translationService->fetchFromUrl($request->input('url'));
if ($text === null) {
return response()->json([
'message' => 'Konnte Text nicht abrufen',
], 422);
}
return response()->json([
'text' => $text,
]);
}
/**
* Übersetzungstext für einen Song importieren.
*
* Verteilt den Text zeilenweise auf die Slides des Songs.
*/
public function import(int $songId, Request $request): JsonResponse
{
$song = Song::find($songId);
if (! $song) {
return response()->json([
'message' => 'Song nicht gefunden',
], 404);
}
$request->validate([
'text' => ['required', 'string'],
]);
$this->translationService->importTranslation($song, $request->input('text'));
return response()->json([
'message' => 'Übersetzung erfolgreich importiert',
]);
}
/**
* Übersetzung eines Songs komplett entfernen.
*/
public function destroy(int $songId): JsonResponse
{
$song = Song::find($songId);
if (! $song) {
return response()->json([
'message' => 'Song nicht gefunden',
], 404);
}
$this->translationService->removeTranslation($song);
return response()->json([
'message' => 'Übersetzung entfernt',
]);
}
}