1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PHPHub\Http\ApiControllers; |
4
|
|
|
|
5
|
|
|
use Auth; |
6
|
|
|
use Dingo\Api\Exception\StoreResourceFailedException; |
7
|
|
|
use Gate; |
8
|
|
|
use PHPHub\Repositories\Criteria\FilterManager; |
9
|
|
|
use PHPHub\Repositories\TopicRepositoryInterface; |
10
|
|
|
use PHPHub\Topic; |
11
|
|
|
use PHPHub\Transformers\TopicTransformer; |
12
|
|
|
use Illuminate\Http\Request; |
13
|
|
|
use Prettus\Validator\Exceptions\ValidatorException; |
14
|
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; |
15
|
|
|
|
16
|
|
|
class TopicsController extends Controller |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var TopicRepositoryInterface |
20
|
|
|
*/ |
21
|
|
|
private $topics; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* TopicController constructor. |
25
|
|
|
* |
26
|
|
|
* @param TopicRepositoryInterface $repository |
27
|
|
|
*/ |
28
|
|
|
public function __construct(TopicRepositoryInterface $repository) |
29
|
|
|
{ |
30
|
|
|
$this->topics = $repository; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* 默认帖子列表. |
35
|
|
|
* |
36
|
|
|
* @return \Illuminate\Http\Response |
37
|
|
|
*/ |
38
|
|
|
public function index() |
39
|
|
|
{ |
40
|
|
|
return $this->commonIndex(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* 获取指定用户发布的帖子. |
45
|
|
|
* |
46
|
|
|
* @param $user_id |
47
|
|
|
* |
48
|
|
|
* @return \Dingo\Api\Http\Response |
49
|
|
|
*/ |
50
|
|
|
public function indexByUserId($user_id) |
51
|
|
|
{ |
52
|
|
|
$this->topics->byUserId($user_id); |
53
|
|
|
|
54
|
|
|
return $this->commonIndex(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* 获取指定节点下的帖子. |
59
|
|
|
* |
60
|
|
|
* @param $node_id |
61
|
|
|
* |
62
|
|
|
* @return \Dingo\Api\Http\Response |
63
|
|
|
*/ |
64
|
|
|
public function indexByNodeId($node_id) |
65
|
|
|
{ |
66
|
|
|
$this->topics->byNodeId($node_id); |
67
|
|
|
|
68
|
|
|
return $this->commonIndex(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* 用户收藏的帖子列表. |
73
|
|
|
* |
74
|
|
|
* @param $user_id |
75
|
|
|
* |
76
|
|
|
* @return \Dingo\Api\Http\Response |
77
|
|
|
*/ |
78
|
|
View Code Duplication |
public function indexByUserFavorite($user_id) |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$this->registerListApiIncludes(); |
81
|
|
|
|
82
|
|
|
$data = $this->topics |
83
|
|
|
->favoriteTopicsWithPaginator($user_id, |
84
|
|
|
['id', 'title', 'is_excellent', 'reply_count', 'updated_at', 'created_at']); |
85
|
|
|
|
86
|
|
|
return $this->response()->paginator($data, new TopicTransformer()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* 用户关注的帖子列表. |
91
|
|
|
* |
92
|
|
|
* @param $user_id |
93
|
|
|
* |
94
|
|
|
* @return \Dingo\Api\Http\Response |
95
|
|
|
*/ |
96
|
|
View Code Duplication |
public function indexByUserAttention($user_id) |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
$this->registerListApiIncludes(); |
99
|
|
|
|
100
|
|
|
$data = $this->topics |
101
|
|
|
->attentionTopicsWithPaginator($user_id, |
102
|
|
|
['id', 'title', 'is_excellent', 'reply_count', 'updated_at', 'created_at']); |
103
|
|
|
|
104
|
|
|
return $this->response()->paginator($data, new TopicTransformer()); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* 发布新帖子. |
109
|
|
|
* |
110
|
|
|
* @param \Illuminate\Http\Request $request |
111
|
|
|
* |
112
|
|
|
* @return \Illuminate\Http\Response |
113
|
|
|
*/ |
114
|
|
View Code Duplication |
public function store(Request $request) |
|
|
|
|
115
|
|
|
{ |
116
|
|
|
try { |
117
|
|
|
$topic = $this->topics->create($request->all()); |
118
|
|
|
|
119
|
|
|
return $this->response()->item($topic, new TopicTransformer()); |
120
|
|
|
} catch (ValidatorException $e) { |
121
|
|
|
throw new StoreResourceFailedException('Could not create new topic.', $e->getMessageBag()->all()); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* 获取指定帖子的详细. |
127
|
|
|
* |
128
|
|
|
* @param int $id |
129
|
|
|
* |
130
|
|
|
* @return \Illuminate\Http\Response |
131
|
|
|
*/ |
132
|
|
|
public function show($id) |
133
|
|
|
{ |
134
|
|
|
$this->topics->addAvailableInclude('user', ['name', 'avatar']); |
135
|
|
|
|
136
|
|
|
$topic = $this->topics |
137
|
|
|
->autoWith() |
138
|
|
|
->autoWithRootColumns(array_diff(Topic::$includable, ['body', 'body_original', 'excerpt'])) |
139
|
|
|
->find($id); |
140
|
|
|
|
141
|
|
|
if (Auth::check()) { |
142
|
|
|
$topic->favorite = $this->topics->userFavorite($topic->id, Auth::id()); |
143
|
|
|
$topic->attention = $this->topics->userAttention($topic->id, Auth::id()); |
144
|
|
|
$topic->vote_up = $this->topics->userUpVoted($topic->id, Auth::id()); |
145
|
|
|
$topic->vote_down = $this->topics->userDownVoted($topic->id, Auth::id()); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $this->response()->item($topic, new TopicTransformer()); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* 更新帖子. |
153
|
|
|
* |
154
|
|
|
* @param \Illuminate\Http\Request $request |
155
|
|
|
* @param int $id |
156
|
|
|
* |
157
|
|
|
* @return \Illuminate\Http\Response |
158
|
|
|
*/ |
159
|
|
|
public function update(Request $request, $id) |
|
|
|
|
160
|
|
|
{ |
161
|
|
|
// |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* 删除帖子. |
166
|
|
|
* |
167
|
|
|
* @param int $id |
168
|
|
|
* |
169
|
|
|
* @return \Illuminate\Http\Response |
170
|
|
|
*/ |
171
|
|
|
public function destroy($id) |
172
|
|
|
{ |
173
|
|
|
$topic = $this->topics->find($id); |
174
|
|
|
|
175
|
|
|
if (Gate::denies('delete', $topic)) { |
176
|
|
|
throw new AccessDeniedHttpException(); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
$this->topics->delete($id); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* 支持帖子. |
184
|
|
|
* |
185
|
|
|
* @param $id |
186
|
|
|
* |
187
|
|
|
* @return \Illuminate\Http\Response |
188
|
|
|
*/ |
189
|
|
|
public function voteUp($id) |
190
|
|
|
{ |
191
|
|
|
$topic = $this->topics->find($id); |
192
|
|
|
|
193
|
|
|
return response([ |
|
|
|
|
194
|
|
|
'vote-up' => $this->topics->voteUp($topic), |
195
|
|
|
'vote_count' => $topic->vote_count, |
196
|
|
|
]); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* 反对帖子. |
201
|
|
|
* |
202
|
|
|
* @param $id |
203
|
|
|
* |
204
|
|
|
* @return \Illuminate\Http\Response |
205
|
|
|
*/ |
206
|
|
|
public function voteDown($id) |
207
|
|
|
{ |
208
|
|
|
$topic = $this->topics->find($id); |
209
|
|
|
|
210
|
|
|
return response([ |
|
|
|
|
211
|
|
|
'vote-down' => $this->topics->voteDown($topic), |
212
|
|
|
'vote_count' => $topic->vote_count, |
213
|
|
|
]); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* 所有帖子列表接口的通用部分. |
218
|
|
|
* |
219
|
|
|
* @return \Dingo\Api\Http\Response |
220
|
|
|
*/ |
221
|
|
|
protected function commonIndex() |
222
|
|
|
{ |
223
|
|
|
FilterManager::addFilter('newest'); |
224
|
|
|
$this->registerListApiIncludes(); |
225
|
|
|
|
226
|
|
|
$data = $this->topics |
227
|
|
|
->autoWith() |
228
|
|
|
->autoWithRootColumns([ |
229
|
|
|
'id', |
230
|
|
|
'title', |
231
|
|
|
'is_excellent', |
232
|
|
|
'reply_count', |
233
|
|
|
'updated_at', |
234
|
|
|
'created_at', |
235
|
|
|
'vote_count', |
236
|
|
|
]) |
237
|
|
|
->paginate(per_page()); |
238
|
|
|
|
239
|
|
|
return $this->response()->paginator($data, new TopicTransformer()); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* 用于客户端的帖子详细 Web View. |
244
|
|
|
* |
245
|
|
|
* @param $id |
246
|
|
|
* |
247
|
|
|
* @return \Illuminate\View\View |
248
|
|
|
*/ |
249
|
|
|
public function showWebView($id) |
250
|
|
|
{ |
251
|
|
|
$topic = $this->topics->find($id, ['title', 'body', 'created_at', 'vote_count']); |
252
|
|
|
|
253
|
|
|
return view('api_web_views.topic', compact('topic')); |
|
|
|
|
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* 收藏帖子. |
258
|
|
|
* |
259
|
|
|
* @param $topic_id |
260
|
|
|
* |
261
|
|
|
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response |
262
|
|
|
*/ |
263
|
|
View Code Duplication |
public function favorite($topic_id) |
|
|
|
|
264
|
|
|
{ |
265
|
|
|
try { |
266
|
|
|
$this->topics->favorite($topic_id, Auth::id()); |
267
|
|
|
} catch (\Exception $e) { |
268
|
|
|
$filed = true; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
return response([ |
|
|
|
|
272
|
|
|
'status' => isset($filed) ? false : true, |
273
|
|
|
]); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* 取消收藏帖子. |
278
|
|
|
* |
279
|
|
|
* @param $topic_id |
280
|
|
|
* |
281
|
|
|
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response |
282
|
|
|
*/ |
283
|
|
View Code Duplication |
public function unFavorite($topic_id) |
|
|
|
|
284
|
|
|
{ |
285
|
|
|
try { |
286
|
|
|
$this->topics->unFavorite($topic_id, Auth::id()); |
287
|
|
|
} catch (\Exception $e) { |
288
|
|
|
$filed = true; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
return response([ |
|
|
|
|
292
|
|
|
'status' => isset($filed) ? false : true, |
293
|
|
|
]); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* 关注帖子. |
298
|
|
|
* |
299
|
|
|
* @param $topic_id |
300
|
|
|
* |
301
|
|
|
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response |
302
|
|
|
*/ |
303
|
|
View Code Duplication |
public function attention($topic_id) |
|
|
|
|
304
|
|
|
{ |
305
|
|
|
try { |
306
|
|
|
$this->topics->attention($topic_id, Auth::id()); |
307
|
|
|
} catch (\Exception $e) { |
308
|
|
|
$filed = true; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
return response([ |
|
|
|
|
312
|
|
|
'status' => isset($filed) ? false : true, |
313
|
|
|
]); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* 取消关注帖子. |
318
|
|
|
* |
319
|
|
|
* @param $topic_id |
320
|
|
|
* |
321
|
|
|
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response |
322
|
|
|
*/ |
323
|
|
View Code Duplication |
public function unAttention($topic_id) |
|
|
|
|
324
|
|
|
{ |
325
|
|
|
try { |
326
|
|
|
$this->topics->unAttention($topic_id, Auth::id()); |
327
|
|
|
} catch (\Exception $e) { |
328
|
|
|
$filed = true; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
return response([ |
|
|
|
|
332
|
|
|
'status' => isset($filed) ? false : true, |
333
|
|
|
]); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* 注册帖子列表接口通用的引入项. |
338
|
|
|
*/ |
339
|
|
|
protected function registerListApiIncludes() |
340
|
|
|
{ |
341
|
|
|
$this->topics->addAvailableInclude('user', ['name', 'avatar']); |
342
|
|
|
$this->topics->addAvailableInclude('last_reply_user', ['name']); |
343
|
|
|
$this->topics->addAvailableInclude('node', ['name']); |
344
|
|
|
} |
345
|
|
|
} |
346
|
|
|
|
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.