- add notes
This commit is contained in:
84
app/Http/Controllers/Dashboard/NoteController.php
Normal file
84
app/Http/Controllers/Dashboard/NoteController.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\Dashboard;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\NoteRequest;
|
||||
use App\Http\Resources\NoteCollection;
|
||||
use App\Http\Resources\NoteResource;
|
||||
use App\Models\Note;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Inertia\Response as InertiaResponse;
|
||||
|
||||
class NoteController extends Controller
|
||||
{
|
||||
public function index(): InertiaResponse
|
||||
{
|
||||
return inertia(
|
||||
'Notes/Index',
|
||||
[
|
||||
'notes' => new NoteCollection(
|
||||
Note::query()
|
||||
->orderByDesc('id')
|
||||
->get()
|
||||
),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function create(): InertiaResponse
|
||||
{
|
||||
return inertia('Notes/Create');
|
||||
}
|
||||
|
||||
public function store(NoteRequest $request): RedirectResponse
|
||||
{
|
||||
$note = Note::query()->create([
|
||||
'title' => $request->get('title'),
|
||||
'note' => $request->get('note'),
|
||||
]);
|
||||
|
||||
return redirect()
|
||||
->route('admin.note.show', compact('note'))
|
||||
->with('success', 'Utworzono nową notatkę.');
|
||||
}
|
||||
|
||||
public function show(Note $note): InertiaResponse
|
||||
{
|
||||
return inertia('Notes/Show', ['note' => new NoteResource($note)]);
|
||||
}
|
||||
|
||||
public function edit(Note $note): InertiaResponse
|
||||
{
|
||||
return inertia('Notes/Edit', ['note' => new NoteResource($note)]);
|
||||
}
|
||||
|
||||
public function update(NoteRequest $request, Note $note): RedirectResponse
|
||||
{
|
||||
$note->update([
|
||||
'title' => $request->get('title'),
|
||||
'note' => $request->get('note'),
|
||||
]);
|
||||
|
||||
return redirect()
|
||||
->route('admin.note.show', compact('note'))
|
||||
->with('success', 'Notatka ' . $request->get('title') . ' została zaktualizowana.');
|
||||
}
|
||||
|
||||
public function delete(Note $note): InertiaResponse
|
||||
{
|
||||
return inertia('Notes/ConfirmDelete', ['note' => new NoteResource($note)]);
|
||||
}
|
||||
|
||||
public function destroy(Note $note): RedirectResponse
|
||||
{
|
||||
$title = $note->title;
|
||||
$note->delete();
|
||||
|
||||
return redirect()
|
||||
->route('admin.note.index')
|
||||
->with('info', 'Notatka ' . $title . ' została usunięta.');
|
||||
}
|
||||
}
|
18
app/Http/Requests/NoteRequest.php
Normal file
18
app/Http/Requests/NoteRequest.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class NoteRequest extends FormRequest
|
||||
{
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'title' => 'required|string|min:3|max:250',
|
||||
'note' => 'required|string|min:3|max:1000',
|
||||
];
|
||||
}
|
||||
}
|
17
app/Http/Resources/NoteCollection.php
Normal file
17
app/Http/Resources/NoteCollection.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\ResourceCollection;
|
||||
|
||||
class NoteCollection extends ResourceCollection
|
||||
{
|
||||
public function toArray($request): array
|
||||
{
|
||||
return [
|
||||
'data' => $this->collection,
|
||||
];
|
||||
}
|
||||
}
|
25
app/Http/Resources/NoteResource.php
Normal file
25
app/Http/Resources/NoteResource.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use App\Models\Note;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
/**
|
||||
* @property Note $resource
|
||||
*/
|
||||
class NoteResource extends JsonResource
|
||||
{
|
||||
public static $wrap = null;
|
||||
|
||||
public function toArray($request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->resource->id,
|
||||
'title' => $this->resource->title,
|
||||
'note' => $this->resource->note,
|
||||
];
|
||||
}
|
||||
}
|
20
app/Models/Note.php
Normal file
20
app/Models/Note.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property string $title
|
||||
* @property string $note
|
||||
*/
|
||||
class Note extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $guarded = [];
|
||||
}
|
Reference in New Issue
Block a user