update deploy
This commit is contained in:
60
app/Repositories/Eloquent/AnnouncementRepository.php
Normal file
60
app/Repositories/Eloquent/AnnouncementRepository.php
Normal 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);
|
||||
}
|
||||
}
|
||||
62
app/Repositories/Eloquent/BaseRepository.php
Normal file
62
app/Repositories/Eloquent/BaseRepository.php
Normal 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();
|
||||
}
|
||||
}
|
||||
38
app/Repositories/Eloquent/CategoryRepository.php
Normal file
38
app/Repositories/Eloquent/CategoryRepository.php
Normal 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);
|
||||
}
|
||||
}
|
||||
58
app/Repositories/Eloquent/CommentRepository.php
Normal file
58
app/Repositories/Eloquent/CommentRepository.php
Normal 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();
|
||||
}
|
||||
}
|
||||
59
app/Repositories/Eloquent/CourseRepository.php
Normal file
59
app/Repositories/Eloquent/CourseRepository.php
Normal 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);
|
||||
}
|
||||
}
|
||||
48
app/Repositories/Eloquent/FaqRepository.php
Normal file
48
app/Repositories/Eloquent/FaqRepository.php
Normal 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());
|
||||
}
|
||||
}
|
||||
42
app/Repositories/Eloquent/GuideCardRepository.php
Normal file
42
app/Repositories/Eloquent/GuideCardRepository.php
Normal 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());
|
||||
}
|
||||
}
|
||||
42
app/Repositories/Eloquent/HeroSlideRepository.php
Normal file
42
app/Repositories/Eloquent/HeroSlideRepository.php
Normal 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());
|
||||
}
|
||||
}
|
||||
48
app/Repositories/Eloquent/LeadRepository.php
Normal file
48
app/Repositories/Eloquent/LeadRepository.php
Normal 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);
|
||||
}
|
||||
}
|
||||
68
app/Repositories/Eloquent/MenuRepository.php
Normal file
68
app/Repositories/Eloquent/MenuRepository.php
Normal 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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
41
app/Repositories/Eloquent/PageRepository.php
Normal file
41
app/Repositories/Eloquent/PageRepository.php
Normal 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);
|
||||
}
|
||||
}
|
||||
47
app/Repositories/Eloquent/ScheduleRepository.php
Normal file
47
app/Repositories/Eloquent/ScheduleRepository.php
Normal 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();
|
||||
}
|
||||
}
|
||||
108
app/Repositories/Eloquent/SettingRepository.php
Normal file
108
app/Repositories/Eloquent/SettingRepository.php
Normal 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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
46
app/Repositories/Eloquent/UserRepository.php
Normal file
46
app/Repositories/Eloquent/UserRepository.php
Normal 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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user