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