feat: add category management
associate with entries and text widgets
This commit is contained in:
parent
c83028b4d4
commit
9b9e1a8e29
22 changed files with 392 additions and 46 deletions
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Resources\EntryResource;
|
||||
use App\Models\Entry;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
|
@ -14,7 +15,7 @@ class EntryController extends Controller
|
|||
*/
|
||||
public function index()
|
||||
{
|
||||
return Entry::all();
|
||||
return EntryResource::collection(Entry::with('category')->get());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -24,7 +25,7 @@ class EntryController extends Controller
|
|||
{
|
||||
$user = Auth::user();
|
||||
|
||||
if (!$user || $user->email !== config('app.admin_email')) {
|
||||
if (! $user || $user->email !== config('app.admin_email')) {
|
||||
return response()->json(['message' => 'Forbidden'], 403);
|
||||
}
|
||||
|
||||
|
|
@ -35,13 +36,13 @@ class EntryController extends Controller
|
|||
|
||||
$validated['slug'] = $this->generateUniqueSlug($validated['title']);
|
||||
|
||||
return Entry::create($validated);
|
||||
return new EntryResource(Entry::create($validated));
|
||||
}
|
||||
|
||||
private function generateUniqueSlug(string $title): string
|
||||
{
|
||||
do {
|
||||
$slug = Str::slug($title) . '-' . Str::random(8);
|
||||
$slug = Str::slug($title).'-'.Str::random(8);
|
||||
} while (Entry::where('slug', $slug)->exists());
|
||||
|
||||
return $slug;
|
||||
|
|
@ -54,7 +55,7 @@ class EntryController extends Controller
|
|||
{
|
||||
$this->authorize('view', $entry);
|
||||
|
||||
return $entry;
|
||||
return new EntryResource($entry);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -69,7 +70,7 @@ class EntryController extends Controller
|
|||
|
||||
$entry->update($validated);
|
||||
|
||||
return $entry;
|
||||
return new EntryResource($entry);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Resources\TextWidgetResource;
|
||||
use App\Models\TextWidget;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
|
@ -13,7 +14,7 @@ class TextWidgetController extends Controller
|
|||
*/
|
||||
public function index()
|
||||
{
|
||||
return TextWidget::all();
|
||||
return TextWidget::with('category')->get()->map(fn ($tw) => new TextWidgetResource($tw));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -33,7 +34,7 @@ class TextWidgetController extends Controller
|
|||
'content' => 'required|string',
|
||||
]);
|
||||
|
||||
return TextWidget::create($validated);
|
||||
return new TextWidgetResource(TextWidget::create($validated));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -41,7 +42,7 @@ class TextWidgetController extends Controller
|
|||
*/
|
||||
public function show(TextWidget $textWidget)
|
||||
{
|
||||
return $textWidget;
|
||||
return new TextWidgetResource($textWidget->load('category'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -57,7 +58,7 @@ class TextWidgetController extends Controller
|
|||
|
||||
$textWidget->update($validated);
|
||||
|
||||
return $textWidget;
|
||||
return new TextWidgetResource($textWidget->load('category'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue