1
|
|
|
<?php |
2
|
|
|
namespace Xetaravel\Http\Controllers\Admin\Blog; |
3
|
|
|
|
4
|
|
|
use Illuminate\Http\RedirectResponse; |
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Illuminate\View\View; |
7
|
|
|
use Xetaravel\Http\Controllers\Admin\Controller; |
8
|
|
|
use Xetaravel\Models\Category; |
9
|
|
|
use Xetaravel\Models\Repositories\CategoryRepository; |
10
|
|
|
use Xetaravel\Models\Validators\CategoryValidator; |
11
|
|
|
|
12
|
|
View Code Duplication |
class CategoryController extends Controller |
|
|
|
|
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Constructor. |
16
|
|
|
*/ |
17
|
|
|
public function __construct() |
18
|
|
|
{ |
19
|
|
|
parent::__construct(); |
20
|
|
|
|
21
|
|
|
$this->breadcrumbs->addCrumb('Blog', route('admin.blog.article.index')); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Show all categories. |
26
|
|
|
* |
27
|
|
|
* @return \Illuminate\View\View |
28
|
|
|
*/ |
29
|
|
|
public function index(): View |
30
|
|
|
{ |
31
|
|
|
$categories = Category::paginate(config('xetaravel.pagination.blog.article_per_page')); |
32
|
|
|
|
33
|
|
|
$this->breadcrumbs->addCrumb('Manage Categories', route('admin.blog.category.index')); |
34
|
|
|
|
35
|
|
|
return view('Admin::Blog.category.index', ['categories' => $categories, 'breadcrumbs' => $this->breadcrumbs]); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Show the caterory create form. |
40
|
|
|
* |
41
|
|
|
* @return \Illuminate\View\View |
42
|
|
|
*/ |
43
|
|
|
public function showCreateForm(): View |
44
|
|
|
{ |
45
|
|
|
$breadcrumbs = $this->breadcrumbs |
|
|
|
|
46
|
|
|
->addCrumb('Manage Categories', route('admin.blog.category.index')) |
47
|
|
|
->addCrumb("Create", route('admin.blog.category.create')); |
48
|
|
|
|
49
|
|
|
return view('Admin::Blog.category.create', ['breadcrumbs' => $this->breadcrumbs]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Handle a category create request for the application. |
54
|
|
|
* |
55
|
|
|
* @param \Illuminate\Http\Request $request |
56
|
|
|
* |
57
|
|
|
* @return \Illuminate\Http\RedirectResponse |
58
|
|
|
*/ |
59
|
|
|
public function create(Request $request): RedirectResponse |
60
|
|
|
{ |
61
|
|
|
CategoryValidator::create($request->all())->validate(); |
62
|
|
|
CategoryRepository::create($request->all()); |
63
|
|
|
|
64
|
|
|
return redirect() |
65
|
|
|
->route('admin.blog.category.index') |
66
|
|
|
->with('success', 'Your category has been created successfully !'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Show the category update form. |
71
|
|
|
* |
72
|
|
|
* @param string $slug The slug of the category. |
73
|
|
|
* @param int $id The id of the category. |
74
|
|
|
* |
75
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View |
|
|
|
|
76
|
|
|
*/ |
77
|
|
|
public function showUpdateForm(string $slug, int $id) |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
$category = Category::findOrFail($id); |
80
|
|
|
|
81
|
|
|
$breadcrumbs = $this->breadcrumbs |
82
|
|
|
->addCrumb('Manage Categories', route('admin.blog.category.index')) |
83
|
|
|
->addCrumb( |
84
|
|
|
"Update : " . e(str_limit($category->title, 30)), |
85
|
|
|
route( |
86
|
|
|
'admin.blog.category.index', |
87
|
|
|
['slug' => $category->slug, 'id' => $category->id] |
88
|
|
|
) |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
return view('Admin::Blog.category.update', compact('category', 'breadcrumbs')); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Handle an category update request for the application. |
96
|
|
|
* |
97
|
|
|
* @param \Illuminate\Http\Request $request |
98
|
|
|
* @param int $id The id of the category. |
99
|
|
|
* |
100
|
|
|
* @return \Illuminate\Http\RedirectResponse |
101
|
|
|
*/ |
102
|
|
|
public function update(Request $request, int $id): RedirectResponse |
103
|
|
|
{ |
104
|
|
|
$category = Category::findOrFail($id); |
105
|
|
|
|
106
|
|
|
CategoryValidator::update($request->all(), $id)->validate(); |
107
|
|
|
CategoryRepository::update($request->all(), $category); |
108
|
|
|
|
109
|
|
|
return redirect() |
110
|
|
|
->route('admin.blog.category.index') |
111
|
|
|
->with('success', 'Your category has been updated successfully !'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Handle the delete request for the category. |
116
|
|
|
* |
117
|
|
|
* @param int $id The id of the category to delete. |
118
|
|
|
* |
119
|
|
|
* @return \Illuminate\Http\RedirectResponse |
120
|
|
|
*/ |
121
|
|
|
public function delete(int $id): RedirectResponse |
122
|
|
|
{ |
123
|
|
|
$category = Category::findOrFail($id); |
124
|
|
|
|
125
|
|
|
if ($category->delete()) { |
126
|
|
|
return redirect() |
127
|
|
|
->route('admin.blog.category.index') |
128
|
|
|
->with('success', 'This category has been deleted successfully !'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return redirect() |
132
|
|
|
->route('admin.blog.category.index') |
133
|
|
|
->with('danger', 'An error occurred while deleting this category !'); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.