115 lines
2.9 KiB
PHP
115 lines
2.9 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Http\Controllers;
|
||
|
|
|
||
|
|
use App\Models\Note;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use App\Support\ApiDataTransformer;
|
||
|
|
use Illuminate\Support\Facades\DB;
|
||
|
|
|
||
|
|
class NoteController extends Controller
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* Display a listing of the resource.
|
||
|
|
*/
|
||
|
|
public function index(Request $request, $modelId)
|
||
|
|
{
|
||
|
|
$modelType = $request->route()->parameter('modelType');
|
||
|
|
if (!$modelType) {
|
||
|
|
$modelType = $request->route()->getAction('modelType');
|
||
|
|
}
|
||
|
|
$model = app("App\\Models\\" . ucfirst($modelType))::findOrFail($modelId);
|
||
|
|
|
||
|
|
// Lade alle Notizen des Modells mit der Benutzerbeziehung
|
||
|
|
$notes = $model->notes()->with('user')->orderBy('created_at', 'desc')->get();
|
||
|
|
|
||
|
|
// Transformiere die Daten in camelCase
|
||
|
|
$notesArray = $notes->map(function ($note) {
|
||
|
|
return ApiDataTransformer::snakeToCamel($note->toArray());
|
||
|
|
});
|
||
|
|
|
||
|
|
return response()->json($notesArray);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Store a newly created resource in storage.
|
||
|
|
*/
|
||
|
|
public function store(Request $request, $modelId)
|
||
|
|
{
|
||
|
|
$modelType = $request->route()->parameter('modelType');
|
||
|
|
if (!$modelType) {
|
||
|
|
$modelType = $request->route()->getAction('modelType');
|
||
|
|
}
|
||
|
|
|
||
|
|
$validatedData = $request->validate([
|
||
|
|
'text' => 'required|string',
|
||
|
|
'userId' => 'required|integer'
|
||
|
|
]);
|
||
|
|
|
||
|
|
// Convert camelCase to snake_case
|
||
|
|
$snakeCaseData = ApiDataTransformer::camelToSnake($validatedData);
|
||
|
|
|
||
|
|
$model = app("App\\Models\\" . ucfirst($modelType))::findOrFail($modelId);
|
||
|
|
$note = new Note($validatedData);
|
||
|
|
$note->user_id = $validatedData['userId'];
|
||
|
|
$model->notes()->save($note);
|
||
|
|
|
||
|
|
return response()->json($this->single($note->id), 201);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function single($id)
|
||
|
|
{
|
||
|
|
$note = Note::with('user')->findOrFail($id);
|
||
|
|
return ApiDataTransformer::snakeToCamel($note->toArray());
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Display the specified resource.
|
||
|
|
*/
|
||
|
|
public function show(Note $note)
|
||
|
|
{
|
||
|
|
//
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Show the form for editing the specified resource.
|
||
|
|
*/
|
||
|
|
public function edit(Note $note)
|
||
|
|
{
|
||
|
|
//
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Update the specified resource in storage.
|
||
|
|
*/
|
||
|
|
public function update(Request $request, Note $note)
|
||
|
|
{
|
||
|
|
//
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Remove the specified resource from storage.
|
||
|
|
*/
|
||
|
|
public function delete(int $id)
|
||
|
|
{
|
||
|
|
DB::beginTransaction();
|
||
|
|
|
||
|
|
try {
|
||
|
|
$note = note::findOrFail($id);
|
||
|
|
$note->delete();
|
||
|
|
DB::commit();
|
||
|
|
|
||
|
|
return response()->json([
|
||
|
|
'message' => 'Notiz gelöscht'
|
||
|
|
], 200);
|
||
|
|
} catch (\Exception $e) {
|
||
|
|
DB::rollBack();
|
||
|
|
return response()->json([
|
||
|
|
'message' => 'Notiz konnte nicht gelöscht werden',
|
||
|
|
'error' => $e->getMessage()
|
||
|
|
], 500);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|