pp-planer/app/Http/Controllers/ProFileController.php
Thorsten Bus bdbf0c65e3 refactor(php): rename SongGroup references throughout controllers/services/tests
Replace all SongGroup/SongArrangementGroup model usages with Label/SongArrangementLabel
after the schema migration to global labels. Updates 12 app files and 11 test files:

- SongService: createDefaultGroups now finds-or-creates global Labels by name
- ArrangementController: validates label_id (labels are global, no song-ownership)
- ProImportService: imports groups as Labels (firstOrCreate by name); does not
  overwrite existing label colors per spec
- ProExportService/SongPdfController/TranslationService/etc: traverse via
  arrangements -> arrangementLabels -> label -> songSlides chain
- All test factories and assertions adapted to label-based schema
2026-05-03 22:55:02 +02:00

72 lines
2.3 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Song;
use App\Services\ProExportService;
use App\Services\ProImportService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
class ProFileController extends Controller
{
public function importPro(Request $request): JsonResponse
{
$request->validate([
'file' => ['required', 'file', 'max:51200'],
]);
$file = $request->file('file');
$extension = strtolower($file->getClientOriginalExtension());
if (! in_array($extension, ['pro', 'zip'])) {
return response()->json([
'message' => 'Nur .pro und .zip Dateien sind erlaubt.',
], 422);
}
try {
$service = new ProImportService;
$songs = $service->import($file);
return response()->json([
'message' => count($songs) === 1
? "Song \"{$songs[0]->title}\" erfolgreich importiert."
: count($songs).' Songs erfolgreich importiert.',
'songs' => collect($songs)->map(fn (Song $song) => [
'id' => $song->id,
'title' => $song->title,
'ccli_id' => $song->ccli_id,
]),
]);
} catch (\InvalidArgumentException $e) {
return response()->json(['message' => $e->getMessage()], 422);
} catch (\RuntimeException $e) {
return response()->json(['message' => $e->getMessage()], 422);
}
}
public function downloadPro(Song $song): BinaryFileResponse
{
if ($this->countSongLabels($song) === 0) {
abort(422, 'Song hat keine Gruppen oder Slides zum Exportieren.');
}
$exportService = new ProExportService;
$tempPath = $exportService->generateProFile($song);
$filename = preg_replace('/[^a-zA-Z0-9äöüÄÖÜß\-_ ]/', '', $song->title).'.pro';
return response()->download($tempPath, $filename)->deleteFileAfterSend(true);
}
private function countSongLabels(Song $song): int
{
return $song->arrangements()
->withCount('arrangementLabels')
->get()
->sum('arrangement_labels_count');
}
}