71 lines
2.0 KiB
PHP
71 lines
2.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\Dashboard;
|
|
|
|
use App\Http\Requests\CategoryRequest;
|
|
use App\Models\Category;
|
|
use App\Repository\Interfaces\CategoryRepository;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\View\View;
|
|
|
|
class CategoryController
|
|
{
|
|
|
|
public function __construct(
|
|
private CategoryRepository $categoryRepository
|
|
) {}
|
|
|
|
public function update(CategoryRequest $request, Category $category)
|
|
{
|
|
$validate = $request->validated();
|
|
if ($this->categoryRepository->update($category, $validate)) {
|
|
return back()->with('message', 'Zaktualizowano kategorię!');
|
|
}
|
|
|
|
return back()->withError(['message_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
|
|
{
|
|
return view('dashboard.categories.create');
|
|
}
|
|
|
|
public function edit(Category $category): View
|
|
{
|
|
return view('dashboard.categories.edit', compact('category'));
|
|
}
|
|
|
|
public function delete(Category $category): View
|
|
{
|
|
return view('dashboard.categories.delete', compact('category'));
|
|
}
|
|
|
|
public function destroy(Category $category): RedirectResponse
|
|
{
|
|
$name = $category->name;
|
|
$category->delete();
|
|
|
|
return redirect()->route('admin.home')->with('message', 'Usunięto kategorię "'. $name .'"');
|
|
}
|
|
|
|
}
|