add all controllers logic

This commit is contained in:
root
2026-03-11 17:53:15 -04:00
parent 3e6c577085
commit 2ef71cc92b
421 changed files with 12009 additions and 5211 deletions
@@ -0,0 +1,56 @@
<?php
namespace App\Services\Preferences;
use App\Models\Preferences;
use Illuminate\Support\Facades\DB;
class PreferencesCommandService
{
public function __construct(private PreferencesOptionsService $options)
{
}
public function upsert(int $userId, array $payload): Preferences
{
$data = $this->mapPayload($payload);
$data['user_id'] = $userId;
return DB::transaction(function () use ($userId, $data) {
$existing = Preferences::query()->where('user_id', $userId)->first();
if ($existing) {
$existing->fill($data);
$existing->save();
return $existing->refresh();
}
return Preferences::query()->create($data);
});
}
public function delete(Preferences $preferences): bool
{
return (bool) $preferences->delete();
}
private function mapPayload(array $payload): array
{
$email = $payload['receive_email_notifications'] ?? $payload['notification_email'] ?? null;
$sms = $payload['receive_sms_notifications'] ?? $payload['notification_sms'] ?? null;
$style = $this->options->normalizeStyle($payload['style_color'] ?? null);
$menu = $this->options->normalizeMenu($payload['menu_color'] ?? null);
$data = [
'notification_email' => $email !== null ? (int) filter_var($email, FILTER_VALIDATE_BOOLEAN) : null,
'notification_sms' => $sms !== null ? (int) filter_var($sms, FILTER_VALIDATE_BOOLEAN) : null,
'theme' => $payload['theme'] ?? null,
'language' => $payload['language'] ?? null,
'style_color' => $style,
'menu_color' => $menu,
];
return array_filter($data, static fn ($value) => $value !== null);
}
}
@@ -0,0 +1,48 @@
<?php
namespace App\Services\Preferences;
class PreferencesOptionsService
{
public function defaultPreferences(): array
{
return [
'receive_email_notifications' => true,
'receive_sms_notifications' => true,
'theme' => 'light',
'language' => 'en',
'style_color' => 'blue',
'menu_color' => 'white',
];
}
public function styleOptions(): array
{
return ['blue', 'green', 'red', 'orange', 'gray', 'black', 'white'];
}
public function menuOptions(): array
{
return ['white', 'gray', 'black', 'blue', 'green'];
}
public function normalizeStyle(?string $style): ?string
{
if (!$style) {
return null;
}
$style = trim($style);
return in_array($style, $this->styleOptions(), true) ? $style : null;
}
public function normalizeMenu(?string $menu): ?string
{
if (!$menu) {
return null;
}
$menu = trim($menu);
return in_array($menu, $this->menuOptions(), true) ? $menu : null;
}
}
@@ -0,0 +1,83 @@
<?php
namespace App\Services\Preferences;
use App\Models\Preferences;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
class PreferencesQueryService
{
public function __construct(private PreferencesOptionsService $options)
{
}
public function getForUser(int $userId): array
{
$row = Preferences::query()->where('user_id', $userId)->first();
$base = $this->options->defaultPreferences();
if (!$row) {
return [
'preferences' => $base,
'options' => $this->optionsPayload(),
];
}
return [
'preferences' => array_merge($base, $this->mapRow($row)),
'options' => $this->optionsPayload(),
];
}
public function paginate(array $filters, int $page = 1, int $perPage = 20): LengthAwarePaginator
{
$query = Preferences::query()
->leftJoin('users', 'users.id', '=', 'user_preferences.user_id')
->select('user_preferences.*', 'users.firstname', 'users.lastname', 'users.email');
if (!empty($filters['user_id'])) {
$query->where('user_preferences.user_id', (int) $filters['user_id']);
}
if (!empty($filters['q'])) {
$term = '%' . strtolower(trim((string) $filters['q'])) . '%';
$query->where(function ($q) use ($term) {
$q->whereRaw('LOWER(users.firstname) LIKE ?', [$term])
->orWhereRaw('LOWER(users.lastname) LIKE ?', [$term])
->orWhereRaw('LOWER(users.email) LIKE ?', [$term]);
});
}
$sortBy = (string) ($filters['sort_by'] ?? 'updated_at');
$sortDir = strtolower((string) ($filters['sort_dir'] ?? 'desc')) === 'asc' ? 'asc' : 'desc';
$allowedSorts = ['updated_at', 'created_at', 'user_id'];
if (in_array($sortBy, $allowedSorts, true)) {
$query->orderBy('user_preferences.' . $sortBy, $sortDir);
} else {
$query->orderBy('user_preferences.updated_at', 'desc');
}
return $query->paginate($perPage, ['*'], 'page', $page);
}
private function optionsPayload(): array
{
return [
'style_options' => $this->options->styleOptions(),
'menu_options' => $this->options->menuOptions(),
];
}
private function mapRow(Preferences $row): array
{
return [
'receive_email_notifications' => (bool) ($row->notification_email ?? false),
'receive_sms_notifications' => (bool) ($row->notification_sms ?? false),
'theme' => $row->theme ?? null,
'language' => $row->language ?? null,
'style_color' => $row->style_color ?? null,
'menu_color' => $row->menu_color ?? null,
];
}
}