123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- <?php
-
- namespace App\Http\Controllers\Api\V1;
-
- use App\Http\Controllers\Controller;
- use Illuminate\Support\Facades\DB;
- use App\Model\Timeline;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Storage;
- use Validator;
- use Helper;
-
-
- class TimelineController extends Controller
- {
- public function __construct()
- {
- DB::enableQueryLog();
- }
-
- public function getAll()
- {
- $query = Timeline::get();
-
- if($query){
- $result = $query;
-
- $res_status = true;
- $msg = 'Mendapatkan Data';
- $status_msg = $msg;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
-
- }else {
-
- $res_status = false;
- $msg = 'Data tidak ditemukan';
- $status_msg = $msg;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
- }
- }
-
- public function getByUser($user_id)
- {
- $query = Timeline::where('user_id', Helper::hash($user_id, 'decode'))->get();
-
- if($query){
- $result = $query;
-
- $res_status = true;
- $msg = 'Mendapatkan Data';
- $status_msg = $msg;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
-
- }else {
-
- $res_status = false;
- $msg = 'Data tidak ditemukan';
- $status_msg = $msg;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
- }
- }
-
- public function getSingle($data_id)
- {
- $query = Timeline::where('id', Helper::hash($data_id, 'decode'))->get();
-
- if($query){
- $result = $query;
-
- $res_status = true;
- $msg = 'Mendapatkan Data';
- $status_msg = $msg;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
-
- }else {
-
- $res_status = false;
- $msg = 'Data tidak ditemukan';
- $status_msg = $msg;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
- }
- }
-
- public function postStore(Request $request)
- {
- $post = $request->all();
- // $file = $request->file('gambar');
- //dd($post);
- /**digunakan untuk set rule validator */
- $rules = [
- 'gambar' => 'required',
- 'user_id' => 'required',
- // 'gambar' => 'required|file|max:1000',
- ];
- /**digunakan untuk set message dari validatornya yang akan keluar gimna, :attribue itu udah langsung mendeteksi inputtan dari name="attribue" */
- // $alert = [
- // 'file' => 'File maksimal 1MB'
- // ];
- // $validator = Validator::make($post, $rules, $alert);
- $validator = Validator::make($post, $rules);
-
- if ($validator->passes()) {
-
- /**menggunakan transaction */
- DB::beginTransaction();
-
- // $nama_file = time()."_".$file->getClientOriginalName();
-
- // $tujuan_upload = 'gambar';
-
- // //$file->move($tujuan_upload,$nama_file);
- // Storage::putFileAs('public/images/Timeline/', $file, $nama_file );
-
- $query = new Timeline();
- $query->is_approved = 'pending';
- $query->caption = $request->caption;
- $query->timeline_photo = Helper::base64_to_image($request->gambar, 'timeline');
- $query->user_id = Helper::hash(Request::all()['user_id'], 'decode');
- $query->save();
-
- $query->hash_id = Helper::hash($query->id, 'encode');
- $query->user_id = Helper::hash($query->user_id, 'encode');
-
-
- if($query){
- $result = $query;
-
- $res_status = true;
- $msg = 'Mendapatkan Data';
- $status_msg = $msg;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
-
- }else {
-
- $res_status = false;
- $msg = 'Data tidak ditemukan';
- $status_msg = $msg;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
- }
- }
- }
-
- public function postUpdate(Request $request)
- {
- $post = $request->all();
-
- $rules = [
- 'id' => 'required',
- 'gambar' => 'required',
- 'user_id' => 'required',
- ];
-
- $validator = Validator::make($post, $rules);
-
- if(!$validator->fails()){
- $Timeline = Timeline::where('id', Helper::hash(Request::all()['id'], 'decode'))->firstOrFail();
-
- $Timeline->name = $request->name;
- $Timeline->Timeline_identity = $request->Timeline_identity;
- $Timeline->Timeline_type = $request->Timeline_type;
- $Timeline->Timeline_photo = Helper::base64_to_image($request->gambar, 'Timeline');
- $Timeline->user_id = Helper::hash(Request::all()['user_id'], 'decode');
- $Timeline->save();
-
- $Timeline->hash_id = Helper::hash($Timeline->id, 'encode');
- $Timeline->user_id = Helper::hash($Timeline->user_id, 'encode');
-
- $result = $Timeline;
- $res_status = true;
- $msg = 'Berhasil membuat user';
- $status_msg = $msg;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
-
- }else {
-
- $res_status = false;
- $msg = 'Gagal Membuat user';
- $status_msg = $validator->errors();
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
- }
- }
-
- public function delete()
- {
- $validator = Validator::make(Request::all(), [
- 'id' => ['required'],
- ]);
- if(!$validator->fails()){
- $query = Timeline::find(Helper::hash(Request::all()['id'], 'decode'))->detroy();
-
- if($query){
- $result = $query;
- $res_status = true;
- $msg = 'Data berhasil dihapus';
- $status_msg = $msg;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
- }else{
- $result = $query;
- $res_status = false;
- $msg = 'Data gagal dihapus';
- $status_msg = $msg;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
- }
- }else{
- $res_status = false;
- $msg = 'Data tidak ditemukan';
- $status_msg = $validator->errors();
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
- }
-
-
- if($query){
- $result = $query;
-
- $res_status = true;
- $msg = 'Mendapatkan Data';
- $status_msg = $msg;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
-
- }else {
-
- $res_status = false;
- $msg = 'Data tidak ditemukan';
- $status_msg = $msg;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
- }
- }
-
- public function resSuccess($param = null, $status = null, $msg = null, $status_msg = null, $result = null)
- {
- $response['response'] = array(
- 'status' => $status,
- 'message' => $msg,
- 'status_msg' => $status_msg,
- );
- $response['param'] = !empty($param) ? $param : '';
-
- $response['results'] = $result;
-
- return response()->json($response, 200);
- }
-
-
- }
|