You've already forked Atomcms-edit
Initial commit
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Community\RareValues;
|
||||
|
||||
use App\Models\Community\RareValue\WebsiteRareValueCategory;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
readonly class RareValueCategoriesService
|
||||
{
|
||||
private const int CACHE_TTL = 300; // 5 minutes
|
||||
|
||||
private const int PAGINATION_PER_PAGE = 12;
|
||||
|
||||
public function fetchAllCategories(): Collection
|
||||
{
|
||||
return Cache::remember('rare_categories_all', self::CACHE_TTL, fn () => WebsiteRareValueCategory::orderBy('priority')->get());
|
||||
}
|
||||
|
||||
public function fetchCategoriesByPriority(): Builder|Collection
|
||||
{
|
||||
return Cache::remember('rare_categories_priority', self::CACHE_TTL, fn () => WebsiteRareValueCategory::query()
|
||||
->orderBy('priority')
|
||||
->with(['furniture' => function ($query) {
|
||||
$query->orderBy('name');
|
||||
}])
|
||||
->get());
|
||||
}
|
||||
|
||||
public function fetchCategoryById(int $id): ?WebsiteRareValueCategory
|
||||
{
|
||||
return Cache::remember("rare_category_{$id}", self::CACHE_TTL, function () use ($id) {
|
||||
/** @var WebsiteRareValueCategory|null $result */
|
||||
$result = WebsiteRareValueCategory::with(['furniture' => function ($query) {
|
||||
$query->orderBy('name');
|
||||
}])
|
||||
->where('id', $id)
|
||||
->first();
|
||||
|
||||
return $result;
|
||||
});
|
||||
}
|
||||
|
||||
public function searchCategories(string $searchTerm): Collection
|
||||
{
|
||||
$cacheKey = 'rare_search_' . md5($searchTerm);
|
||||
|
||||
return Cache::remember($cacheKey, self::CACHE_TTL, fn () => WebsiteRareValueCategory::with(['furniture' => function ($query) use ($searchTerm) {
|
||||
$query->where('name', 'like', '%' . $searchTerm . '%')
|
||||
->orderBy('name');
|
||||
}])
|
||||
->whereHas('furniture', function ($query) use ($searchTerm) {
|
||||
$query->where('name', 'like', '%' . $searchTerm . '%');
|
||||
})
|
||||
->orderBy('priority')
|
||||
->get());
|
||||
}
|
||||
|
||||
public function getRaresWithPagination(int $categoryId, string $sortBy = 'name', string $sortOrder = 'asc'): LengthAwarePaginator
|
||||
{
|
||||
$cacheKey = "rare_category_{$categoryId}_page_" . request('page', 1) . "_{$sortBy}_{$sortOrder}";
|
||||
|
||||
return Cache::remember($cacheKey, self::CACHE_TTL, fn () => WebsiteRareValueCategory::find($categoryId)
|
||||
?->furniture()
|
||||
->orderBy($sortBy, $sortOrder)
|
||||
->paginate(self::PAGINATION_PER_PAGE) ?? collect()->paginate(self::PAGINATION_PER_PAGE));
|
||||
}
|
||||
|
||||
public function getRareStatistics(): array
|
||||
{
|
||||
return Cache::remember('rare_statistics', self::CACHE_TTL, function () {
|
||||
$categories = WebsiteRareValueCategory::withCount('furniture')->get();
|
||||
|
||||
return [
|
||||
'total_categories' => $categories->count(),
|
||||
'total_rares' => $categories->sum('furniture_count'),
|
||||
'most_valuable_category' => $categories->sortByDesc('furniture_count')->first()?->name ?? 'N/A',
|
||||
'average_rares_per_category' => $categories->avg('furniture_count') ?? 0,
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
public function clearCache(): void
|
||||
{
|
||||
Cache::forget('rare_categories_all');
|
||||
Cache::forget('rare_categories_priority');
|
||||
Cache::forget('rare_statistics');
|
||||
|
||||
// Clear individual category caches
|
||||
WebsiteRareValueCategory::pluck('id')->each(function ($id) {
|
||||
Cache::forget("rare_category_{$id}");
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user