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.
128 lines
4.5 KiB
128 lines
4.5 KiB
<?php |
|
|
|
namespace App\Http\Controllers; |
|
|
|
use Illuminate\Http\Request; |
|
use App\Models\Image; |
|
|
|
class ImageController extends Controller |
|
{ |
|
public function uploadImage(Request $request){ |
|
|
|
if($request->hasFile('files')){ |
|
|
|
$this->validate($request, [ |
|
'ref_id' => 'required', |
|
'category' => 'required' |
|
]); |
|
|
|
$ref_id = $request->ref_id; |
|
$dokumen = $request->file('files'); |
|
$extension = $dokumen->extension(); |
|
$name = $ref_id.date("YmdHis").".".$extension; |
|
|
|
$resultMove = $dokumen->move($this->pathImage, $name); |
|
|
|
if($resultMove){ |
|
$data = [ |
|
'ref_id' => (int)$ref_id, |
|
'image' => $name, |
|
'category' => $request->category |
|
]; |
|
|
|
$result = Image::create($data); |
|
|
|
if($result){ |
|
return response()->json(['status'=>'success', "name_image"=>$name,'message'=>'image upload is successfully!','code'=>200], 200); |
|
}else{ |
|
unlink($this->pathImage.$name); |
|
return response()->json(['status'=>'failed','message'=>'image upload is failed!','code'=>400], 400); |
|
} |
|
|
|
}else{ |
|
return response()->json(['status'=>'failed','message'=>'image upload is failed!','code'=>400], 400); |
|
} |
|
}else{ |
|
return response()->json(['status'=>'failed','message'=>'image is required!','code'=>400], 400); |
|
} |
|
} |
|
|
|
public function delete($id) |
|
{ |
|
$data = Image::find($id); |
|
|
|
if($data){ |
|
unlink($this->pathImage.$data->image); |
|
$delete = $data->delete(); |
|
}else{ |
|
return response()->json(['status'=>'failed','message'=>'data image not found!','code'=>400], 400); |
|
die(); |
|
} |
|
|
|
|
|
if($delete){ |
|
return response()->json(['status'=>'success','message'=>'data image successfully deleted!','code'=>200], 200); |
|
}else{ |
|
return response()->json(['status'=>'failed','message'=>'data image failed deleted!','code'=>400], 400); |
|
} |
|
} |
|
|
|
public function search(Request $request) |
|
{ |
|
// payload same like in goland API oke |
|
$payload = $request->all(); |
|
// second parameter is table name |
|
$dataBuilder = $this->setUpPayload($payload, 'm_image'); |
|
// builder for get data |
|
$builder = $dataBuilder['builder']; |
|
// builder for count data |
|
$countBuilder = $dataBuilder['count']; |
|
// execute builder |
|
$dataGet = $builder->get(); |
|
// execute count builder |
|
$totalRecord = $countBuilder->count(); |
|
// send response about the result |
|
return response()->json(['status'=>'success','code'=>200,'data'=>$dataGet, 'totalRecord'=>$totalRecord], 200); |
|
} |
|
|
|
public function deleteByRef($id, $category) |
|
{ |
|
$data = Image::where("ref_id", $id)->where("category", $category)->first(); |
|
|
|
if($data){ |
|
unlink($this->pathImage.$data->image); |
|
$delete = $data->delete(); |
|
}else{ |
|
return response()->json(['status'=>'failed','message'=>'data image not found!','code'=>400], 400); |
|
die(); |
|
} |
|
|
|
|
|
if($delete){ |
|
return response()->json(['status'=>'success','message'=>'data image successfully deleted!','code'=>200], 200); |
|
}else{ |
|
return response()->json(['status'=>'failed','message'=>'data image failed deleted!','code'=>400], 400); |
|
} |
|
} |
|
|
|
public function getByRefId($id, $category) |
|
{ |
|
if(!$id || (int) $id < 0 || $id==""){ |
|
return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400); |
|
die(); |
|
} |
|
|
|
if(!$category || $category==""){ |
|
return response()->json(['status'=>'failed','message'=>'category is required!','code'=>400], 400); |
|
die(); |
|
} |
|
|
|
$result = Image::where("ref_id", $id)->where("category", $category)->first(); |
|
|
|
if($result){ |
|
return response()->json(['status'=>'success','code'=>200,'data'=>$result], 200); |
|
}else{ |
|
return response()->json(['status'=>'failed','message'=>'failed get data image, please try again later!','code'=>400], 400); |
|
} |
|
} |
|
}
|
|
|