149 lines
4.8 KiB
PHP
Executable File
149 lines
4.8 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Models\Configuration;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
class ConfigurationController extends BaseApiController
|
|
{
|
|
protected Configuration $config;
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->config = model(Configuration::class);
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$page = max(1, (int) ($this->request->getGet('page') ?? 1));
|
|
$perPage = min(100, max(1, (int) ($this->request->getGet('per_page') ?? 20)));
|
|
$key = $this->request->getGet('key');
|
|
|
|
$query = $this->config->newQuery()->orderBy('id', 'ASC');
|
|
if ($key) {
|
|
$query->where('config_key', 'LIKE', '%' . $key . '%');
|
|
}
|
|
|
|
$result = $this->paginate($query, $page, $perPage);
|
|
return $this->success($result, 'Configurations retrieved successfully');
|
|
}
|
|
|
|
public function show($id = null)
|
|
{
|
|
$config = $this->config->find($id);
|
|
if (!$config) {
|
|
return $this->respondError('Configuration not found', Response::HTTP_NOT_FOUND);
|
|
}
|
|
|
|
return $this->success($config, 'Configuration retrieved successfully');
|
|
}
|
|
|
|
public function getByKey($key = null)
|
|
{
|
|
$config = $this->config->where('config_key', $key)->first();
|
|
if (!$config) {
|
|
return $this->respondError('Configuration not found', Response::HTTP_NOT_FOUND);
|
|
}
|
|
|
|
return $this->success($config, 'Configuration retrieved successfully');
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$data = $this->payloadData();
|
|
if (empty($data)) {
|
|
return $this->respondError('Invalid request data', Response::HTTP_BAD_REQUEST);
|
|
}
|
|
|
|
$rules = [
|
|
'config_key' => 'required|string|max_length[255]|is_unique[configuration.config_key]',
|
|
'config_value' => 'required',
|
|
];
|
|
|
|
$errors = $this->validateRequest($data, $rules);
|
|
if (!empty($errors)) {
|
|
return $this->respondValidationError($errors);
|
|
}
|
|
|
|
try {
|
|
$config = $this->config->create([
|
|
'config_key' => $data['config_key'],
|
|
'config_value' => $data['config_value'],
|
|
]);
|
|
} catch (\Throwable $e) {
|
|
log_message('error', 'Configuration creation error: ' . $e->getMessage());
|
|
return $this->respondError('Failed to create configuration', Response::HTTP_INTERNAL_SERVER_ERROR);
|
|
}
|
|
|
|
return $this->respondCreated([
|
|
'id' => $config->id,
|
|
'data' => $config,
|
|
], 'Configuration created successfully');
|
|
}
|
|
|
|
public function update($id = null)
|
|
{
|
|
$config = $this->config->find($id);
|
|
if (!$config) {
|
|
return $this->respondError('Configuration not found', Response::HTTP_NOT_FOUND);
|
|
}
|
|
|
|
$data = $this->payloadData();
|
|
if (empty($data)) {
|
|
return $this->respondError('Invalid request data', Response::HTTP_BAD_REQUEST);
|
|
}
|
|
|
|
$updateData = array_intersect_key($data, array_flip(['config_key', 'config_value']));
|
|
if (empty($updateData)) {
|
|
return $this->respondError('No valid fields to update', Response::HTTP_BAD_REQUEST);
|
|
}
|
|
|
|
try {
|
|
$config->fill($updateData);
|
|
$config->save();
|
|
} catch (\Throwable $e) {
|
|
log_message('error', 'Configuration update error: ' . $e->getMessage());
|
|
return $this->respondError('Failed to update configuration', Response::HTTP_INTERNAL_SERVER_ERROR);
|
|
}
|
|
|
|
return $this->success($config->fresh(), 'Configuration updated successfully');
|
|
}
|
|
|
|
public function delete($id = null)
|
|
{
|
|
$config = $this->config->find($id);
|
|
if (!$config) {
|
|
return $this->respondError('Configuration not found', Response::HTTP_NOT_FOUND);
|
|
}
|
|
|
|
try {
|
|
$config->delete();
|
|
} catch (\Throwable $e) {
|
|
log_message('error', 'Configuration deletion error: ' . $e->getMessage());
|
|
return $this->respondError('Failed to delete configuration', Response::HTTP_INTERNAL_SERVER_ERROR);
|
|
}
|
|
|
|
return $this->success(null, 'Configuration deleted successfully');
|
|
}
|
|
|
|
public function listData()
|
|
{
|
|
$configs = $this->config->newQuery()
|
|
->orderBy('id', 'ASC')
|
|
->get()
|
|
->map(function ($row) {
|
|
return [
|
|
'id' => (int) ($row->id ?? 0),
|
|
'config_key' => (string) ($row->config_key ?? ''),
|
|
'config_value' => (string) ($row->config_value ?? ''),
|
|
];
|
|
})
|
|
->values()
|
|
->toArray();
|
|
|
|
return $this->success(['configs' => $configs], 'Configuration list retrieved');
|
|
}
|
|
}
|