1 | <?php |
||
2 | |||
3 | namespace App\Http\Controllers; |
||
4 | |||
5 | use Illuminate\Support\Facades\DB; |
||
6 | use Illuminate\Http\Request; |
||
7 | use Carbon\Carbon; |
||
8 | use App\Hospital; |
||
9 | use App\Room; |
||
10 | |||
11 | class RoomController extends Controller |
||
12 | { |
||
13 | /** |
||
14 | * Show the form for creating a new resource. |
||
15 | * |
||
16 | * @return \Illuminate\Http\Response |
||
17 | */ |
||
18 | public function create($hospital_id) |
||
19 | { |
||
20 | $data = [ |
||
21 | 'hospital' => Hospital::find($hospital_id) |
||
22 | ]; |
||
23 | return view('pages.ext.add-room')->with('data', $data); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
24 | } |
||
25 | |||
26 | /** |
||
27 | * Store a newly created resource in storage. |
||
28 | * |
||
29 | * @param Request $request |
||
30 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
||
31 | * @throws \Illuminate\Validation\ValidationException |
||
32 | */ |
||
33 | public function store(Request $request) |
||
34 | { |
||
35 | $this->validate($request, [ |
||
36 | 'name' => 'required', |
||
37 | 'price' => 'required|numeric|min:6', |
||
38 | 'description' => 'required|min:150', |
||
39 | ]); |
||
40 | |||
41 | $hospital_id = $request->input('hospital_id'); |
||
42 | $room_id = DB::table('rooms') |
||
43 | ->insertGetId(array( |
||
44 | 'name' => $request->input('name'), |
||
45 | 'price_per_night' => $request->input('price'), |
||
46 | 'description' => $request->input('description'), |
||
47 | 'created_at' => Carbon::now(), |
||
48 | 'updated_at' => Carbon::now() |
||
49 | )); |
||
50 | |||
51 | if($room_id != null && $hospital_id != null) { |
||
52 | $data = [ |
||
53 | 'hospital_id' => $hospital_id, |
||
54 | 'room_id' => $room_id |
||
55 | ]; |
||
56 | |||
57 | if($this->insertRoomDetails($data) && $this->updateHospitalUpdatedAt($hospital_id)) { |
||
58 | return redirect(route('hospital.show', $hospital_id))->with('success', 'Kamar baru berhasil ditambahkan !'); |
||
59 | } |
||
60 | } |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Display the specified resource. |
||
65 | * |
||
66 | * @param int $id |
||
67 | * @return \Illuminate\Http\Response |
||
68 | */ |
||
69 | public function show($id, $hospital_id) |
||
70 | { |
||
71 | $data = [ |
||
72 | 'hospital' => Hospital::find($hospital_id), |
||
73 | 'room' => Room::find($id) |
||
74 | ]; |
||
75 | return view('pages.ext.view-room')->with('data', $data); |
||
0 ignored issues
–
show
|
|||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Show the form for editing the specified resource. |
||
80 | * |
||
81 | * @param int $id |
||
82 | * @param int $hospital_id |
||
83 | * @return \Illuminate\Http\Response |
||
84 | */ |
||
85 | public function edit($id, $hospital_id) |
||
86 | { |
||
87 | $data = [ |
||
88 | 'room' => Room::find($id), |
||
89 | 'hospital' => Hospital::find($hospital_id) |
||
90 | ]; |
||
91 | return view('pages.ext.edit-room')->with('data', $data); |
||
0 ignored issues
–
show
|
|||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Update the specified resource in storage. |
||
96 | * |
||
97 | * @param Request $request |
||
98 | * @param $id |
||
99 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
||
100 | * @throws \Illuminate\Validation\ValidationException |
||
101 | */ |
||
102 | public function update(Request $request, $id) |
||
103 | { |
||
104 | $this->validate($request, [ |
||
105 | 'name' => 'required', |
||
106 | 'price' => 'required|numeric|min:6', |
||
107 | 'description' => 'required|min:150', |
||
108 | ]); |
||
109 | |||
110 | $room = Room::find($id); |
||
111 | $room->name = $request->input('name'); |
||
112 | $room->price_per_night = $request->input('price'); |
||
113 | $room->description = $request->input('description'); |
||
114 | |||
115 | $hospital = $this->updateHospitalUpdatedAt($request->input('hospital_id')); |
||
116 | |||
117 | if($room->save() && $hospital == true) { |
||
0 ignored issues
–
show
|
|||
118 | return redirect(route('hospital.show', $request->input('hospital_id')))->with('success', 'Detil Kamar berhasil diubah !'); |
||
119 | } |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Remove the specified resource from storage. |
||
124 | * |
||
125 | * @param int $id |
||
126 | * @param int $hospital_id |
||
127 | * @return \Illuminate\Http\Response |
||
128 | */ |
||
129 | public function destroy($room_id, $hospital_id) |
||
130 | { |
||
131 | $room = Room::find($room_id); |
||
132 | $hospital = $this->updateHospitalUpdatedAt($hospital_id); |
||
133 | $detail = $this->deleteRoomDetails($room_id, $hospital_id); |
||
134 | |||
135 | if($room && $hospital && $detail) { |
||
136 | return redirect(route('hospital.show', ['id' => $hospital_id]))->with('success', 'Kamar dihapus !'); |
||
0 ignored issues
–
show
|
|||
137 | } |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @param $data |
||
142 | * @return bool |
||
143 | */ |
||
144 | private function insertRoomDetails($data) |
||
145 | { |
||
146 | $insert = DB::table('room_details') |
||
147 | ->insert([ |
||
148 | 'room_id' => $data['room_id'], |
||
149 | 'hospital_id' => $data['hospital_id'] |
||
150 | ]); |
||
151 | |||
152 | if($insert) { |
||
153 | return true; |
||
154 | } |
||
155 | |||
156 | return false; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * @param $room_id |
||
161 | * @param $hospital_id |
||
162 | * @return bool |
||
163 | */ |
||
164 | private function deleteRoomDetails($room_id, $hospital_id) |
||
165 | { |
||
166 | $delete = DB::table('room_details') |
||
167 | ->where('room_id', '=', $room_id) |
||
168 | ->where('hospital_id', '=', $hospital_id) |
||
169 | ->delete(); |
||
170 | |||
171 | if($delete) { |
||
172 | return true; |
||
173 | } |
||
174 | |||
175 | return false; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * @param $id |
||
180 | * @return bool |
||
181 | */ |
||
182 | private function updateHospitalUpdatedAt($id) |
||
183 | { |
||
184 | $hospital = Hospital::find($id); |
||
185 | $hospital->updated_at = Carbon::now(); |
||
186 | if($hospital->save()) { |
||
187 | return true; |
||
188 | } |
||
189 | |||
190 | return false; |
||
191 | } |
||
192 | } |
||
193 |