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.

TimelineController.php 7.6KB

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\Timeline;
  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 TimelineController extends Controller
  12. {
  13. public function __construct()
  14. {
  15. DB::enableQueryLog();
  16. }
  17. public function getAll()
  18. {
  19. $query = Timeline::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 = Timeline::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 = Timeline::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/Timeline/', $file, $nama_file );
  89. $query = new Timeline();
  90. $query->is_approved = 'pending';
  91. $query->caption = $request->caption;
  92. $query->timeline_photo = Helper::base64_to_image($request->gambar, 'timeline');
  93. $query->user_id = Helper::hash(Request::all()['user_id'], 'decode');
  94. $query->save();
  95. $query->hash_id = Helper::hash($query->id, 'encode');
  96. $query->user_id = Helper::hash($query->user_id, 'encode');
  97. if($query){
  98. $result = $query;
  99. $res_status = true;
  100. $msg = 'Mendapatkan Data';
  101. $status_msg = $msg;
  102. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  103. }else {
  104. $res_status = false;
  105. $msg = 'Data tidak ditemukan';
  106. $status_msg = $msg;
  107. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  108. }
  109. }
  110. }
  111. public function postUpdate(Request $request)
  112. {
  113. $post = $request->all();
  114. $rules = [
  115. 'id' => 'required',
  116. 'gambar' => 'required',
  117. 'user_id' => 'required',
  118. ];
  119. $validator = Validator::make($post, $rules);
  120. if(!$validator->fails()){
  121. $Timeline = Timeline::where('id', Helper::hash(Request::all()['id'], 'decode'))->firstOrFail();
  122. $Timeline->name = $request->name;
  123. $Timeline->Timeline_identity = $request->Timeline_identity;
  124. $Timeline->Timeline_type = $request->Timeline_type;
  125. $Timeline->Timeline_photo = Helper::base64_to_image($request->gambar, 'Timeline');
  126. $Timeline->user_id = Helper::hash(Request::all()['user_id'], 'decode');
  127. $Timeline->save();
  128. $Timeline->hash_id = Helper::hash($Timeline->id, 'encode');
  129. $Timeline->user_id = Helper::hash($Timeline->user_id, 'encode');
  130. $result = $Timeline;
  131. $res_status = true;
  132. $msg = 'Berhasil membuat user';
  133. $status_msg = $msg;
  134. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  135. }else {
  136. $res_status = false;
  137. $msg = 'Gagal Membuat user';
  138. $status_msg = $validator->errors();
  139. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  140. }
  141. }
  142. public function delete()
  143. {
  144. $validator = Validator::make(Request::all(), [
  145. 'id' => ['required'],
  146. ]);
  147. if(!$validator->fails()){
  148. $query = Timeline::find(Helper::hash(Request::all()['id'], 'decode'))->detroy();
  149. if($query){
  150. $result = $query;
  151. $res_status = true;
  152. $msg = 'Data berhasil dihapus';
  153. $status_msg = $msg;
  154. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  155. }else{
  156. $result = $query;
  157. $res_status = false;
  158. $msg = 'Data gagal dihapus';
  159. $status_msg = $msg;
  160. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  161. }
  162. }else{
  163. $res_status = false;
  164. $msg = 'Data tidak ditemukan';
  165. $status_msg = $validator->errors();
  166. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  167. }
  168. if($query){
  169. $result = $query;
  170. $res_status = true;
  171. $msg = 'Mendapatkan Data';
  172. $status_msg = $msg;
  173. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  174. }else {
  175. $res_status = false;
  176. $msg = 'Data tidak ditemukan';
  177. $status_msg = $msg;
  178. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  179. }
  180. }
  181. public function resSuccess($param = null, $status = null, $msg = null, $status_msg = null, $result = null)
  182. {
  183. $response['response'] = array(
  184. 'status' => $status,
  185. 'message' => $msg,
  186. 'status_msg' => $status_msg,
  187. );
  188. $response['param'] = !empty($param) ? $param : '';
  189. $response['results'] = $result;
  190. return response()->json($response, 200);
  191. }
  192. }