196 lines
5.8 KiB
PHP
196 lines
5.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\PageContent;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class PageContentController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index(): JsonResponse
|
|
{
|
|
try {
|
|
$content = PageContent::all();
|
|
return response()->json($content);
|
|
} catch (\Exception $e) {
|
|
return response()->json([
|
|
'message' => 'Pagina-inhoud 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|unique:page_content,key',
|
|
'value' => 'required|string',
|
|
'description' => 'nullable|string',
|
|
], [
|
|
'key.required' => 'Sleutel is verplicht',
|
|
'key.unique' => 'Deze sleutel bestaat al',
|
|
'key.max' => 'Sleutel mag maximaal 255 karakters bevatten',
|
|
'value.required' => 'Waarde is verplicht',
|
|
]);
|
|
|
|
$content = PageContent::create($validated);
|
|
|
|
return response()->json($content, 201);
|
|
} catch (ValidationException $e) {
|
|
return response()->json([
|
|
'message' => 'Validatie mislukt',
|
|
'errors' => $e->errors()
|
|
], 422);
|
|
} catch (\Exception $e) {
|
|
return response()->json([
|
|
'message' => 'Pagina-inhoud aanmaken mislukt',
|
|
'error' => $e->getMessage()
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show(PageContent $pageContent): JsonResponse
|
|
{
|
|
try {
|
|
return response()->json($pageContent);
|
|
} catch (\Exception $e) {
|
|
return response()->json([
|
|
'message' => 'Pagina-inhoud ophalen mislukt',
|
|
'error' => $e->getMessage()
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(Request $request, PageContent $pageContent): JsonResponse
|
|
{
|
|
try {
|
|
$validated = $request->validate([
|
|
'value' => 'required|string',
|
|
'description' => 'nullable|string',
|
|
], [
|
|
'value.required' => 'Waarde is verplicht',
|
|
]);
|
|
|
|
$pageContent->update($validated);
|
|
|
|
return response()->json($pageContent);
|
|
} catch (ValidationException $e) {
|
|
return response()->json([
|
|
'message' => 'Validatie mislukt',
|
|
'errors' => $e->errors()
|
|
], 422);
|
|
} catch (\Exception $e) {
|
|
return response()->json([
|
|
'message' => 'Pagina-inhoud bijwerken mislukt',
|
|
'error' => $e->getMessage()
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(PageContent $pageContent): JsonResponse
|
|
{
|
|
try {
|
|
$pageContent->delete();
|
|
return response()->json([
|
|
'message' => 'Pagina-inhoud succesvol verwijderd'
|
|
]);
|
|
} catch (\Exception $e) {
|
|
return response()->json([
|
|
'message' => 'Pagina-inhoud verwijderen mislukt',
|
|
'error' => $e->getMessage()
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get a specific content by key
|
|
*/
|
|
public function getByKey(string $key): JsonResponse
|
|
{
|
|
try {
|
|
$content = PageContent::where('key', $key)->first();
|
|
|
|
if (!$content) {
|
|
return response()->json([
|
|
'message' => 'Pagina-inhoud niet gevonden'
|
|
], 404);
|
|
}
|
|
|
|
return response()->json($content);
|
|
} catch (\Exception $e) {
|
|
return response()->json([
|
|
'message' => 'Pagina-inhoud ophalen mislukt',
|
|
'error' => $e->getMessage()
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Update or create content 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',
|
|
]);
|
|
|
|
$content = PageContent::updateOrCreate(
|
|
['key' => $key],
|
|
[
|
|
'value' => $validated['value'],
|
|
'description' => $validated['description'] ?? null
|
|
]
|
|
);
|
|
|
|
return response()->json($content);
|
|
} catch (ValidationException $e) {
|
|
return response()->json([
|
|
'message' => 'Validatie mislukt',
|
|
'errors' => $e->errors()
|
|
], 422);
|
|
} catch (\Exception $e) {
|
|
return response()->json([
|
|
'message' => 'Pagina-inhoud bijwerken mislukt',
|
|
'error' => $e->getMessage()
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get all content as key-value pairs for public access
|
|
*/
|
|
public function getPublicContent(): JsonResponse
|
|
{
|
|
try {
|
|
$content = PageContent::all()->pluck('value', 'key');
|
|
return response()->json($content);
|
|
} catch (\Exception $e) {
|
|
return response()->json([
|
|
'message' => 'Pagina-inhoud ophalen mislukt',
|
|
'error' => $e->getMessage()
|
|
], 500);
|
|
}
|
|
}
|
|
} |