Big update for category controller - for admin panel
This commit is contained in:
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
@@ -15,6 +16,9 @@ class LoginController extends Controller
|
||||
|
||||
public function authenticate(Request $request)
|
||||
{
|
||||
if (Auth::check())
|
||||
return redirect()->route('admin.home');
|
||||
|
||||
$credentials = $request->validate([
|
||||
'email' => ['required', 'email'],
|
||||
'password' => ['required'],
|
||||
@@ -39,8 +43,11 @@ class LoginController extends Controller
|
||||
return redirect()->route('admin.auth.login');
|
||||
}
|
||||
|
||||
public function login(): View
|
||||
public function login(): View|RedirectResponse
|
||||
{
|
||||
if (Auth::check())
|
||||
return redirect()->route('admin.home');
|
||||
|
||||
return view('auth.login');
|
||||
}
|
||||
|
||||
|
@@ -3,6 +3,8 @@
|
||||
namespace App\Http\Controllers\Dashboard;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Repository\Interfaces\CategoryRepository;
|
||||
use App\Repository\Interfaces\ProjectRepository;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\View\View;
|
||||
|
||||
@@ -10,12 +12,18 @@ class AdminPanelController extends Controller
|
||||
{
|
||||
|
||||
public function __construct(
|
||||
|
||||
) {}
|
||||
private CategoryRepository $categoryRepository,
|
||||
private ProjectRepository $projectRepository
|
||||
) {
|
||||
$this->categoryRepository->auth = true;
|
||||
}
|
||||
|
||||
public function __invoke(Request $request): View
|
||||
{
|
||||
return view('dashboard.home');
|
||||
$categories = $this->categoryRepository->all();
|
||||
$projects = $this->projectRepository->all();
|
||||
|
||||
return view('dashboard.home', compact('categories', 'projects'));
|
||||
}
|
||||
|
||||
}
|
||||
|
63
app/Http/Controllers/Dashboard/CategoryController.php
Normal file
63
app/Http/Controllers/Dashboard/CategoryController.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?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', ['category' => $category])->with('message', 'Utworzono kategorię!');
|
||||
}
|
||||
|
||||
return back()->withError(['message_error', 'Wystąpił błąd podczas tworzenia!']);
|
||||
}
|
||||
|
||||
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 .'"');
|
||||
}
|
||||
|
||||
}
|
34
app/Http/Requests/CategoryRequest.php
Normal file
34
app/Http/Requests/CategoryRequest.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class CategoryRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function authorize()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'name' => 'required|string|min:3|max:25',
|
||||
'slug' => 'required|string|min:3|max:25',
|
||||
'priority' => 'required|numeric|min:0|max:10',
|
||||
'default' => 'nullable|in:yes,1,true,on',
|
||||
'visible' => 'nullable|in:yes,1,true,on'
|
||||
];
|
||||
}
|
||||
}
|
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
@@ -25,4 +25,13 @@ class Category extends Model
|
||||
'default' => 'bool',
|
||||
'visible' => 'bool'
|
||||
];
|
||||
|
||||
public function scopeVisibled(Builder $builder)
|
||||
{
|
||||
return $builder->where(function (Builder $query) {
|
||||
$query->where('visible', true)
|
||||
->orWhere('default', true);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -13,6 +13,8 @@ use Illuminate\Support\Collection;
|
||||
class CategoryRepository implements CategoryRepositoryInterface
|
||||
{
|
||||
|
||||
public bool $auth = false;
|
||||
|
||||
public function __construct(
|
||||
private Category $category
|
||||
) {}
|
||||
@@ -22,8 +24,12 @@ class CategoryRepository implements CategoryRepositoryInterface
|
||||
$categories = $this->category
|
||||
->query()
|
||||
->orderby('priority', 'ASC')
|
||||
->orderby('name', 'ASC')->get();
|
||||
return (new CategoryCollection($categories))->collection;
|
||||
->orderby('name', 'ASC');
|
||||
|
||||
if (!$this->auth)
|
||||
$categories->visibled();
|
||||
|
||||
return (new CategoryCollection($categories->get()))->collection;
|
||||
}
|
||||
|
||||
public function get(string $slug): CategoryResource
|
||||
@@ -32,12 +38,19 @@ class CategoryRepository implements CategoryRepositoryInterface
|
||||
->query()
|
||||
->where('slug', $slug)
|
||||
->firstOrFail();
|
||||
|
||||
if (!$this->auth)
|
||||
$category->visibled();
|
||||
|
||||
return new CategoryResource($category);
|
||||
}
|
||||
|
||||
public function update(Category $category, array $data = []): bool
|
||||
{
|
||||
$data = $this->parseToArray($data);
|
||||
if (!$category->default && isset($data['default']) && $data['default'] === true)
|
||||
$this->unsetDefault();
|
||||
|
||||
return $category
|
||||
->update($data);
|
||||
}
|
||||
@@ -45,11 +58,22 @@ class CategoryRepository implements CategoryRepositoryInterface
|
||||
public function create(array $data = []): Category
|
||||
{
|
||||
$data = $this->parseToArray($data);
|
||||
if (isset($data['default']) && $data['default'] === true)
|
||||
$this->unsetDefault();
|
||||
|
||||
return $this->category
|
||||
->query()
|
||||
->create($data);
|
||||
}
|
||||
|
||||
private function unsetDefault(): void
|
||||
{
|
||||
$this->category
|
||||
->query()
|
||||
->where('default', true)
|
||||
->first()?->update(['default' => false]);
|
||||
}
|
||||
|
||||
private function parseToArray(array $data = []): array
|
||||
{
|
||||
$toSave = [];
|
||||
@@ -58,10 +82,21 @@ class CategoryRepository implements CategoryRepositoryInterface
|
||||
$toSave['name'] = $data['name'];
|
||||
if (isset($data['slug']) && !empty($data['slug']))
|
||||
$toSave['slug'] = $data['slug'];
|
||||
if (isset($data['default']) && is_bool($data['default']))
|
||||
$toSave['default'] = $data['default'];
|
||||
if (isset($data['visible']) && is_bool($data['visible']))
|
||||
$toSave['visible'] = $data['visible'];
|
||||
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;
|
||||
|
||||
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;
|
||||
|
||||
return $toSave;
|
||||
}
|
||||
|
Reference in New Issue
Block a user