update deploy

This commit is contained in:
bulut
2026-03-27 10:41:54 +03:00
parent 69d19c0176
commit 6f6448aa06
422 changed files with 37956 additions and 0 deletions

View File

@@ -0,0 +1,20 @@
<?php
namespace App\Repositories\Contracts;
use App\Models\Announcement;
use Illuminate\Pagination\LengthAwarePaginator;
/**
* @extends BaseRepositoryInterface<Announcement>
*/
interface AnnouncementRepositoryInterface extends BaseRepositoryInterface
{
public function findBySlug(string $slug): ?Announcement;
/**
* @param array<string, mixed> $filters
* @return LengthAwarePaginator<int, Announcement>
*/
public function paginate(array $filters = [], int $perPage = 15): LengthAwarePaginator;
}

View File

@@ -0,0 +1,37 @@
<?php
namespace App\Repositories\Contracts;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;
/**
* @template T of Model
*/
interface BaseRepositoryInterface
{
/**
* @param array<string, mixed> $filters
* @return LengthAwarePaginator<int, T>
*/
public function paginate(array $filters = [], int $perPage = 15): LengthAwarePaginator;
/**
* @return T|null
*/
public function findById(int $id): ?Model;
/**
* @param array<string, mixed> $data
* @return T
*/
public function create(array $data): Model;
/**
* @param array<string, mixed> $data
* @return T
*/
public function update(Model $model, array $data): Model;
public function delete(Model $model): bool;
}

View File

@@ -0,0 +1,20 @@
<?php
namespace App\Repositories\Contracts;
use App\Models\Category;
use Illuminate\Pagination\LengthAwarePaginator;
/**
* @extends BaseRepositoryInterface<Category>
*/
interface CategoryRepositoryInterface extends BaseRepositoryInterface
{
public function findBySlug(string $slug): ?Category;
/**
* @param array<string, mixed> $filters
* @return LengthAwarePaginator<int, Category>
*/
public function paginate(array $filters = [], int $perPage = 15): LengthAwarePaginator;
}

View File

@@ -0,0 +1,24 @@
<?php
namespace App\Repositories\Contracts;
use App\Models\Comment;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Pagination\LengthAwarePaginator;
/**
* @extends BaseRepositoryInterface<Comment>
*/
interface CommentRepositoryInterface extends BaseRepositoryInterface
{
/**
* @param array<string, mixed> $filters
* @return LengthAwarePaginator<int, Comment>
*/
public function paginate(array $filters = [], int $perPage = 15): LengthAwarePaginator;
/**
* @return Collection<int, Comment>
*/
public function getApprovedByCommentable(string $commentableType, int $commentableId): Collection;
}

View File

@@ -0,0 +1,20 @@
<?php
namespace App\Repositories\Contracts;
use App\Models\Course;
use Illuminate\Pagination\LengthAwarePaginator;
/**
* @extends BaseRepositoryInterface<Course>
*/
interface CourseRepositoryInterface extends BaseRepositoryInterface
{
public function findBySlug(string $slug): ?Course;
/**
* @param array<string, mixed> $filters
* @return LengthAwarePaginator<int, Course>
*/
public function paginate(array $filters = [], int $perPage = 15): LengthAwarePaginator;
}

View File

@@ -0,0 +1,25 @@
<?php
namespace App\Repositories\Contracts;
use App\Enums\FaqCategory;
use App\Models\Faq;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Pagination\LengthAwarePaginator;
/**
* @extends BaseRepositoryInterface<Faq>
*/
interface FaqRepositoryInterface extends BaseRepositoryInterface
{
/**
* @param array<string, mixed> $filters
* @return LengthAwarePaginator<int, Faq>
*/
public function paginate(array $filters = [], int $perPage = 15): LengthAwarePaginator;
/**
* @return Collection<int, Faq>
*/
public function getByCategory(FaqCategory $category): Collection;
}

View File

