Files
Caramel-CRM/app/Http/Controllers/TodoController.php
T

135 lines
4.4 KiB
PHP
Raw Normal View History

2025-12-02 17:32:52 +01:00
<?php
namespace App\Http\Controllers;
use App\Models\Todo;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use App\Support\ApiDataTransformer;
class TodoController extends Controller
{
public function index()
{
2026-02-17 10:35:03 +01:00
$todos = Todo::with('type')->orderBy('due_date')->get();
2025-12-02 17:32:52 +01:00
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)
2025-12-02 17:32:52 +01:00
{
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',
2025-12-02 17:32:52 +01:00
]);
$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'];
}
}
}
2025-12-02 17:32:52 +01:00
$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',
2025-12-02 17:32:52 +01:00
]);
$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'];
}
}
}
2025-12-02 17:32:52 +01:00
$todo->update($data);
return response()->json($todo);
}
public function destroy(string $id)
{
$todo = Todo::findOrFail($id);
$todo->delete();
return response()->noContent();
}
}