initial project
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controllers\Backend;
|
||||
|
||||
use App\Controllers\BaseController;
|
||||
use App\Models\TopupSettingModel;
|
||||
use CodeIgniter\API\ResponseTrait;
|
||||
use CodeIgniter\RESTful\ResourceController;
|
||||
|
||||
class TopupSettingController extends ResourceController
|
||||
{
|
||||
use ResponseTrait;
|
||||
|
||||
private $settings;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->settings = new TopupSettingModel();
|
||||
}
|
||||
|
||||
public function index()
|
||||
{
|
||||
$data = $this->settings
|
||||
->orderBy('created_at', 'DESC')
|
||||
->findAll();
|
||||
|
||||
return $this->respond([
|
||||
'status' => 200,
|
||||
'data' => $data,
|
||||
]);
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
// echo(123123);exit;
|
||||
$payload = $this->request->getJSON(true) ?? $this->request->getPost();
|
||||
|
||||
$rules = [
|
||||
'topup_amount' => 'required|decimal|greater_than[0]',
|
||||
'credit_amount' => 'required|decimal|greater_than_equal_to[0]',
|
||||
'status' => 'required|in_list[active,inactive]',
|
||||
];
|
||||
|
||||
if (! $this->validate($rules)) {
|
||||
return $this->failValidationErrors($this->validator->getErrors());
|
||||
}
|
||||
|
||||
$id = $this->settings->insert([
|
||||
'topup_amount' => $payload['topup_amount'],
|
||||
'credit_amount' => $payload['credit_amount'],
|
||||
'status' => $payload['status'],
|
||||
], true);
|
||||
|
||||
if (! $id) {
|
||||
return $this->fail('Create failed', 400);
|
||||
}
|
||||
|
||||
return $this->respond([
|
||||
'status' => 200,
|
||||
'message' => 'Topup Setting Created',
|
||||
'data' => $this->settings->find($id),
|
||||
]);
|
||||
}
|
||||
|
||||
public function update($id = null)
|
||||
{
|
||||
// echo(123);exit;
|
||||
$id = (int) $id;
|
||||
if (! $this->settings->find($id)) {
|
||||
return $this->fail('Not found', 404);
|
||||
}
|
||||
|
||||
$payload = $this->request->getJSON(true) ?? $this->request->getRawInput();
|
||||
|
||||
$rules = [
|
||||
'topup_amount' => 'permit_empty|decimal|greater_than[0]',
|
||||
'credit_amount' => 'permit_empty|decimal|greater_than_equal_to[0]',
|
||||
'status' => 'permit_empty|in_list[active,inactive]',
|
||||
];
|
||||
|
||||
if (! $this->validate($rules)) {
|
||||
return $this->failValidationErrors($this->validator->getErrors());
|
||||
}
|
||||
|
||||
if (! $this->settings->update($id, $payload)) {
|
||||
return $this->fail('Update failed', 400);
|
||||
}
|
||||
|
||||
return $this->respond([
|
||||
'status' => 200,
|
||||
'message' => 'Topup Setting Updated',
|
||||
'data' => $this->settings->find($id),
|
||||
]);
|
||||
}
|
||||
|
||||
public function delete($id = null)
|
||||
{
|
||||
$id = (int) $id;
|
||||
if (! $this->settings->find($id)) {
|
||||
return $this->fail('Not found', 404);
|
||||
}
|
||||
|
||||
if (! $this->settings->delete($id)) {
|
||||
return $this->fail('Delete failed', 400);
|
||||
}
|
||||
|
||||
return $this->respond([
|
||||
'status' => 200,
|
||||
'message' => 'Topup Setting deleted',
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user