@@ -0,0 +1,24 @@
<?php
namespace App\Repositories\Contracts;
use App\Models\GuideCard;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Pagination\LengthAwarePaginator;
/**
* @extends BaseRepositoryInterface<GuideCard>
*/
interface GuideCardRepositoryInterface extends BaseRepositoryInterface
{
/**
* @param array<string, mixed> $filters
* @return LengthAwarePaginator<int, GuideCard>
*/
public function paginate(array $filters = [], int $perPage = 15): LengthAwarePaginator;
/**
* @return Collection<int, GuideCard>
*/
public function active(): Collection;
}

View File

@@ -0,0 +1,24 @@
<?php
namespace App\Repositories\Contracts;
use App\Models\HeroSlide;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Pagination\LengthAwarePaginator;
/**
* @extends BaseRepositoryInterface<HeroSlide>
*/
interface HeroSlideRepositoryInterface extends BaseRepositoryInterface
{
/**
* @param array<string, mixed> $filters
* @return LengthAwarePaginator<int, HeroSlide>
*/
public function paginate(array $filters = [], int $perPage = 15): LengthAwarePaginator;
/**
* @return Collection<int, HeroSlide>
*/
public function active(): Collection;
}

View File

@@ -0,0 +1,18 @@
<?php
namespace App\Repositories\Contracts;
use App\Models\Lead;
use Illuminate\Pagination\LengthAwarePaginator;
/**
* @extends BaseRepositoryInterface<Lead>
*/
interface LeadRepositoryInterface extends BaseRepositoryInterface
{
/**
* @param array<string, mixed> $filters
* @return LengthAwarePaginator<int, Lead>
*/
public function paginate(array $filters = [], int $perPage = 15): LengthAwarePaginator;
}

View File

@@ -0,0 +1,30 @@
<?php
namespace App\Repositories\Contracts;
use App\Enums\MenuLocation;
use App\Models\Menu;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Pagination\LengthAwarePaginator;
/**
* @extends BaseRepositoryInterface<Menu>
*/
interface MenuRepositoryInterface extends BaseRepositoryInterface
{
/**
* @param array<string, mixed> $filters
* @return LengthAwarePaginator<int, Menu>
*/
public function paginate(array $filters = [], int $perPage = 15): LengthAwarePaginator;
/**
* @return Collection<int, Menu>
*/
public function getByLocation(MenuLocation $location): Collection;
/**
* @param array<int, array{id: int, order_index: int, parent_id?: int|null}> $items
*/
public function reorder(array $items): void;
}

View File

@@ -0,0 +1,20 @@
<?php
namespace App\Repositories\Contracts;
use App\Models\Page;
use Illuminate\Pagination\LengthAwarePaginator;
/**
* @extends BaseRepositoryInterface<Page>
*/
interface PageRepositoryInterface extends BaseRepositoryInterface
{
public function findBySlug(string $slug): ?Page;
/**
* @param array<string, mixed> $filters
* @return LengthAwarePaginator<int, Page>
*/
public function paginate(array $filters = [], int $perPage = 15): LengthAwarePaginator;
}

View File

@@ -0,0 +1,24 @@
<?php
namespace App\Repositories\Contracts;
use App\Models\CourseSchedule;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Pagination\LengthAwarePaginator;
/**
* @extends BaseRepositoryInterface<CourseSchedule>
*/
interface ScheduleRepositoryInterface extends BaseRepositoryInterface
{
/**
* @param array<string, mixed> $filters
* @return LengthAwarePaginator<int, CourseSchedule>
*/
public function paginate(array $filters = [], int $perPage = 15): LengthAwarePaginator;
/**
* @return Collection<int, CourseSchedule>
*/
public function upcoming(int $limit = 20): Collection;
}

View File

