Spaces:
Running
Running
| namespace App\Domain\Config; | |
| use Illuminate\Support\Carbon; | |
| use Illuminate\Support\Facades\DB; | |
| use stdClass; | |
| class ConfigRepository | |
| { | |
| public function listNamespace(string $namespace): array | |
| { | |
| return DB::table('configuration_settings as current_settings') | |
| ->where('current_settings.namespace', $namespace) | |
| ->whereNull('current_settings.deleted_at') | |
| ->whereRaw('current_settings.version_number = ( | |
| SELECT MAX(version_number) | |
| FROM configuration_settings | |
| WHERE namespace = current_settings.namespace | |
| AND setting_key = current_settings.setting_key | |
| AND deleted_at IS NULL | |
| )') | |
| ->orderBy('current_settings.setting_key') | |
| ->get() | |
| ->map(fn ($row) => (array) $row) | |
| ->all(); | |
| } | |
| public function latest(string $namespace, string $key): ?stdClass | |
| { | |
| return DB::table('configuration_settings') | |
| ->where('namespace', $namespace) | |
| ->where('setting_key', $key) | |
| ->whereNull('deleted_at') | |
| ->orderByDesc('version_number') | |
| ->first(); | |
| } | |
| public function versions(string $namespace, string $key): array | |
| { | |
| return DB::table('configuration_settings') | |
| ->where('namespace', $namespace) | |
| ->where('setting_key', $key) | |
| ->whereNull('deleted_at') | |
| ->orderByDesc('version_number') | |
| ->get() | |
| ->map(fn ($row) => (array) $row) | |
| ->all(); | |
| } | |
| public function createVersion(string $id, string $namespace, string $key, mixed $value, string $status = 'DRAFT'): string | |
| { | |
| $latest = $this->latest($namespace, $key); | |
| $now = Carbon::now(); | |
| DB::table('configuration_settings')->insert([ | |
| 'id' => $id, | |
| 'namespace' => $namespace, | |
| 'setting_key' => $key, | |
| 'setting_value' => json_encode($value, JSON_THROW_ON_ERROR), | |
| 'status' => $status, | |
| 'version_number' => $latest === null ? 1 : ((int) $latest->version_number) + 1, | |
| 'created_at' => $now, | |
| 'updated_at' => $now, | |
| ]); | |
| return $id; | |
| } | |
| public function publish(string $id, ?string $approvedByUserId): void | |
| { | |
| DB::table('configuration_settings')->where('id', $id)->whereNull('deleted_at')->update([ | |
| 'status' => 'PUBLISHED', | |
| 'approved_by_user_id' => $approvedByUserId, | |
| 'approved_at' => Carbon::now(), | |
| 'published_at' => Carbon::now(), | |
| 'updated_at' => Carbon::now(), | |
| ]); | |
| } | |
| } | |