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
|
|
|
class CategoryController extends Controller |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Show all categories. |
16
|
|
|
* |
17
|
|
|
* @return \Illuminate\View\View |
18
|
|
|
*/ |
19
|
|
View Code Duplication |
public function index(): View |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
$categories = Category::paginate(config('xetaravel.pagination.blog.article_per_page')); |
22
|
|
|
|
23
|
|
|
$this->breadcrumbs->addCrumb('Manage Categories', route('admin.blog.category.index')); |
|
|
|
|
24
|
|
|
|
25
|
|
|
return view('Admin::Blog.category.index', ['categories' => $categories, 'breadcrumbs' => $this->breadcrumbs]); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Show the caterory create form. |
30
|
|
|
* |
31
|
|
|
* @return \Illuminate\View\View |
32
|
|
|
*/ |
33
|
|
|
public function showCreateForm(): View |
34
|
|
|
{ |
35
|
|
|
$breadcrumbs = $this->breadcrumbs |
|
|
|
|
36
|
|
|
->addCrumb('Manage Categories', route('admin.blog.article.index')) |
37
|
|
|
->addCrumb("Create", route('admin.blog.article.create')); |
38
|
|
|
|
39
|
|
|
return view('Admin::Blog.category.create', ['breadcrumbs' => $this->breadcrumbs]); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Handle a category create request for the application. |
44
|
|
|
* |
45
|
|
|
* @param \Illuminate\Http\Request $request |
46
|
|
|
* |
47
|
|
|
* @return \Illuminate\Http\RedirectResponse |
48
|
|
|
*/ |
49
|
|
View Code Duplication |
public function create(Request $request): RedirectResponse |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
CategoryValidator::create($request->all())->validate(); |
52
|
|
|
|
53
|
|
|
if (CategoryRepository::create($request->all())) { |
54
|
|
|
return redirect() |
55
|
|
|
->route('admin.blog.category.index') |
56
|
|
|
->with('success', 'Your category has been created successfully !'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return redirect() |
60
|
|
|
->route('admin.blog.category.index') |
61
|
|
|
->with('danger', 'An error occurred while creating your category !'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Show the category update form. |
66
|
|
|
* |
67
|
|
|
* @param string $slug The slug of the category. |
68
|
|
|
* @param int $id The id of the category. |
69
|
|
|
* |
70
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View |
|
|
|
|
71
|
|
|
*/ |
72
|
|
|
public function showUpdateForm(string $slug, int $id) |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
$category = Category::find($id); |
75
|
|
|
|
76
|
|
|
if (is_null($category)) { |
77
|
|
|
return redirect() |
78
|
|
|
->route('admin.blog.category.index') |
79
|
|
|
->with('danger', 'This category doesn\'t exist or has been deleted !'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$breadcrumbs = $this->breadcrumbs |
83
|
|
|
->addCrumb('Manage Categories', route('admin.blog.category.index')) |
84
|
|
|
->addCrumb( |
85
|
|
|
"Update : " . e(str_limit($category->title, 30)), |
86
|
|
|
route( |
87
|
|
|
'admin.blog.category.index', |
88
|
|
|
['slug' => $category->slug, 'id' => $category->id] |
89
|
|
|
) |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
return view('Admin::Blog.category.update', compact('category', 'breadcrumbs')); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Handle an category update request for the application. |
97
|
|
|
* |
98
|
|
|
* @param \Illuminate\Http\Request $request |
99
|
|
|
* @param int $id The id of the category. |
100
|
|
|
* |
101
|
|
|
* @return \Illuminate\Http\RedirectResponse |
102
|
|
|
*/ |
103
|
|
View Code Duplication |
public function update(Request $request, int $id): RedirectResponse |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
CategoryValidator::update($request->all(), $id)->validate(); |
106
|
|
|
|
107
|
|
|
$category = Category::find($id); |
108
|
|
|
|
109
|
|
|
if (CategoryRepository::update($request->all(), $category)) { |
110
|
|
|
return redirect() |
111
|
|
|
->route('admin.blog.category.index') |
112
|
|
|
->with('success', 'Your category has been updated successfully !'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return redirect() |
116
|
|
|
->route('admin.blog.category.index') |
117
|
|
|
->with('danger', 'An error occurred while updating your category !'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Handle the delete request for the category. |
122
|
|
|
* |
123
|
|
|
* @param int $id The id of the category to delete. |
124
|
|
|
* |
125
|
|
|
* @return \Illuminate\Http\RedirectResponse |
126
|
|
|
*/ |
127
|
|
View Code Duplication |
public function delete(int $id): RedirectResponse |
|
|
|
|
128
|
|
|
{ |
129
|
|
|
$category = Category::find($id); |
130
|
|
|
|
131
|
|
|
if (is_null($category)) { |
132
|
|
|
return redirect() |
133
|
|
|
->route('admin.blog.category.index') |
134
|
|
|
->with('danger', 'This category doesn\'t exist or has already been deleted !'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
if ($category->delete()) { |
138
|
|
|
return redirect() |
139
|
|
|
->route('admin.blog.category.index') |
140
|
|
|
->with('success', 'This category has been deleted successfully !'); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return redirect() |
144
|
|
|
->route('admin.blog.category.index') |
145
|
|
|
->with('danger', 'An error occurred while deleting this category !'); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
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.