@@ -0,0 +1,51 @@
<?php
namespace App\Repositories\Contracts;
use App\Enums\SettingGroup;
use App\Models\Setting;
use Illuminate\Database\Eloquent\Collection;
/**
* @extends BaseRepositoryInterface<Setting>
*/
interface SettingRepositoryInterface extends BaseRepositoryInterface
{
/**
* @return Collection<int, Setting>
*/
public function all(): Collection;
/**
* Get all public settings grouped by group name (nested key-value format).
*
* @return array<string, array<string, mixed>>
*/
public function publicGrouped(): array;
/**
* Get public settings for a single group (key-value format).
*
* @return array<string, mixed>
*/
public function publicByGroup(SettingGroup $group): array;
public function findByKey(string $key): ?Setting;
/**
* @return Collection<int, Setting>
*/
public function getByGroup(SettingGroup $group): Collection;
/**
* Bulk update settings using dot notation keys (e.g. "general.site_name").
*
* @param array<string, mixed> $settings
*/
public function bulkUpdate(array $settings): void;
/**
* Clear all setting caches.
*/
public function clearCache(): void;
}

View File

@@ -0,0 +1,20 @@
<?php
namespace App\Repositories\Contracts;
use App\Models\User;
use Illuminate\Pagination\LengthAwarePaginator;
/**
* @extends BaseRepositoryInterface<User>
*/
interface UserRepositoryInterface extends BaseRepositoryInterface
{
/**
* @param array<string, mixed> $filters
* @return LengthAwarePaginator<int, User>
*/
public function paginate(array $filters = [], int $perPage = 15): LengthAwarePaginator;
public function findByEmail(string $email): ?User;
}

View File

@@ -0,0 +1,60 @@
<?php
namespace App\Repositories\Eloquent;
use App\Models\Announcement;
use App\Repositories\Contracts\AnnouncementRepositoryInterface;
use Illuminate\Pagination\LengthAwarePaginator;
/**
* @extends BaseRepository<Announcement>
*/
class AnnouncementRepository extends BaseRepository implements AnnouncementRepositoryInterface
{
public function __construct(Announcement $model)
{
parent::__construct($model);
}
public function findBySlug(string $slug): ?Announcement
{
return $this->model->newQuery()->where('slug', $slug)->first();
}
/**
* @param array<string, mixed> $filters
* @return LengthAwarePaginator<int, Announcement>
*/
public function paginate(array $filters = [], int $perPage = 15): LengthAwarePaginator
{
$query = $this->model->newQuery();
if (! empty($filters['category'])) {
$query->where('category', $filters['category']);
}
if (! empty($filters['featured'])) {
$query->where('is_featured', true);
}
if (! empty($filters['search'])) {
$query->where(function ($q) use ($filters) {
$q->where('title', 'like', '%'.$filters['search'].'%')
->orWhere('excerpt', 'like', '%'.$filters['search'].'%');
});
}
$sortField = $filters['sort'] ?? '-published_at';
$direction = str_starts_with($sortField, '-') ? 'desc' : 'asc';
$sortField = ltrim($sortField, '-');
$allowedSorts = ['title', 'published_at', 'created_at'];
if (in_array($sortField, $allowedSorts)) {
$query->orderBy($sortField, $direction);
} else {
$query->latest('published_at');
}
return $query->paginate($perPage);
}
}

View File

@@ -0,0 +1,62 @@
<?php
namespace App\Repositories\Eloquent;
use App\Repositories\Contracts\BaseRepositoryInterface;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;
/**
* @template T of Model
*
* @implements BaseRepositoryInterface<T>
*/
abstract class BaseRepository implements BaseRepositoryInterface
{
/**
* @param T $model
*/
public function __construct(protected Model $model) {}
/**
* @param array<string, mixed> $filters
* @return LengthAwarePaginator<int, T>
*/
public function paginate(array $filters = [], int $perPage = 15): LengthAwarePaginator
{
return $this->model->newQuery()->latest()->paginate($perPage);
}
/**
* @return T|null
*/
public function findById(int $id): ?Model
{
return $this->model->newQuery()->find($id);
}
/**
* @param array<string, mixed> $data
* @return T
*/
public function create(array $data): Model
{
return $this->model->newQuery()->create($data);
}
/**
* @param array<string, mixed> $data
* @return T
*/
public function update(Model $model, array $data): Model
{
$model->update($data);
return $model->fresh();
}
public function delete(Model $model): bool
{
return $model->delete();
}
}

