فهرست منبع

feat(class): resource com campos reais e validação do request

ClassResource expõe título, pacote, horários e contadores
(presentes/total); ClassRequest valida os campos da aula.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
ebagabee 1 ماه پیش
والد
کامیت
e2700931e2
2فایلهای تغییر یافته به همراه34 افزوده شده و 22 حذف شده
  1. 15 11
      app/Http/Requests/ClassRequest.php
  2. 19 11
      app/Http/Resources/ClassResource.php

+ 15 - 11
app/Http/Requests/ClassRequest.php

@@ -6,20 +6,24 @@
 
 class ClassRequest extends FormRequest
 {
-    public function rules(): array
+    public function authorize(): bool
     {
-        $rules = [
-            // Add your validation rules here
-            //'field' => 'sometimes|string|max:255',
-        ];
+        return true;
+    }
 
-        // Different rules for creation
-        //if ($this->isMethod('POST')) {
-            // Make fields required if needed
-            // $rules['field'] = 'required|string|max:255';
-        //}
+    public function rules(): array
+    {
+        $required = $this->isMethod('POST') ? 'required' : 'sometimes';
 
-        return $rules;
+        return [
+            'title'                 => "{$required}|string|max:255",
+            'class_package_unit_id' => "{$required}|integer|exists:class_package_units,id",
+            'instructor'            => 'nullable|string|max:255',
+            'room'                  => 'nullable|string|max:255',
+            'date_time_start'       => "{$required}|date",
+            'date_time_end'         => "{$required}|date|after:date_time_start",
+            'status'                => 'sometimes|string|max:50',
+        ];
     }
 
     /**

+ 19 - 11
app/Http/Resources/ClassResource.php

@@ -18,19 +18,27 @@ class ClassResource extends JsonResource
     public function toArray(Request $request): array
     {
         return [
-            'id' => $this->id,
-            'name' => $this->name,
-            'created_at' => Carbon::parse($this->created_at)->format('Y-m-d H:i:s'),
-            'updated_at' => Carbon::parse($this->updated_at)->format('Y-m-d H:i:s'),
-            // Add your fields here
+            'id'                    => $this->id,
+            'title'                 => $this->title,
+            'instructor'            => $this->instructor,
+            'room'                  => $this->room,
+            'status'                => $this->status,
+            'unit_id'               => $this->unit_id,
+            'class_package_unit_id' => $this->class_package_unit_id,
+            'package_name'          => $this->whenLoaded('packageUnit', fn () => $this->packageUnit?->name),
+            'date_time_start'       => $this->date_time_start
+                ? Carbon::parse($this->date_time_start)->format('Y-m-d\TH:i:s')
+                : null,
+            'date_time_end'         => $this->date_time_end
+                ? Carbon::parse($this->date_time_end)->format('Y-m-d\TH:i:s')
+                : null,
 
-            // Conditional fields
-            // $this->mergeWhen($request->user()?->isAdmin(), [
-            //     'internal_notes' => $this->internal_notes,
-            // ]),
+            // Contadores p/ exibir no card (presentes / total de alunos)
+            'students_count'        => $this->students_count,
+            'present_count'         => $this->present_count,
 
-            // Relationships
-            // 'user' => new UserResource($this->whenLoaded('user')),
+            'created_at'            => Carbon::parse($this->created_at)->format('Y-m-d H:i:s'),
+            'updated_at'            => Carbon::parse($this->updated_at)->format('Y-m-d H:i:s'),
         ];
     }