Two month of work
This commit is contained in:
@@ -11,14 +11,14 @@ class NoteController extends Controller
|
||||
{
|
||||
/**
|
||||
* 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 index(Request $request, $modelId)
|
||||
public function index(Request $request, string $modelType, int $modelId)
|
||||
{
|
||||
$modelType = $request->route()->parameter('modelType');
|
||||
if (!$modelType) {
|
||||
$modelType = $request->route()->getAction('modelType');
|
||||
}
|
||||
$model = app("App\\Models\\" . ucfirst($modelType))::findOrFail($modelId);
|
||||
$model = app("App\\Models\\" . $modelType)::findOrFail($modelId);
|
||||
|
||||
// Lade alle Notizen des Modells mit der Benutzerbeziehung
|
||||
$notes = $model->notes()->with('user')->orderBy('created_at', 'desc')->get();
|
||||
@@ -34,24 +34,30 @@ public function index(Request $request, $modelId)
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request, $modelId)
|
||||
public function store(Request $request)
|
||||
{
|
||||
$modelType = $request->route()->parameter('modelType');
|
||||
if (!$modelType) {
|
||||
$modelType = $request->route()->getAction('modelType');
|
||||
}
|
||||
|
||||
$validatedData = $request->validate([
|
||||
'userId' => 'required|integer|exists:users,id',
|
||||
'text' => 'required|string',
|
||||
'userId' => 'required|integer'
|
||||
'noteableId' => 'required|integer',
|
||||
'noteableType' => 'required|string',
|
||||
'createdAt' => 'sometimes|date|nullable'
|
||||
]);
|
||||
|
||||
// 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 = app("App\\Models\\" . $snakeCaseData['noteable_type'])::findOrFail($snakeCaseData['noteable_id']);
|
||||
|
||||
// Create a new Note instance
|
||||
$note = new Note($snakeCaseData);
|
||||
|
||||
// Set the created_at field if it is provided
|
||||
if (isset($snakeCaseData['created_at'])) {
|
||||
$note->created_at = $snakeCaseData['created_at'];
|
||||
}
|
||||
|
||||
$model->notes()->save($note);
|
||||
|
||||
return response()->json($this->single($note->id), 201);
|
||||
@@ -64,22 +70,6 @@ public static function single($id)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user