1 | <?php |
||
2 | |||
3 | namespace App\Http\Controllers; |
||
4 | |||
5 | use App\Common; |
||
6 | use App\Thread; |
||
7 | use Illuminate\Http\Request; |
||
8 | use Illuminate\Support\Facades\Auth; |
||
9 | |||
10 | class ThreadAnswerController extends Controller |
||
11 | { |
||
12 | public function __construct() |
||
13 | { |
||
14 | $this->middleware('auth:doctor'); |
||
15 | } |
||
16 | |||
17 | /** |
||
18 | * Display a listing of the resource. |
||
19 | * |
||
20 | * @return \Illuminate\Http\Response |
||
21 | */ |
||
22 | public function index($id) |
||
23 | { |
||
24 | $doctor = $this->currentUser(); |
||
25 | if($doctor->id != $id) { |
||
26 | return redirect()->back()->with('warning', 'Anda tidak berhak mengakses laman tersebut.'); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
27 | } |
||
28 | |||
29 | $data = [ |
||
30 | 'doctor' => $doctor, |
||
31 | 'thread' => Thread::where('doctor_id', $doctor->id)->paginate(15) |
||
32 | ]; |
||
33 | return view('pages.profile-thread')->with('data', $data); |
||
0 ignored issues
–
show
|
|||
34 | } |
||
35 | |||
36 | /** |
||
37 | * Store a newly created resource in storage. |
||
38 | * |
||
39 | * @param Request $request |
||
40 | * @param $id |
||
41 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
||
42 | * @throws \Illuminate\Validation\ValidationException |
||
43 | */ |
||
44 | public function store(Request $request, $id) |
||
45 | { |
||
46 | $this->validate($request, [ |
||
47 | 'answer' => 'required|min:150' |
||
48 | ]); |
||
49 | |||
50 | $doctor = $this->currentUser(); |
||
51 | $thread = Thread::find($id); |
||
52 | |||
53 | $thread->doctor_id = $doctor->id; |
||
54 | $thread->answer = $request->input('answer'); |
||
55 | $thread->status = true; |
||
56 | |||
57 | if($thread->save()) { |
||
58 | |||
59 | $log = Common::registerLog([ |
||
0 ignored issues
–
show
|
|||
60 | 'action' => "menjawab diskusi oleh ", |
||
61 | 'target' => 'thread', |
||
62 | 'prefix' => 't-answer', |
||
63 | 'target_id' => $thread->id, |
||
64 | 'actor' => session('guard'), |
||
65 | 'actor_id' => Common::currentUser(session('guard'))->id |
||
66 | ]); |
||
67 | |||
68 | return redirect(route('doctor.thread.show', $id))->with('success', 'Jawaban terkirim !'); |
||
69 | } |
||
70 | return redirect()->back()->with('failed', 'Gagal mengirim jawaban.'); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Show the form for editing the specified resource. |
||
75 | * |
||
76 | * @param $id |
||
77 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
78 | */ |
||
79 | public function edit($id) |
||
80 | { |
||
81 | $thread = Thread::find($id); |
||
82 | if(!$thread) { |
||
83 | abort(404); |
||
84 | } |
||
85 | |||
86 | $doctor = $this->currentUser(); |
||
87 | $data = [ |
||
88 | 'doctor' => $doctor, |
||
89 | 'thread' => $thread |
||
90 | ]; |
||
91 | |||
92 | return view('pages.ext.edit-thread')->with('data', $data); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Update the specified resource in storage. |
||
97 | * |
||
98 | * @param Request $request |
||
99 | * @param $id |
||
100 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
||
101 | * @throws \Illuminate\Validation\ValidationException |
||
102 | */ |
||
103 | public function update(Request $request, $id) |
||
104 | { |
||
105 | $this->validate($request, [ |
||
106 | 'answer' => 'required|min:150' |
||
107 | ]); |
||
108 | |||
109 | $thread = Thread::find($id); |
||
110 | $thread->answer = $request->input('answer'); |
||
111 | $thread->status = true; |
||
112 | |||
113 | if($thread->save()) { |
||
114 | return redirect(route('doctor.thread.show', $id))->with('success', 'Perubahan jawaban terkirim !'); |
||
115 | } |
||
116 | return redirect()->back()->with('failed', 'Gagal mengirim perubahan jawaban.'); |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Remove the specified resource from storage. |
||
121 | * |
||
122 | * @param int $id |
||
123 | * @return \Illuminate\Http\Response |
||
124 | */ |
||
125 | public function destroy($id) |
||
126 | { |
||
127 | $thread = Thread::find($id); |
||
128 | if($thread->doctor_id != $this->currentUser()->id) { |
||
129 | return redirect()->back()->with('warning', 'Anda tidak berhak menghapus jawaban untuk diskusi tersebut.'); |
||
0 ignored issues
–
show
|
|||
130 | } |
||
131 | |||
132 | $thread->doctor_id = null; |
||
133 | $thread->answer = null; |
||
134 | $thread->status = false; |
||
135 | if($thread->save()) { |
||
136 | return redirect()->back()->with('success', 'Jawaban dihapus !'); |
||
0 ignored issues
–
show
|
|||
137 | } |
||
138 | return redirect()->back()->with('failed', 'Gagal mengahapus jawaban.'); |
||
0 ignored issues
–
show
|
|||
139 | } |
||
140 | |||
141 | /** |
||
142 | * @return mixed |
||
143 | */ |
||
144 | private function currentUser() |
||
145 | { |
||
146 | return Auth::guard('doctor')->user(); |
||
147 | } |
||
148 | } |
||
149 |