Няма описание
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 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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::where('is_approved', 'approved')->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. $rules = [
  69. 'gambar' => 'required',
  70. 'user_id' => 'required'
  71. ];
  72. $validator = Validator::make($post, $rules);
  73. if ($validator->passes()) {
  74. $query = new Timeline();
  75. $query->is_approved = 'pending';
  76. $query->caption = $request->caption;
  77. $query->timeline_photo = Helper::base64_to_image($request->gambar, 'timeline');
  78. $query->user_id = Helper::hash($request->user_id, 'decode');
  79. $query->save();
  80. $query->hash_id = Helper::hash($query->id, 'encode');
  81. $query->user_id = Helper::hash($query->user_id, 'encode');
  82. if($query){
  83. $result = $query;
  84. $res_status = true;
  85. $msg = 'Mendapatkan Data';
  86. $status_msg = $msg;
  87. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  88. }else {
  89. $res_status = false;
  90. $msg = 'Data tidak ditemukan';
  91. $status_msg = $msg;
  92. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  93. }
  94. }
  95. }
  96. public function postUpdate(Request $request)
  97. {
  98. $post = $request->all();
  99. $rules = [
  100. 'id' => 'required',
  101. 'caption' => 'required'
  102. ];
  103. $validator = Validator::make($post, $rules);
  104. if(!$validator->fails()){
  105. $timeline = Timeline::where('id', Helper::hash($request->id, 'decode'))->firstOrFail();
  106. $timeline->caption = $request->caption;
  107. $timeline->save();
  108. $timeline->hash_id = Helper::hash($timeline->id, 'encode');
  109. $timeline->user_id = Helper::hash($timeline->user_id, 'encode');
  110. $result = $timeline;
  111. $res_status = true;
  112. $msg = 'Berhasil Update Data';
  113. $status_msg = $msg;
  114. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  115. }else {
  116. $res_status = false;
  117. $msg = 'Gagal Update Data';
  118. $status_msg = $validator->errors();
  119. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  120. }
  121. }
  122. public function delete(Request $request)
  123. {
  124. $post = $request->all();
  125. $rules = [
  126. 'id' => 'required'
  127. ];
  128. $validator = Validator::make($post, $rules);
  129. if(!$validator->fails()){
  130. $query = Timeline::find(Helper::hash($request->id, 'decode'))->delete();
  131. if($query){
  132. $result = $query;
  133. $res_status = true;
  134. $msg = 'Data berhasil dihapus';
  135. $status_msg = $msg;
  136. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  137. }else{
  138. $result = $query;
  139. $res_status = false;
  140. $msg = 'Data gagal dihapus';
  141. $status_msg = $msg;
  142. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  143. }
  144. }else{
  145. $res_status = false;
  146. $msg = 'Data tidak ditemukan';
  147. $status_msg = $validator->errors();
  148. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  149. }
  150. if($query){
  151. $result = $query;
  152. $res_status = true;
  153. $msg = 'Mendapatkan Data';
  154. $status_msg = $msg;
  155. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  156. }else {
  157. $res_status = false;
  158. $msg = 'Data tidak ditemukan';
  159. $status_msg = $msg;
  160. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  161. }
  162. }
  163. public function resSuccess($param = null, $status = null, $msg = null, $status_msg = null, $result = null)
  164. {
  165. $response['response'] = array(
  166. 'status' => $status,
  167. 'message' => $msg,
  168. 'status_msg' => $status_msg,
  169. );
  170. $response['param'] = !empty($param) ? $param : '';
  171. $response['results'] = $result;
  172. return response()->json($response, 200);
  173. }
  174. }