View File

@@ -0,0 +1,38 @@
<?php
namespace App\Repositories\Eloquent;
use App\Models\Category;
use App\Repositories\Contracts\CategoryRepositoryInterface;
use Illuminate\Pagination\LengthAwarePaginator;
/**
* @extends BaseRepository<Category>
*/
class CategoryRepository extends BaseRepository implements CategoryRepositoryInterface
{
public function __construct(Category $model)
{
parent::__construct($model);
}
public function findBySlug(string $slug): ?Category
{
return $this->model->newQuery()->with('menuCourses')->where('slug', $slug)->first();
}
/**
* @param array<string, mixed> $filters
* @return LengthAwarePaginator<int, Category>
*/
public function paginate(array $filters = [], int $perPage = 15): LengthAwarePaginator
{
$query = $this->model->newQuery()->with('menuCourses');
if (! empty($filters['search'])) {
$query->where('label', 'like', '%'.$filters['search'].'%');
}
return $query->latest()->paginate($perPage);
}
}

View File

@@ -0,0 +1,58 @@
<?php
namespace App\Repositories\Eloquent;
use App\Models\Comment;
use App\Repositories\Contracts\CommentRepositoryInterface;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Pagination\LengthAwarePaginator;
/**
* @extends BaseRepository<Comment>
*/
class CommentRepository extends BaseRepository implements CommentRepositoryInterface
{
public function __construct(Comment $model)
{
parent::__construct($model);
}
/**
* @param array<string, mixed> $filters
* @return LengthAwarePaginator<int, Comment>
*/
public function paginate(array $filters = [], int $perPage = 15): LengthAwarePaginator
{
$query = $this->model->newQuery();
if (isset($filters['is_approved'])) {
$query->where('is_approved', filter_var($filters['is_approved'], FILTER_VALIDATE_BOOLEAN));
}
if (! empty($filters['commentable_type'])) {
$query->where('commentable_type', $filters['commentable_type']);
}
if (! empty($filters['search'])) {
$query->where(function ($q) use ($filters) {
$q->where('author_name', 'like', '%'.$filters['search'].'%')
->orWhere('content', 'like', '%'.$filters['search'].'%');
});
}
return $query->latest()->paginate($perPage);
}
/**
* @return Collection<int, Comment>
*/
public function getApprovedByCommentable(string $commentableType, int $commentableId): Collection
{
return $this->model->newQuery()
->where('commentable_type', $commentableType)
->where('commentable_id', $commentableId)
->where('is_approved', true)
->latest()
->get();
}
}

View File

@@ -0,0 +1,59 @@
<?php
namespace App\Repositories\Eloquent;
use App\Models\Course;
use App\Repositories\Contracts\CourseRepositoryInterface;
use Illuminate\Pagination\LengthAwarePaginator;
/**
* @extends BaseRepository<Course>
*/
class CourseRepository extends BaseRepository implements CourseRepositoryInterface
{
public function __construct(Course $model)
{
parent::__construct($model);
}
public function findBySlug(string $slug): ?Course
{
return $this->model->newQuery()
->with(['category', 'schedules', 'blocks'])
->where('slug', $slug)
->first();
}
/**
* @param array<string, mixed> $filters
* @return LengthAwarePaginator<int, Course>
*/
public function paginate(array $filters = [], int $perPage = 15): LengthAwarePaginator
{
$query = $this->model->newQuery()->with('category');
if (! empty($filters['category'])) {
$query->whereHas('category', fn ($q) => $q->where('slug', $filters['category']));
}
if (! empty($filters['search'])) {
$query->where(function ($q) use ($filters) {
$q->where('title', 'like', '%'.$filters['search'].'%')
->orWhere('desc', 'like', '%'.$filters['search'].'%');
});
}
$sortField = $filters['sort'] ?? '-created_at';
$direction = str_starts_with($sortField, '-') ? 'desc' : 'asc';
$sortField = ltrim($sortField, '-');
$allowedSorts = ['title', 'created_at', 'students', 'rating'];
if (in_array($sortField, $allowedSorts)) {
$query->orderBy($sortField, $direction);
} else {
$query->latest();
}
return $query->paginate($perPage);
}
}

