-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPromptSectionController.php
More file actions
125 lines (105 loc) · 3.94 KB
/
PromptSectionController.php
File metadata and controls
125 lines (105 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
declare(strict_types=1);
namespace App\Controllers;
use App\Http\RedirectResponse;
use App\Http\Request;
use App\Http\Response;
use App\Repositories\PromptSectionRepository;
use RuntimeException;
final class PromptSectionController
{
public function __construct(private readonly PromptSectionRepository $sections)
{
}
public function index(Request $request): Response
{
return view('sections/index', [
'sections' => $this->sections->all(),
'message' => $request->query('message'),
]);
}
public function create(Request $request): Response
{
return view('sections/form', [
'action' => '/sections/create',
'section' => ['key' => '', 'title' => '', 'description' => '', 'order_index' => 0, 'enabled' => 1],
'errors' => [],
]);
}
public function store(Request $request): Response
{
$payload = $this->validate($request);
if ($payload['errors']) {
return view('sections/form', [
'action' => '/sections/create',
'section' => $payload['data'],
'errors' => $payload['errors'],
]);
}
if ($this->sections->findByKey($payload['data']['key'])) {
$payload['errors']['key'] = 'A section with this key already exists.';
return view('sections/form', [
'action' => '/sections/create',
'section' => $payload['data'],
'errors' => $payload['errors'],
]);
}
$this->sections->create($payload['data']);
return new RedirectResponse('/sections?message=Section%20created');
}
public function edit(Request $request, array $params): Response
{
$section = $this->requireSection((int) $params['id']);
return view('sections/form', [
'action' => '/sections/' . $section['id'] . '/edit',
'section' => $section,
'errors' => [],
]);
}
public function update(Request $request, array $params): Response
{
$section = $this->requireSection((int) $params['id']);
$payload = $this->validate($request, $section, false);
if ($payload['errors']) {
return view('sections/form', [
'action' => '/sections/' . $section['id'] . '/edit',
'section' => array_merge($section, $payload['data']),
'errors' => $payload['errors'],
]);
}
$this->sections->update($section['id'], $payload['data']);
return new RedirectResponse('/sections?message=Section%20updated');
}
private function requireSection(int $id): array
{
$section = $this->sections->find($id);
if (!$section) {
throw new RuntimeException('Section not found.');
}
return $section;
}
/**
* @return array{data:array<string,mixed>,errors:array<string,string>}
*/
private function validate(Request $request, array $defaults = [], bool $requireKey = true): array
{
$data = [
'key' => trim((string) $request->input('key', $defaults['key'] ?? '')),
'title' => trim((string) $request->input('title', $defaults['title'] ?? '')),
'description' => (string) $request->input('description', $defaults['description'] ?? ''),
'order_index' => (int) $request->input('order_index', $defaults['order_index'] ?? 0),
'enabled' => $request->input('enabled', $defaults['enabled'] ?? 1) ? 1 : 0,
];
$errors = [];
if ($requireKey && $data['key'] === '') {
$errors['key'] = 'Key is required.';
}
if ($data['title'] === '') {
$errors['title'] = 'Title is required.';
}
if ($data['order_index'] < 0) {
$errors['order_index'] = 'Order index must be zero or greater.';
}
return ['data' => $data, 'errors' => $errors];
}
}