Add Notes model, controller and database table. Implement it in Customer Dialog, #6
This commit is contained in:
@@ -4,7 +4,9 @@
|
||||
|
||||
use Inertia\Inertia;
|
||||
use App\Models\Customer;
|
||||
use App\Models\Note;
|
||||
use App\Support\ApiDataTransformer;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class CustomerController extends Controller
|
||||
{
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -27,7 +27,6 @@ class Customer extends Model
|
||||
'billing_address',
|
||||
'payment_terms_id',
|
||||
'status',
|
||||
'notes',
|
||||
'logo',
|
||||
];
|
||||
|
||||
@@ -96,4 +95,12 @@ public function paymentTerms()
|
||||
{
|
||||
return $this->belongsTo(PaymentTerms::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the notes for the customer
|
||||
*/
|
||||
public function notes()
|
||||
{
|
||||
return $this->morphMany(Note::class, 'notable');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Note extends Model
|
||||
{
|
||||
/** @use HasFactory<\Database\Factories\NoteFactory> */
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'text',
|
||||
'notable_id',
|
||||
'notable_type'
|
||||
];
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function notable()
|
||||
{
|
||||
return $this->morphTo();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user