adrema/app/Payment/SubscriptionController.php

101 lines
3.1 KiB
PHP
Raw Permalink Normal View History

2021-07-04 01:44:41 +02:00
<?php
namespace App\Payment;
2022-01-02 12:32:57 +01:00
use App\Fee;
2021-07-04 01:44:41 +02:00
use App\Http\Controllers\Controller;
2022-02-12 01:16:37 +01:00
use Illuminate\Http\RedirectResponse;
2021-07-04 01:44:41 +02:00
use Illuminate\Http\Request;
2022-02-12 01:16:37 +01:00
use Inertia\Response;
2021-07-04 01:44:41 +02:00
class SubscriptionController extends Controller
{
2022-02-12 01:16:37 +01:00
public function index(Request $request): Response
{
2021-07-04 01:44:41 +02:00
session()->put('menu', 'subscription');
session()->put('title', 'Beiträge');
2022-02-12 14:28:03 +01:00
return \Inertia::render('subscription/SubscriptionIndex', [
2021-07-04 01:44:41 +02:00
'data' => SubscriptionResource::collection(Subscription::get()),
]);
}
2022-02-12 01:16:37 +01:00
public function create(): Response
{
2021-07-04 01:44:41 +02:00
session()->put('menu', 'subscription');
session()->put('title', 'Beitrag erstellen');
2022-02-12 14:28:03 +01:00
return \Inertia::render('subscription/SubscriptionForm', [
2022-01-02 12:32:57 +01:00
'fees' => Fee::pluck('name', 'id'),
2021-07-04 01:44:41 +02:00
'mode' => 'create',
2022-12-13 23:11:32 +01:00
'data' => [
2024-06-28 12:47:41 +02:00
'name' => '',
'fee_id' => null,
2022-12-13 23:11:32 +01:00
'children' => [],
],
2023-05-02 23:13:00 +02:00
'meta' => SubscriptionResource::meta(),
2021-07-04 01:44:41 +02:00
]);
}
2022-02-12 01:16:37 +01:00
public function store(Request $request): RedirectResponse
{
2022-12-13 23:11:32 +01:00
$subscriptionParams = $request->validate([
2021-07-04 01:44:41 +02:00
'name' => 'required|max:255',
'fee_id' => 'required|exists:fees,id',
2022-11-17 21:05:35 +01:00
], [], [
'fee_id' => 'Nami-Beitrag',
2022-12-13 23:11:32 +01:00
]);
$children = $request->validate([
'children' => 'present|array',
'children.*.amount' => 'required|numeric',
'children.*.name' => 'required|max:255',
]);
$subscription = Subscription::create($subscriptionParams);
$subscription->children()->createMany($children['children']);
2021-07-04 01:44:41 +02:00
return redirect()->route('subscription.index');
}
2022-02-12 01:16:37 +01:00
public function edit(Subscription $subscription, Request $request): Response
{
2021-07-04 01:44:41 +02:00
session()->put('menu', 'subscription');
session()->put('title', "Beitrag {$subscription->name} bearbeiten");
2022-02-12 14:28:03 +01:00
return \Inertia::render('subscription/SubscriptionForm', [
2022-01-02 12:32:57 +01:00
'fees' => Fee::pluck('name', 'id'),
2021-07-04 01:44:41 +02:00
'mode' => 'edit',
'data' => new SubscriptionResource($subscription),
2023-05-02 23:13:00 +02:00
'meta' => SubscriptionResource::meta(),
2021-07-04 01:44:41 +02:00
]);
}
2022-02-12 01:16:37 +01:00
public function update(Subscription $subscription, Request $request): RedirectResponse
{
2022-12-13 23:11:32 +01:00
$subscriptionParams = $request->validate([
2021-07-04 01:44:41 +02:00
'name' => 'required|max:255',
'fee_id' => 'required|exists:fees,id',
2022-11-17 21:05:35 +01:00
], [], [
'fee_id' => 'Nami-Beitrag',
2022-12-13 23:11:32 +01:00
]);
$subscription->update($subscriptionParams);
$children = $request->validate([
'children' => 'present|array',
'children.*.amount' => 'required|numeric',
'children.*.name' => 'required|max:255',
]);
$subscription->children()->delete();
$subscription->children()->createMany($children['children']);
return redirect()->route('subscription.index');
}
public function destroy(Subscription $subscription): RedirectResponse
{
$subscription->delete();
2021-07-04 01:44:41 +02:00
return redirect()->route('subscription.index');
}
}