View File

@@ -0,0 +1,48 @@
<?php
namespace App\Repositories\Eloquent;
use App\Enums\FaqCategory;
use App\Models\Faq;
use App\Repositories\Contracts\FaqRepositoryInterface;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Cache;
/**
* @extends BaseRepository<Faq>
*/
class FaqRepository extends BaseRepository implements FaqRepositoryInterface
{
public function __construct(Faq $model)
{
parent::__construct($model);
}
/**
* @param array<string, mixed> $filters
* @return LengthAwarePaginator<int, Faq>
*/
public function paginate(array $filters = [], int $perPage = 15): LengthAwarePaginator
{
$query = $this->model->newQuery();
if (! empty($filters['category'])) {
$query->where('category', $filters['category']);
}
return $query->orderBy('order_index')->paginate($perPage);
}
/**
* @return Collection<int, Faq>
*/
public function getByCategory(FaqCategory $category): Collection
{
return Cache::remember("faqs.{$category->value}", 3600, fn () => $this->model->newQuery()
->where('category', $category)
->where('is_active', true)
->orderBy('order_index')
->get());
}
}

View File

@@ -0,0 +1,42 @@
<?php
namespace App\Repositories\Eloquent;
use App\Models\GuideCard;
use App\Repositories\Contracts\GuideCardRepositoryInterface;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Cache;
/**
* @extends BaseRepository<GuideCard>
*/
class GuideCardRepository extends BaseRepository implements GuideCardRepositoryInterface
{
public function __construct(GuideCard $model)
{
parent::__construct($model);
}
/**
* @param array<string, mixed> $filters
* @return LengthAwarePaginator<int, GuideCard>
*/
public function paginate(array $filters = [], int $perPage = 15): LengthAwarePaginator
{
return $this->model->newQuery()
->orderBy('order_index')
->paginate($perPage);
}
/**
* @return Collection<int, GuideCard>
*/
public function active(): Collection
{
return Cache::remember('guide_cards', 3600, fn () => $this->model->newQuery()
->where('is_active', true)
->orderBy('order_index')
->get());
}
}

View File

@@ -0,0 +1,42 @@
<?php
namespace App\Repositories\Eloquent;
use App\Models\HeroSlide;
use App\Repositories\Contracts\HeroSlideRepositoryInterface;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Cache;
/**
* @extends BaseRepository<HeroSlide>
*/
class HeroSlideRepository extends BaseRepository implements HeroSlideRepositoryInterface
{
public function __construct(HeroSlide $model)
{
parent::__construct($model);
}
/**
* @param array<string, mixed> $filters
* @return LengthAwarePaginator<int, HeroSlide>
*/
public function paginate(array $filters = [], int $perPage = 15): LengthAwarePaginator
{
return $this->model->newQuery()
->orderBy('order_index')
->paginate($perPage);
}
/**
* @return Collection<int, HeroSlide>
*/
public function active(): Collection
{
return Cache::remember('hero_slides', 3600, fn () => $this->model->newQuery()
->where('is_active', true)
->orderBy('order_index')
->get());
}
}

View File

