180 lines
5.1 KiB
PHP
180 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Setting;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class SettingController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index(): JsonResponse
|
|
{
|
|
try {
|
|
$settings = Setting::all();
|
|
return response()->json($settings);
|
|
} catch (\Exception $e) {
|
|
return response()->json([
|
|
'message' => 'Instellingen ophalen mislukt',
|
|
'error' => $e->getMessage()
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(Request $request): JsonResponse
|
|
{
|
|
try {
|
|
$validated = $request->validate([
|
|
'key' => 'required|string|max:255',
|
|
'value' => 'required|string',
|
|
'description' => 'nullable|string',
|
|
], [
|
|
'key.required' => 'Sleutel is verplicht',
|
|
'key.max' => 'Sleutel mag maximaal 255 karakters bevatten',
|
|
'value.required' => 'Waarde is verplicht',
|
|
]);
|
|
|
|
$setting = Setting::create($validated);
|
|
|
|
return response()->json($setting, 201);
|
|
} catch (ValidationException $e) {
|
|
return response()->json([
|
|
'message' => 'Validatie mislukt',
|
|
'errors' => $e->errors()
|
|
], 422);
|
|
} catch (\Exception $e) {
|
|
return response()->json([
|
|
'message' => 'Instelling aanmaken mislukt',
|
|
'error' => $e->getMessage()
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show(Setting $setting): JsonResponse
|
|
{
|
|
try {
|
|
return response()->json($setting);
|
|
} catch (\Exception $e) {
|
|
return response()->json([
|
|
'message' => 'Instelling ophalen mislukt',
|
|
'error' => $e->getMessage()
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(Request $request, Setting $setting): JsonResponse
|
|
{
|
|
try {
|
|
$validated = $request->validate([
|
|
'value' => 'required|string',
|
|
'description' => 'nullable|string',
|
|
], [
|
|
'value.required' => 'Waarde is verplicht',
|
|
]);
|
|
|
|
$setting->update($validated);
|
|
|
|
return response()->json($setting);
|
|
} catch (ValidationException $e) {
|
|
return response()->json([
|
|
'message' => 'Validatie mislukt',
|
|
'errors' => $e->errors()
|
|
], 422);
|
|
} catch (\Exception $e) {
|
|
return response()->json([
|
|
'message' => 'Instelling bijwerken mislukt',
|
|
'error' => $e->getMessage()
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(Setting $setting): JsonResponse
|
|
{
|
|
try {
|
|
$setting->delete();
|
|
return response()->json([
|
|
'message' => 'Instelling succesvol verwijderd'
|
|
]);
|
|
} catch (\Exception $e) {
|
|
return response()->json([
|
|
'message' => 'Instelling verwijderen mislukt',
|
|
'error' => $e->getMessage()
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get a specific setting by key
|
|
*/
|
|
public function getByKey(string $key): JsonResponse
|
|
{
|
|
try {
|
|
$setting = Setting::where('key', $key)->first();
|
|
|
|
if (!$setting) {
|
|
return response()->json([
|
|
'message' => 'Instelling niet gevonden'
|
|
], 404);
|
|
}
|
|
|
|
return response()->json($setting);
|
|
} catch (\Exception $e) {
|
|
return response()->json([
|
|
'message' => 'Instelling ophalen mislukt',
|
|
'error' => $e->getMessage()
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Update or create a setting by key
|
|
*/
|
|
public function updateByKey(Request $request, string $key): JsonResponse
|
|
{
|
|
try {
|
|
$validated = $request->validate([
|
|
'value' => 'required|string',
|
|
'description' => 'nullable|string',
|
|
], [
|
|
'value.required' => 'Waarde is verplicht',
|
|
]);
|
|
|
|
$setting = Setting::updateOrCreate(
|
|
['key' => $key],
|
|
[
|
|
'value' => $validated['value'],
|
|
'description' => $validated['description'] ?? null
|
|
]
|
|
);
|
|
|
|
return response()->json($setting);
|
|
} catch (ValidationException $e) {
|
|
return response()->json([
|
|
'message' => 'Validatie mislukt',
|
|
'errors' => $e->errors()
|
|
], 422);
|
|
} catch (\Exception $e) {
|
|
return response()->json([
|
|
'message' => 'Instelling bijwerken mislukt',
|
|
'error' => $e->getMessage()
|
|
], 500);
|
|
}
|
|
}
|
|
}
|