1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Web; |
4
|
|
|
|
5
|
|
|
use App\Entities\Topic; |
6
|
|
|
use App\Entities\User; |
7
|
|
|
use App\Exceptions\WebException; |
8
|
|
|
use App\Http\Controllers\Controller; |
9
|
|
|
use App\Http\Requests\Topic\ReplyStoreRequest; |
10
|
|
|
use App\Http\Requests\Topic\StoreRequest; |
11
|
|
|
use App\Repositories\Criteria\OrderBy; |
12
|
|
|
use App\Repositories\Criteria\Where; |
13
|
|
|
use App\Repositories\TopicRepository; |
14
|
|
|
use App\Services\Topic\Validator\UserValidator; |
15
|
|
|
use Illuminate\Database\Eloquent\Collection; |
16
|
|
|
|
17
|
|
|
class TopicController extends Controller |
18
|
|
|
{ |
19
|
|
|
private $repository; |
20
|
|
|
|
21
|
|
|
public function __construct(TopicRepository $repository) |
22
|
|
|
{ |
23
|
|
|
$this->repository = $repository; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function reply($id, ReplyStoreRequest $request) |
27
|
|
|
{ |
28
|
|
|
/** @var User $user */ |
29
|
|
|
$user = auth()->user(); |
30
|
|
|
try { |
31
|
|
|
app(UserValidator::class)->validate($user); |
32
|
|
|
} catch (WebException $exception) { |
33
|
|
|
return redirect(route('topic.list'))->withErrors($exception->getMessage()); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** @var Topic $topic */ |
37
|
|
|
$topic = $this->repository->find($id); |
38
|
|
|
if ($topic) { |
|
|
|
|
39
|
|
|
$reply = [ |
40
|
|
|
'user_id' => app('auth')->guard()->id(), |
41
|
|
|
'topic_id' => $id, |
42
|
|
|
'content' => $request->getBody(), |
43
|
|
|
]; |
44
|
|
|
$topic->replies()->create($reply); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return redirect(route('topic.view', ['id' => $id])); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function create() |
51
|
|
|
{ |
52
|
|
|
/** @var User $user */ |
53
|
|
|
$user = auth()->user(); |
54
|
|
|
try { |
55
|
|
|
app(UserValidator::class)->validate($user); |
56
|
|
|
} catch (WebException $exception) { |
57
|
|
|
return redirect(route('topic.list'))->withErrors($exception->getMessage()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return view('web.topic.create'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function store(StoreRequest $request) |
64
|
|
|
{ |
65
|
|
|
/** @var User $user */ |
66
|
|
|
$user = $request->user(); |
67
|
|
|
try { |
68
|
|
|
app(UserValidator::class)->validate($user); |
69
|
|
|
} catch (WebException $exception) { |
70
|
|
|
return redirect(route('topic.list'))->withErrors($exception->getMessage()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$data = [ |
74
|
|
|
'user_id' => $user->id, |
75
|
|
|
'title' => $request->getTitle(), |
76
|
|
|
'content' => $request->getBody(), |
77
|
|
|
'contest_id' => $request->getContestId(), |
78
|
|
|
'problem_id' => $request->getProblemId(), |
79
|
|
|
]; |
80
|
|
|
|
81
|
|
|
$this->repository->create($data); |
82
|
|
|
|
83
|
|
|
if ($request->filled('contest_id')) { |
84
|
|
|
return redirect(route('contest.clarify', ['contest' => $request->getContestId()])); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return redirect(route('topic.list')); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function index() |
91
|
|
|
{ |
92
|
|
|
if (request()->filled('uid')) { |
93
|
|
|
/** @var User $user */ |
94
|
|
|
$user = User::query()->where('username', request()->input('uid'))->first(); |
95
|
|
|
if ($user) { |
|
|
|
|
96
|
|
|
$this->repository->pushCriteria(new Where('user_id', $user->id)); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
if (request()->filled('pid')) { |
100
|
|
|
$this->repository->pushCriteria(new Where('problem_id', request('pid'))); |
101
|
|
|
} |
102
|
|
|
$this->repository->pushCriteria(new OrderBy('id', 'desc')); |
103
|
|
|
// 获取非比赛的clarity |
104
|
|
|
$this->repository->pushCriteria(new Where('contest_id', 1, '<')); |
105
|
|
|
/** @var Collection $topics */ |
106
|
|
|
$topics = $this->repository->paginate(request('per_page', 50)); |
|
|
|
|
107
|
|
|
$topics->load('user'); |
108
|
|
|
|
109
|
|
|
return view('web.topic.list')->with('topics', $topics); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function show($id) |
113
|
|
|
{ |
114
|
|
|
$topic = $this->repository->findOrFail($id); |
115
|
|
|
/** @var User $user */ |
116
|
|
|
$user = auth()->user(); |
117
|
|
|
|
118
|
|
|
$isUserCanReply = false; |
119
|
|
|
if ($user) { |
|
|
|
|
120
|
|
|
$isUserCanReply = app(UserValidator::class)->isUserColdDown($user); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return view('web.topic.view', [ |
124
|
|
|
'topic' => $topic, |
125
|
|
|
'isUserCanReply' => $isUserCanReply, |
126
|
|
|
]); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|