reconstruction of the project
This commit is contained in:
@@ -3,78 +3,116 @@
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\BaseModel;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class Configuration extends BaseModel
|
||||
{
|
||||
protected $table = 'configuration'; // The name of the table
|
||||
protected $primaryKey = 'id'; // The primary key of the table
|
||||
protected $table = 'configuration';
|
||||
|
||||
// CI model didn't use timestamps
|
||||
public $timestamps = false;
|
||||
|
||||
protected $fillable = [
|
||||
'config_key',
|
||||
'config_value',
|
||||
];
|
||||
|
||||
public $timestamps = false;
|
||||
/* =========================
|
||||
* CI method equivalents
|
||||
* ========================= */
|
||||
|
||||
/**
|
||||
* Get configuration value by key.
|
||||
*
|
||||
* @param string $key
|
||||
* @return string|null
|
||||
* Get configuration value by key (deterministic in case duplicates exist).
|
||||
*/
|
||||
public function getConfigValueByKey(string $key)
|
||||
public static function getConfigValueByKey(string $key): ?string
|
||||
{
|
||||
// Deterministic read in case historical duplicates exist
|
||||
$result = $this->where('config_key', $key)
|
||||
->orderBy('id', 'DESC')
|
||||
->first();
|
||||
return $result ? $result['config_value'] : null;
|
||||
try {
|
||||
$row = static::query()
|
||||
->where('config_key', $key)
|
||||
->orderByDesc('id')
|
||||
->first();
|
||||
} catch (\Throwable $e) {
|
||||
if (app()->runningInConsole()) {
|
||||
return null;
|
||||
}
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return $row ? (string) $row->config_value : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set configuration value by key.
|
||||
*
|
||||
* @param string $key
|
||||
* @param string $value
|
||||
* @return bool
|
||||
* If duplicates exist, update ALL of them to avoid inconsistent reads.
|
||||
*/
|
||||
public function setConfigValueByKey(string $key, string $value): bool
|
||||
public static function setConfigValueByKey(string $key, string $value): bool
|
||||
{
|
||||
// If one or more rows exist for this key, update ALL of them to avoid
|
||||
// inconsistent reads when duplicates are present.
|
||||
$count = $this->where('config_key', $key)->countAllResults();
|
||||
$count = static::query()->where('config_key', $key)->count();
|
||||
|
||||
if ($count > 0) {
|
||||
// Use a direct builder update scoped by key to affect all matches
|
||||
$ok = (bool) $this->db->table($this->table)
|
||||
$affected = static::query()
|
||||
->where('config_key', $key)
|
||||
->update(['config_value' => $value]);
|
||||
return $ok;
|
||||
|
||||
return $affected >= 0;
|
||||
}
|
||||
|
||||
// Insert a new record if none exist
|
||||
return $this->insert(['config_key' => $key, 'config_value' => $value]) !== false;
|
||||
$row = static::query()->create([
|
||||
'config_key' => $key,
|
||||
'config_value' => $value,
|
||||
]);
|
||||
|
||||
return !empty($row->id);
|
||||
}
|
||||
|
||||
// Method to retrieve all configuration data
|
||||
public function getAllConfigs()
|
||||
/**
|
||||
* Retrieve all configuration rows.
|
||||
*/
|
||||
public static function getAllConfigs()
|
||||
{
|
||||
return $this->findAll();
|
||||
return static::query()->get();
|
||||
}
|
||||
|
||||
// Method to update configuration by key
|
||||
public function updateConfig($id, $data)
|
||||
/**
|
||||
* Update configuration by id.
|
||||
*/
|
||||
public static function updateConfig(int $id, array $data): bool
|
||||
{
|
||||
return $this->update($id, $data);
|
||||
return static::query()->whereKey($id)->update($data) >= 0;
|
||||
}
|
||||
|
||||
// Method to add new configuration
|
||||
public function addConfig($data)
|
||||
/**
|
||||
* Add new configuration row.
|
||||
*/
|
||||
public static function addConfig(array $data): int
|
||||
{
|
||||
return $this->insert($data);
|
||||
$row = static::query()->create($data);
|
||||
return (int) $row->id;
|
||||
}
|
||||
|
||||
public function getConfig($key)
|
||||
/**
|
||||
* Backward-compatible helper (keeps your "semester" special-case).
|
||||
*
|
||||
* NOTE: In Laravel it’s better to call SemesterRangeService directly from services/controllers,
|
||||
* but this keeps behavior consistent with the CI model.
|
||||
*/
|
||||
public static function getConfig(string $key): ?string
|
||||
{
|
||||
return $this->getConfigValueByKey((string) $key);
|
||||
$key = (string) $key;
|
||||
|
||||
if ($key === 'semester') {
|
||||
try {
|
||||
// If you have this service in Laravel, inject/resolve it from container.
|
||||
// This call assumes the service constructor can accept Configuration model (like CI did).
|
||||
$semester = app(\App\Services\SemesterRangeService::class)->getSemesterForDate();
|
||||
if (is_string($semester) && $semester !== '') {
|
||||
return $semester;
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
// ignore and fall back
|
||||
}
|
||||
}
|
||||
|
||||
return static::getConfigValueByKey($key);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user