orderBy('due_date')->get(); return ApiDataTransformer::snakeToCamel($todos->toArray()); } /** * Display a listing of the resource. * @param Request $request * @param string $modelType The type of the model (e.g., 'Customer', 'Invoice') * @param string $modelId The ID of the model * @return \Illuminate\Http\JsonResponse */ public function todosForModel(Request $request, string $modelType, int $modelId) { $model = app("App\\Models\\" . $modelType)::findOrFail($modelId); // Load all todos of the model with the user relationship $todos = $model->todos()->with('type')->orderBy('created_at', 'desc')->get(); // Transformiere die Daten in camelCase $notesArray = $todos->map(function ($todo) { return ApiDataTransformer::snakeToCamel($todo->toArray()); }); return response()->json($notesArray); } public function single(string $id) { return Todo::with('type')->findOrFail($id); } public function store(Request $request) { $data = $request->validate([ 'id' => 'nullable|string', 'etag' => 'nullable|string', 'title' => 'required|string', 'description' => 'nullable|string', 'type_id' => 'nullable|exists:todo_types,id', 'url' => 'nullable|url', 'due_date' => 'nullable|date', 'recurring' => 'nullable|string', 'priority' => 'nullable|integer', 'status' => 'nullable|string', 'parent' => 'nullable|string', 'object' => 'nullable|string', 'todoable_id' => 'nullable|integer', 'todoable_type' => 'nullable|string', ]); $data['id'] = $data['id'] ?? (string) Str::uuid(); $now = now(); $data['created_at'] = $data['created_at'] ?? $now; $data['last_modified'] = $now; // Set the title with the todoable title if todoable_id and todoable_type are set if (isset($data['todoable_id']) && isset($data['todoable_type'])) { $modelName = str_replace('App\\Models\\', '', $data['todoable_type']); $modelClass = 'App\\Models\\' . $modelName; if (class_exists($modelClass)) { $todoable = $modelClass::find($data['todoable_id']); if ($todoable) { $data['title'] = '[' . $todoable->title . '] ' . $data['title']; } } } $todo = Todo::create($data); return response()->json($todo, 201); } public function update(Request $request, string $id) { $todo = Todo::findOrFail($id); $data = $request->validate([ 'etag' => 'nullable|string', 'title' => 'sometimes|required|string', 'description' => 'nullable|string', 'type_id' => 'nullable|exists:todo_types,id', 'url' => 'nullable|url', 'due_date' => 'nullable|date', 'recurring' => 'nullable|string', 'priority' => 'nullable|integer', 'status' => 'nullable|string', 'parent' => 'nullable|string', 'object' => 'nullable|string', 'todoable_id' => 'nullable|integer', 'todoable_type' => 'nullable|string', ]); $data['last_modified'] = now(); // Set the title with the todoable title if todoable_id and todoable_type are set if (isset($data['todoable_id']) && isset($data['todoable_type'])) { $modelName = str_replace('App\\Models\\', '', $data['todoable_type']); $modelClass = 'App\\Models\\' . $modelName; if (class_exists($modelClass)) { $todoable = $modelClass::find($data['todoable_id']); if ($todoable) { $data['title'] = '[' . $todoable->title . '] ' . $data['title']; } } } $todo->update($data); return response()->json($todo); } public function destroy(string $id) { $todo = Todo::findOrFail($id); $todo->delete(); return response()->noContent(); } }