No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TransportationController.php 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. namespace App\Http\Controllers\Api\V1;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Support\Facades\DB;
  5. use App\Model\Transportation;
  6. use Illuminate\Support\Facades\Auth;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\Storage;
  9. use Validator;
  10. use Helper;
  11. class TransportationController extends Controller
  12. {
  13. public function __construct()
  14. {
  15. DB::enableQueryLog();
  16. }
  17. public function getAll()
  18. {
  19. $query = Transportation::get();
  20. if($query){
  21. $result = $query;
  22. $res_status = true;
  23. $msg = 'Mendapatkan Data';
  24. $status_msg = $msg;
  25. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  26. }else {
  27. $res_status = false;
  28. $msg = 'Data tidak ditemukan';
  29. $status_msg = $msg;
  30. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  31. }
  32. }
  33. public function getByUser($user_id)
  34. {
  35. $query = Transportation::where('user_id', Helper::hash($user_id, 'decode'))->get();
  36. if($query){
  37. $result = $query;
  38. $res_status = true;
  39. $msg = 'Mendapatkan Data';
  40. $status_msg = $msg;
  41. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  42. }else {
  43. $res_status = false;
  44. $msg = 'Data tidak ditemukan';
  45. $status_msg = $msg;
  46. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  47. }
  48. }
  49. public function getSingle($data_id)
  50. {
  51. $query = Transportation::where('id', Helper::hash($data_id, 'decode'))->get();
  52. if($query){
  53. $result = $query;
  54. $res_status = true;
  55. $msg = 'Mendapatkan Data';
  56. $status_msg = $msg;
  57. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  58. }else {
  59. $res_status = false;
  60. $msg = 'Data tidak ditemukan';
  61. $status_msg = $msg;
  62. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  63. }
  64. }
  65. public function postStore(Request $request)
  66. {
  67. $post = $request->all();
  68. // $file = $request->file('gambar');
  69. //dd($post);
  70. /**digunakan untuk set rule validator */
  71. $rules = [
  72. 'gambar' => 'required',
  73. 'user_id' => 'required',
  74. // 'gambar' => 'required|file|max:1000',
  75. ];
  76. /**digunakan untuk set message dari validatornya yang akan keluar gimna, :attribue itu udah langsung mendeteksi inputtan dari name="attribue" */
  77. // $alert = [
  78. // 'file' => 'File maksimal 1MB'
  79. // ];
  80. // $validator = Validator::make($post, $rules, $alert);
  81. $validator = Validator::make($post, $rules);
  82. if ($validator->passes()) {
  83. /**menggunakan transaction */
  84. DB::beginTransaction();
  85. // $nama_file = time()."_".$file->getClientOriginalName();
  86. // $tujuan_upload = 'gambar';
  87. // //$file->move($tujuan_upload,$nama_file);
  88. // Storage::putFileAs('public/images/transportation/', $file, $nama_file );
  89. $query = new Transportation();
  90. $query->name = $request->name;
  91. $query->transportation_identity = $request->transportation_identity;
  92. $query->transportation_type = $request->transportation_type;
  93. $query->transportation_photo = Helper::base64_to_image($request->gambar, 'transportation');
  94. $query->user_id = Helper::hash(Request::all()['user_id'], 'decode');
  95. $query->save();
  96. $query->hash_id = Helper::hash($query->id, 'encode');
  97. $query->user_id = Helper::hash($query->user_id, 'encode');
  98. if($query){
  99. $result = $query;
  100. $res_status = true;
  101. $msg = 'Mendapatkan Data';
  102. $status_msg = $msg;
  103. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  104. }else {
  105. $res_status = false;
  106. $msg = 'Data tidak ditemukan';
  107. $status_msg = $msg;
  108. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  109. }
  110. }
  111. }
  112. public function postUpdate(Request $request)
  113. {
  114. $post = $request->all();
  115. $rules = [
  116. 'id' => 'required',
  117. 'gambar' => 'required',
  118. 'user_id' => 'required',
  119. ];
  120. $validator = Validator::make($post, $rules);
  121. if(!$validator->fails()){
  122. $transportation = Transportation::where('id', Helper::hash(Request::all()['id'], 'decode'))->firstOrFail();
  123. $transportation->name = $request->name;
  124. $transportation->transportation_identity = $request->transportation_identity;
  125. $transportation->transportation_type = $request->transportation_type;
  126. $transportation->transportation_photo = Helper::base64_to_image($request->gambar, 'transportation');
  127. $transportation->user_id = Helper::hash(Request::all()['user_id'], 'decode');
  128. $transportation->save();
  129. $transportation->hash_id = Helper::hash($transportation->id, 'encode');
  130. $transportation->user_id = Helper::hash($transportation->user_id, 'encode');
  131. $result = $transportation;
  132. $res_status = true;
  133. $msg = 'Berhasil membuat user';
  134. $status_msg = $msg;
  135. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  136. }else {
  137. $res_status = false;
  138. $msg = 'Gagal Membuat user';
  139. $status_msg = $validator->errors();
  140. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  141. }
  142. }
  143. public function delete()
  144. {
  145. $validator = Validator::make(Request::all(), [
  146. 'id' => ['required'],
  147. ]);
  148. if(!$validator->fails()){
  149. $query = Transportation::find(Helper::hash(Request::all()['id'], 'decode'))->detroy();
  150. if($query){
  151. $result = $query;
  152. $res_status = true;
  153. $msg = 'Data berhasil dihapus';
  154. $status_msg = $msg;
  155. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  156. }else{
  157. $result = $query;
  158. $res_status = false;
  159. $msg = 'Data gagal dihapus';
  160. $status_msg = $msg;
  161. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  162. }
  163. }else{
  164. $res_status = false;
  165. $msg = 'Data tidak ditemukan';
  166. $status_msg = $validator->errors();
  167. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  168. }
  169. if($query){
  170. $result = $query;
  171. $res_status = true;
  172. $msg = 'Mendapatkan Data';
  173. $status_msg = $msg;
  174. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  175. }else {
  176. $res_status = false;
  177. $msg = 'Data tidak ditemukan';
  178. $status_msg = $msg;
  179. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  180. }
  181. }
  182. public function resSuccess($param = null, $status = null, $msg = null, $status_msg = null, $result = null)
  183. {
  184. $response['response'] = array(
  185. 'status' => $status,
  186. 'message' => $msg,
  187. 'status_msg' => $status_msg,
  188. );
  189. $response['param'] = !empty($param) ? $param : '';
  190. $response['results'] = $result;
  191. return response()->json($response, 200);
  192. }
  193. }