farhantock
9 months ago
4 changed files with 154 additions and 0 deletions
@ -0,0 +1,81 @@
|
||||
<?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','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); |
||||
} |
||||
} |
@ -0,0 +1,29 @@
|
||||
<?php |
||||
|
||||
namespace App\Models; |
||||
|
||||
use Illuminate\Database\Eloquent\Model; |
||||
|
||||
class RefferalCode extends Model |
||||
{ |
||||
protected $table = 'refferal_code'; |
||||
|
||||
const CREATED_AT = 'created_at'; |
||||
const UPDATED_AT = 'updated_at'; |
||||
|
||||
protected $fillable = [ |
||||
'id', |
||||
'code', |
||||
'amount', |
||||
'exp', |
||||
'type', |
||||
'allocation', |
||||
'description', |
||||
'created_at', |
||||
'created_by', |
||||
'updated_at', |
||||
'updated_by', |
||||
'deleted_at', |
||||
'deleted_by' |
||||
]; |
||||
} |
Loading…
Reference in new issue