@@ -0,0 +1,48 @@
<?php
namespace App\Repositories\Eloquent;
use App\Models\Lead;
use App\Repositories\Contracts\LeadRepositoryInterface;
use Illuminate\Pagination\LengthAwarePaginator;
/**
* @extends BaseRepository<Lead>
*/
class LeadRepository extends BaseRepository implements LeadRepositoryInterface
{
public function __construct(Lead $model)
{
parent::__construct($model);
}
/**
* @param array<string, mixed> $filters
* @return LengthAwarePaginator<int, Lead>
*/
public function paginate(array $filters = [], int $perPage = 15): LengthAwarePaginator
{
$query = $this->model->newQuery();
if (! empty($filters['status'])) {
$query->where('status', $filters['status']);
}
if (! empty($filters['source'])) {
$query->where('source', $filters['source']);
}
if (isset($filters['is_read'])) {
$query->where('is_read', filter_var($filters['is_read'], FILTER_VALIDATE_BOOLEAN));
}
if (! empty($filters['search'])) {
$query->where(function ($q) use ($filters) {
$q->where('name', 'like', '%'.$filters['search'].'%')
->orWhere('phone', 'like', '%'.$filters['search'].'%');
});
}
return $query->latest()->paginate($perPage);
}
}

View File

@@ -0,0 +1,68 @@
<?php
namespace App\Repositories\Eloquent;
use App\Enums\MenuLocation;
use App\Models\Menu;
use App\Repositories\Contracts\MenuRepositoryInterface;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Cache;
/**
* @extends BaseRepository<Menu>
*/
class MenuRepository extends BaseRepository implements MenuRepositoryInterface
{
public function __construct(Menu $model)
{
parent::__construct($model);
}
/**
* @param array<string, mixed> $filters
* @return LengthAwarePaginator<int, Menu>
*/
public function paginate(array $filters = [], int $perPage = 15): LengthAwarePaginator
{
$query = $this->model->newQuery();
if (! empty($filters['location'])) {
$query->where('location', $filters['location']);
}
return $query->whereNull('parent_id')
->with('children')
->orderBy('order_index')
->paginate($perPage);
}
/**
* @return Collection<int, Menu>
*/
public function getByLocation(MenuLocation $location): Collection
{
return Cache::remember("menus.{$location->value}", 3600, fn () => $this->model->newQuery()
->where('location', $location)
->where('is_active', true)
->whereNull('parent_id')
->with(['children' => fn ($q) => $q->where('is_active', true)->orderBy('order_index')])
->orderBy('order_index')
->get());
}
/**
* @param array<int, array{id: int, order_index: int, parent_id?: int|null}> $items
*/
public function reorder(array $items): void
{
foreach ($items as $item) {
$this->model->newQuery()
->where('id', $item['id'])
->update([
'order_index' => $item['order_index'],
'parent_id' => $item['parent_id'] ?? null,
]);
}
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace App\Repositories\Eloquent;
use App\Models\Page;
use App\Repositories\Contracts\PageRepositoryInterface;
use Illuminate\Pagination\LengthAwarePaginator;
/**
* @extends BaseRepository<Page>
*/
class PageRepository extends BaseRepository implements PageRepositoryInterface
{
public function __construct(Page $model)
{
parent::__construct($model);
}
public function findBySlug(string $slug): ?Page
{
return $this->model->newQuery()
->with('blocks')
->where('slug', $slug)
->first();
}
/**
* @param array<string, mixed> $filters
* @return LengthAwarePaginator<int, Page>
*/
public function paginate(array $filters = [], int $perPage = 15): LengthAwarePaginator
{
$query = $this->model->newQuery();
if (! empty($filters['search'])) {
$query->where('title', 'like', '%'.$filters['search'].'%');
}
return $query->latest()->paginate($perPage);
}
}

View File

@@ -0,0 +1,47 @@
<?php
namespace App\Repositories\Eloquent;
use App\Models\CourseSchedule;
use App\Repositories\Contracts\ScheduleRepositoryInterface;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Pagination\LengthAwarePaginator;
/**
* @extends BaseRepository<CourseSchedule>
*/
class ScheduleRepository extends BaseRepository implements ScheduleRepositoryInterface
{
public function __construct(CourseSchedule $model)
{
parent::__construct($model);
}
/**
* @param array<string, mixed> $filters
* @return LengthAwarePaginator<int, CourseSchedule>
*/
public function paginate(array $filters = [], int $perPage = 15): LengthAwarePaginator
{
$query = $this->model->newQuery()->with('course.category');
if (! empty($filters['course_id'])) {
$query->where('course_id', $filters['course_id']);
}
return $query->orderBy('start_date')->paginate($perPage);
}
/**
* @return Collection<int, CourseSchedule>
*/
public function upcoming(int $limit = 20): Collection
{
return $this->model->newQuery()
->with('course.category')
->where('start_date', '>=', now())
->orderBy('start_date')
->limit($limit)
->get();
}
}

View File

@@ -0,0 +1,108 @@
<?php
namespace App\Repositories\Eloquent;
use App\Enums\SettingGroup;
use App\Models\Setting;
use App\Repositories\Contracts\SettingRepositoryInterface;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Cache;
/**
* @extends BaseRepository<Setting>
*/
class SettingRepository extends BaseRepository implements SettingRepositoryInterface
{
public function __construct(Setting $model)
{
parent::__construct($model);
}
/**
* @return Collection<int, Setting>
*/
public function all(): Collection
{
return $this->model->newQuery()
->orderBy('group')
->orderBy('order_index')
->get();
}
/**
* @return array<string, array<string, mixed>>
*/
public function publicGrouped(): array
{
return Cache::remember('site_settings_all', 3600, function () {
return $this->model->newQuery()
->where('is_public', true)
->orderBy('group')
->orderBy('order_index')
->get()
->filter(fn (Setting $s) => ! $s->isSensitive())
->groupBy(fn (Setting $s) => $s->group->value)
->map(fn ($group) => $group->pluck('value', 'key')->all())
->all();
});
}
/**
* @return array<string, mixed>
*/
public function publicByGroup(SettingGroup $group): array
{
return Cache::remember("site_settings_{$group->value}", 3600, function () use ($group) {
return $this->model->newQuery()
->where('group', $group)
->where('is_public', true)
->orderBy('order_index')
->get()
->filter(fn (Setting $s) => ! $s->isSensitive())
->pluck('value', 'key')
->all();
});
}
public function findByKey(string $key): ?Setting
{
return $this->model->newQuery()->where('key', $key)->first();
}
/**
* @return Collection<int, Setting>
*/
public function getByGroup(SettingGroup $group): Collection
{
return $this->model->newQuery()
->where('group', $group)
->orderBy('order_index')
->get();
}
/**
* @param array<string, mixed> $settings
*/
public function bulkUpdate(array $settings): void
{
foreach ($settings as $dotKey => $value) {
[$group, $key] = explode('.', $dotKey, 2);
$this->model->newQuery()
->where('group', $group)
->where('key', $key)
->update(['value' => $value]);
}
$this->clearCache();
}
public function clearCache(): void
{
Cache::forget('site_settings_all');
foreach (SettingGroup::cases() as $group) {
Cache::forget("site_settings_{$group->value}");
}
}
}

View File

@@ -0,0 +1,46 @@
<?php
namespace App\Repositories\Eloquent;
use App\Models\User;
use App\Repositories\Contracts\UserRepositoryInterface;
use Illuminate\Pagination\LengthAwarePaginator;
/**
* @extends BaseRepository<User>
*/
class UserRepository extends BaseRepository implements UserRepositoryInterface
{
public function __construct(User $model)
{
parent::__construct($model);
}
/**
* @param array<string, mixed> $filters
* @return LengthAwarePaginator<int, User>
*/
public function paginate(array $filters = [], int $perPage = 15): LengthAwarePaginator
{
$query = $this->model->newQuery()->with('roles');
if (! empty($filters['search'])) {
$search = $filters['search'];
$query->where(function ($q) use ($search) {
$q->where('name', 'like', "%{$search}%")
->orWhere('email', 'like', "%{$search}%");
});
}
if (! empty($filters['role'])) {
$query->role($filters['role']);
}
return $query->latest()->paginate($perPage);
}
public function findByEmail(string $email): ?User
{
return $this->model->newQuery()->where('email', $email)->first();
}
}