Custom Backend OSPRO Surveyor Indonesia
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.

81 lines
2.6 KiB

<?php
namespace App\Http\Controllers;
use App\Models\RefferalCode;
use Illuminate\Http\Request;
class RefferalCodeController extends Controller
{
public function add(Request $request)
{
$this->validate($request, [
'code' => 'required|unique:refferal_code,code',
'exp' => 'nullable',
'type' => 'nullable',
'allocation' => 'nullable'
]);
$data = $request->all();
$data['created_by'] = $this->currentName;
$result = RefferalCode::create($data);
if($result){
return response()->json(['status'=>'success', 'data'=> $result, 'message'=>'Add refferal code successfully!','code'=>200], 200);
}else{
return response()->json(['status'=>'failed','message'=>'Add refferal Code failed!','code'=>400], 400);
}
}
public function edit($id)
{
if (!$id || (int) $id < 0 || $id == "") {
return response()->json(['status' => 'failed', 'message' => 'Id is required!', 'code' => 400], 400);
die();
}
$result = RefferalCode::find($id);
if ($result) {
return response()->json(['status' => 'success', 'code' => 200, 'data' => $result], 200);
} else {
return response()->json(['status' => 'failed', 'message' => 'Failed get refferal code, please try again later!', 'code' => 400], 400);
}
}
public function update(Request $request, $id)
{
if(!$id || (int) $id < 0 || $id==""){
return response()->json(['status'=>'failed','message'=>'Id is required!','code'=>400], 400);
}
$data = RefferalCode::find($id);
if($data){
$result = $data->update($request->all());
}else{
return response()->json(['status'=>'failed','message'=>'Refferal code not found!','code'=>400], 400);
die();
}
if($result){
return response()->json(['status'=>'success','message'=>'Refferal code successfully updated!','code'=>200], 200);
}else{
return response()->json(['status'=>'failed','message'=>'Refferal code failed updated!','code'=>400], 400);
}
}
public function search(Request $request)
{
$payload = $request->all();
$dataBuilder = $this->setUpPayload($payload, 'refferal_code');
$builder = $dataBuilder['builder'];
$countBuilder = $dataBuilder['count'];
$dataGet = $builder->get();
$totalRecord = $countBuilder->count();
return response()->json(['status' => 'success', 'code' => 200, 'data' => $dataGet, 'totalRecord' => $totalRecord], 200);
}
}