- add category management

This commit is contained in:
2023-07-28 15:43:53 +02:00
parent d47c719d13
commit 8974721c9c
9 changed files with 235 additions and 41 deletions

View File

@@ -9,6 +9,7 @@ use App\Models\Category;
use App\Repository\Interfaces\CategoryRepository;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;
use Inertia\Response as InertiaResponse;
class CategoryController
{
@@ -21,42 +22,35 @@ class CategoryController
{
$validate = $request->validated();
if ($this->categoryRepository->update($category, $validate)) {
return back()->with('message', 'Zaktualizowano kategorię!');
return back()
->with('success', 'Zaktualizowano kategorię!');
}
return back()->withError(['message_error', 'Wystąpił błąd podczas aktualizacji!']);
return back()
->with(['error', 'Wystąpił błąd podczas aktualizacji!']);
}
public function store(CategoryRequest $request)
{
// $validate = $request->validated();
// if ($category = $this->categoryRepository->create($validate)) {
// return redirect()
// ->route('admin.category.update', compact('category'))
// ->with('message', 'Utworzono kategorię!');
// }
// return back()->withError(['message_error', 'Wystąpił błąd podczas tworzenia!']);
$category = $this->categoryRepository->create($request->validated());
return redirect()
->route('admin.category.update', compact('category'))
->with('message', 'Utworzono kategorię!');
}
public function create(): View
public function create(): InertiaResponse
{
return view('dashboard.categories.create');
return inertia('Categories/Create');
}
public function edit(Category $category): View
public function edit(Category $category): InertiaResponse
{
return view('dashboard.categories.edit', compact('category'));
return inertia('Categories/Edit', compact('category'));
}
public function delete(Category $category): View
public function delete(Category $category): InertiaResponse
{
return view('dashboard.categories.delete', compact('category'));
return inertia('Categories/ConfirmDelete', compact('category'));
}
public function destroy(Category $category): RedirectResponse
@@ -64,7 +58,9 @@ class CategoryController
$name = $category->name;
$category->delete();
return redirect()->route('admin.home')->with('message', 'Usunięto kategorię "'. $name .'"');
return redirect()
->route('admin.home')
->with('info', 'Usunięto kategorię "'. $name .'"');
}
}

View File

@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace App\Http\Controllers\Dashboard;
use App\Http\Requests\ProjectRequest;

View File

@@ -48,7 +48,7 @@ class CategoryRepository implements CategoryRepositoryInterface
public function update(Category $category, array $data = []): bool
{
$data = $this->parseToArray($data);
if (!$category->default && isset($data['default']) && $data['default'] === true)
if (!$category->default && $data['default'] === true)
$this->unsetDefault();
return $category
@@ -58,7 +58,7 @@ class CategoryRepository implements CategoryRepositoryInterface
public function create(array $data = []): Category
{
$data = $this->parseToArray($data);
if (isset($data['default']) && $data['default'] === true)
if ($data['default'] === true)
$this->unsetDefault();
return $this->category
@@ -85,18 +85,12 @@ class CategoryRepository implements CategoryRepositoryInterface
if (isset($data['priority']) && !is_integer($data['priority']))
$toSave['priority'] = (int)$data['priority'];
if (
isset($data['default']) &&
in_array($data['default'], ['yes', 'on', 1, true])
) $toSave['default'] = true;
else $toSave['default'] = false;
$toSave['default'] = $data['default'];
if (
(isset($toSave['default']) && $toSave['default'] === true) ||
(isset($data['visible']) &&
in_array($data['visible'], ['yes', 'on', 1, true]))
) $toSave['visible'] = true;
else $toSave['visible'] = false;
if ($toSave['default'] === true)
$toSave['visible'] = true;
else
$toSave['visible'] = $data['visible'];
return $toSave;
}