67 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			67 lines
		
	
	
		
			1.7 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;
 | |
| use Inertia\Response as InertiaResponse;
 | |
| 
 | |
| 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('success', 'Zaktualizowano kategorię!');
 | |
|         }
 | |
| 
 | |
|         return back()
 | |
|             ->with(['error', 'Wystąpił błąd podczas aktualizacji!']);
 | |
|     }
 | |
| 
 | |
|     public function store(CategoryRequest $request)
 | |
|     {
 | |
|         $category = $this->categoryRepository->create($request->validated());
 | |
|         return redirect()
 | |
|             ->route('admin.category.update', compact('category'))
 | |
|             ->with('message', 'Utworzono kategorię!');
 | |
|     }
 | |
| 
 | |
|     public function create(): InertiaResponse
 | |
|     {
 | |
|         return inertia('Categories/Create');
 | |
|     }
 | |
| 
 | |
|     public function edit(Category $category): InertiaResponse
 | |
|     {
 | |
|         return inertia('Categories/Edit', compact('category'));
 | |
|     }
 | |
| 
 | |
|     public function delete(Category $category): InertiaResponse
 | |
|     {
 | |
|         return inertia('Categories/ConfirmDelete', compact('category'));
 | |
|     }
 | |
| 
 | |
|     public function destroy(Category $category): RedirectResponse
 | |
|     {
 | |
|         $name = $category->name;
 | |
|         $category->delete();
 | |
| 
 | |
|         return redirect()
 | |
|             ->route('admin.home')
 | |
|             ->with('info', 'Usunięto kategorię "'. $name .'"');
 | |
|     }
 | |
| 
 | |
| }
 |