|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Auth; |
|
6
|
|
|
use Illuminate\Http\Request; |
|
7
|
|
|
use App\Thread; |
|
8
|
|
|
use App\ThreadTopic; |
|
9
|
|
|
|
|
10
|
|
|
class ThreadAskController extends Controller |
|
11
|
|
|
{ |
|
12
|
|
|
public function __construct() |
|
13
|
|
|
{ |
|
14
|
|
|
$this->middleware('auth', ['except' => [ |
|
15
|
|
|
'index', 'show' |
|
16
|
|
|
]]); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Display a listing of the resource. |
|
22
|
|
|
* |
|
23
|
|
|
* @return \Illuminate\Http\Response |
|
24
|
|
|
*/ |
|
25
|
|
|
public function index() |
|
26
|
|
|
{ |
|
27
|
|
|
$threads = Thread::orderBy('created_at', 'desc')->paginate(5); |
|
28
|
|
|
$data = [ |
|
29
|
|
|
'threads' => $threads |
|
30
|
|
|
]; |
|
31
|
|
|
return view('ask-index')->with('data', $data); |
|
|
|
|
|
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Show the form for creating a new resource. |
|
36
|
|
|
* |
|
37
|
|
|
* @return \Illuminate\Http\Response |
|
38
|
|
|
*/ |
|
39
|
|
|
public function create() |
|
40
|
|
|
{ |
|
41
|
|
|
$threads = Thread::orderBy('created_at', 'desc')->paginate(5); |
|
42
|
|
|
$data = [ |
|
43
|
|
|
'threads' => $threads |
|
44
|
|
|
]; |
|
45
|
|
|
return view('ask-form')->with('data', $data); |
|
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Store a newly created resource in storage. |
|
50
|
|
|
* |
|
51
|
|
|
* @param Request $request |
|
52
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
|
53
|
|
|
* @throws \Illuminate\Validation\ValidationException |
|
54
|
|
|
*/ |
|
55
|
|
|
public function store(Request $request) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->validate($request, [ |
|
58
|
|
|
'topic' => 'required|min:10', |
|
59
|
|
|
'question' => 'required|min:100' |
|
60
|
|
|
]); |
|
61
|
|
|
|
|
62
|
|
|
$topic = $this->addTopic($request->input('topic')); |
|
63
|
|
|
|
|
64
|
|
|
if($topic != null) { |
|
65
|
|
|
$thread = new Thread; |
|
66
|
|
|
$thread->user_id = $this->currentUser()->id; |
|
67
|
|
|
$thread->id_topic = $topic->id; |
|
68
|
|
|
$thread->question = $request->input('question'); |
|
69
|
|
|
|
|
70
|
|
|
if($thread->save()) { |
|
71
|
|
|
return redirect(route('user.thread.index'))->with('success', 'Pertanyaan dikirim !'); |
|
72
|
|
|
} |
|
73
|
|
|
return redirect()->back()->with('failed', 'Gagal mengirim pertanyaan, silahkan coba lagi nanti.'); |
|
74
|
|
|
} |
|
75
|
|
|
return redirect()->back()->with('failed', 'Gagal mengirim pertanyaan, silahkan coba lagi nanti.'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Display the specified resource. |
|
80
|
|
|
* |
|
81
|
|
|
* @param int $id |
|
82
|
|
|
* @return \Illuminate\Http\Response |
|
83
|
|
|
*/ |
|
84
|
|
|
public function show($id) |
|
85
|
|
|
{ |
|
86
|
|
|
$thread = Thread::find($id); |
|
87
|
|
|
if(!$thread) { |
|
88
|
|
|
abort(404); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$threads = Thread::orderBy('created_at', 'desc')->paginate(5); |
|
92
|
|
|
$data = [ |
|
93
|
|
|
'thread' => $thread, |
|
94
|
|
|
'threads' => $threads |
|
95
|
|
|
]; |
|
96
|
|
|
return view('ask-view')->with('data', $data); |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Show the form for editing the specified resource. |
|
101
|
|
|
* |
|
102
|
|
|
* @param int $id |
|
103
|
|
|
* @return \Illuminate\Http\Response |
|
104
|
|
|
*/ |
|
105
|
|
|
public function edit($id) |
|
106
|
|
|
{ |
|
107
|
|
|
$user = $this->currentUser(); |
|
108
|
|
|
$thread = Thread::find($id); |
|
109
|
|
|
if($thread->user_id != $user->id) { |
|
110
|
|
|
return redirect()->back()->with('warning', 'Anda tidak berhak mengakses laman tersebut.'); |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
if($thread->doctor_id != null) { |
|
114
|
|
|
return redirect()->back()->with('warning', 'Tidak dapat mengubah pertanyaan karena sudah terjawab, silahkan tanyakan pertanyaan baru.'); |
|
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
$threads = Thread::orderBy('created_at', 'desc')->paginate(5); |
|
117
|
|
|
$data = [ |
|
118
|
|
|
'thread' => $thread, |
|
119
|
|
|
'threads' => $threads |
|
120
|
|
|
]; |
|
121
|
|
|
|
|
122
|
|
|
return view('ask-edit')->with('data', $data); |
|
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Update the specified resource in storage. |
|
128
|
|
|
* |
|
129
|
|
|
* @param Request $request |
|
130
|
|
|
* @param $id |
|
131
|
|
|
* @return \Illuminate\Http\RedirectResponse |
|
132
|
|
|
* @throws \Illuminate\Validation\ValidationException |
|
133
|
|
|
*/ |
|
134
|
|
|
public function update(Request $request, $id) |
|
135
|
|
|
{ |
|
136
|
|
|
$user = $this->currentUser(); |
|
137
|
|
|
$thread = Thread::find($id); |
|
138
|
|
|
if($thread->user_id != $user->id) { |
|
139
|
|
|
return redirect()->back()->with('warning', 'Anda tidak berhak mengubah ulasan tersebut.'); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
$this->validate($request, [ |
|
143
|
|
|
'topic' => 'required|min:10', |
|
144
|
|
|
'question' => 'required|min:100' |
|
145
|
|
|
]); |
|
146
|
|
|
|
|
147
|
|
|
$thread->topic = $request->input('topic'); |
|
148
|
|
|
$thread->question = $request->input('question'); |
|
149
|
|
|
if($thread->save()) { |
|
150
|
|
|
return redirect(route('user.thread.show', $thread->id))->with('success', 'Berhasil mengubah ulasan !'); |
|
151
|
|
|
} |
|
152
|
|
|
return redirect(route('user.thread.show', $thread->id))->with('success', 'Berhasil mengubah ulasan !'); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Remove the specified resource from storage. |
|
157
|
|
|
* |
|
158
|
|
|
* @param int $id |
|
159
|
|
|
* @return \Illuminate\Http\Response |
|
160
|
|
|
*/ |
|
161
|
|
|
public function destroy($id) |
|
162
|
|
|
{ |
|
163
|
|
|
$user = $this->currentUser(); |
|
164
|
|
|
$thread = Thread::find($id); |
|
165
|
|
|
if($thread->user_id != $user->id) { |
|
166
|
|
|
return redirect()->back()->with('warning', 'Anda tidak berhak menghapus ulasan tersebut.'); |
|
|
|
|
|
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
if($thread->delete() && $this->deleteTopic($thread->id_topic)) { |
|
170
|
|
|
return redirect(route('user.profile'))->with('success', 'Ulasan dihapus !'); |
|
|
|
|
|
|
171
|
|
|
} |
|
172
|
|
|
return redirect()->back()->with('failed', 'Gagal menghapus ulasan.'); |
|
|
|
|
|
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
|
|
176
|
|
|
private function currentUser() { |
|
177
|
|
|
return Auth::guard('web')->user(); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @param string $topic |
|
182
|
|
|
* @return ThreadTopic|null |
|
183
|
|
|
*/ |
|
184
|
|
|
private function addTopic(string $topic) { |
|
185
|
|
|
$new = new ThreadTopic; |
|
186
|
|
|
$new->topic_name = $topic; |
|
187
|
|
|
if($new->save()) { |
|
188
|
|
|
return $new; |
|
189
|
|
|
} |
|
190
|
|
|
return null; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Delete topic with given id |
|
195
|
|
|
* |
|
196
|
|
|
* @param int $id |
|
197
|
|
|
* @return bool |
|
198
|
|
|
*/ |
|
199
|
|
|
private function deleteTopic($id) { |
|
200
|
|
|
$topic = ThreadTopic::find($id); |
|
201
|
|
|
if($topic->delete()) { |
|
202
|
|
|
return true; |
|
203
|
|
|
} |
|
204
|
|
|
return false; |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|