update controllers logic
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace App\Http\Controllers\Api\Attendance;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Services\ApplicationUrlService;
|
||||
use App\Services\AttendanceCommentTemplateService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
@@ -11,10 +12,22 @@ use Illuminate\Support\Facades\Validator;
|
||||
class AttendanceCommentTemplateController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
protected AttendanceCommentTemplateService $service
|
||||
protected AttendanceCommentTemplateService $service,
|
||||
protected ApplicationUrlService $urls,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Bootstrap URLs for admin UIs (legacy CodeIgniter parity + SPA).
|
||||
*/
|
||||
public function bootstrapUrls(): JsonResponse
|
||||
{
|
||||
return response()->json([
|
||||
'status' => true,
|
||||
'data' => $this->urls->attendanceCommentTemplateBootstrapData(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$activeOnly = filter_var(
|
||||
@@ -119,4 +132,83 @@ class AttendanceCommentTemplateController extends Controller
|
||||
'message' => 'Template deleted successfully.',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* CodeIgniter {@see \App\Controllers\View\AttendanceCommentTemplateController::save}
|
||||
* POST fields: id?, min_score, max_score, template_text, is_active (checkbox "on").
|
||||
*/
|
||||
public function legacySave(Request $request): JsonResponse
|
||||
{
|
||||
$id = $request->input('id');
|
||||
$hasId = $id !== null && $id !== '' && (int) $id > 0;
|
||||
|
||||
$rules = [
|
||||
'min_score' => ['required', 'integer', 'min:0', 'max:100'],
|
||||
'max_score' => ['required', 'integer', 'min:0', 'max:100'],
|
||||
'template_text' => ['required', 'string', 'max:5000'],
|
||||
];
|
||||
|
||||
$validator = Validator::make($request->all(), $rules);
|
||||
|
||||
$validator->after(function ($v) use ($request) {
|
||||
$min = (int) $request->input('min_score');
|
||||
$max = (int) $request->input('max_score');
|
||||
if ($max < $min) {
|
||||
$v->errors()->add('max_score', 'The max_score field must be greater than or equal to min_score.');
|
||||
}
|
||||
});
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json([
|
||||
'message' => 'Validation failed.',
|
||||
'errors' => $validator->errors(),
|
||||
], 422);
|
||||
}
|
||||
|
||||
$isActive = $this->legacyIsActive($request);
|
||||
|
||||
$payload = [
|
||||
'min_score' => (int) $request->input('min_score'),
|
||||
'max_score' => (int) $request->input('max_score'),
|
||||
'template_text' => (string) $request->input('template_text'),
|
||||
'is_active' => $isActive,
|
||||
];
|
||||
|
||||
if ($hasId) {
|
||||
$this->service->update((int) $id, $payload);
|
||||
} else {
|
||||
$this->service->create($payload);
|
||||
}
|
||||
|
||||
return response()->json(['status' => 'success']);
|
||||
}
|
||||
|
||||
/**
|
||||
* CodeIgniter {@see \App\Controllers\View\AttendanceCommentTemplateController::delete}.
|
||||
*/
|
||||
public function legacyDelete(Request $request): JsonResponse
|
||||
{
|
||||
$id = $request->input('id');
|
||||
if ($id === null || $id === '' || (int) $id <= 0) {
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => 'ID not provided',
|
||||
], 400);
|
||||
}
|
||||
|
||||
$this->service->delete((int) $id);
|
||||
|
||||
return response()->json(['status' => 'success']);
|
||||
}
|
||||
|
||||
private function legacyIsActive(Request $request): bool
|
||||
{
|
||||
$raw = $request->input('is_active');
|
||||
|
||||
return $raw === 'on'
|
||||
|| $raw === 1
|
||||
|| $raw === '1'
|
||||
|| $raw === true
|
||||
|| $raw === 'true';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user