Files
bandtoolv2/app/Http/Controllers/BandApplicationController.php
T
2025-10-28 21:51:43 +01:00

96 lines
3.8 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Band;
use App\Models\Bewerbung;
use App\Models\BandApplication;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
class BandApplicationController extends Controller
{
/**
* Speichert eine Bewerbung (Band-Stammdaten + Bewerbung zur Runde).
* Route: POST /bewerbung/{bewerbung:slug}/apply (name: band.apply)
*/
public function store(Request $request, Bewerbung $bewerbung)
{
// Validierung
$data = $request->validate([
// Band-Stammdaten
'band.name' => ['required','string','max:255'],
'band.email' => ['required','email','max:255'],
'band.phone' => ['nullable','string','max:100'],
'band.city' => ['nullable','string','max:255'],
'band.website' => ['nullable','url','max:255'],
'band.spotify' => ['nullable','url','max:255'],
'band.instagram' => ['nullable','url','max:255'],
'band.youtube' => ['nullable','url','max:255'],
'band.about' => ['nullable','string','max:5000'],
// Dateien optional
'band.tech_rider' => ['nullable','file','mimes:pdf','max:8192'],
'band.stage_plan' => ['nullable','file','mimes:pdf','max:8192'],
// Bewerbung
'message' => ['nullable','string','max:5000'],
'demo_link' => ['nullable','url','max:255'],
'privacy_ok' => ['accepted'], // muss angehakt sein
]);
$application = DB::transaction(function() use ($data, $request, $bewerbung) {
// 1) Band per E-Mail finden/neu anlegen
$band = Band::firstOrNew(['email' => $data['band']['email']]);
$band->fill([
'name' => $data['band']['name'],
'phone' => $data['band']['phone'] ?? null,
'city' => $data['band']['city'] ?? null,
'website' => $data['band']['website'] ?? null,
'spotify' => $data['band']['spotify'] ?? null,
'instagram' => $data['band']['instagram'] ?? null,
'youtube' => $data['band']['youtube'] ?? null,
'about' => $data['band']['about'] ?? null,
]);
if ($request->hasFile('band.tech_rider')) {
$band->tech_rider_path = $request->file('band.tech_rider')->store('bands/tech', 'public');
}
if ($request->hasFile('band.stage_plan')) {
$band->stage_plan_path = $request->file('band.stage_plan')->store('bands/stage', 'public');
}
$band->save();
// 2) Bewerbungs-Duplikate verhindern
$existing = BandApplication::where('band_id', $band->id)
->where('bewerbung_id', $bewerbung->id)
->first();
if ($existing) {
// Optional: Nachricht/Demolink aktualisieren
$existing->update([
'message' => $data['message'] ?? $existing->message,
'demo_link' => $data['demo_link'] ?? $existing->demo_link,
]);
return $existing;
}
// 3) Bewerbung anlegen
return BandApplication::create([
'band_id' => $band->id,
'bewerbung_id' => $bewerbung->id,
'status' => 'submitted',
'message' => $data['message'] ?? null,
'demo_link' => $data['demo_link'] ?? null,
'privacy_ok' => true,
]);
});
return redirect()
->route('bewerbung.show', $bewerbung) // oder ['bewerbung' => $bewerbung->slug]
->with('success', 'Danke! Eure Bewerbung ist eingegangen.');
}
}