Added new api

This commit is contained in:
m.razaghi
2026-07-04 17:05:50 +03:30
parent f242b8936a
commit ce5ccc00a1
11 changed files with 1006 additions and 17 deletions

View File

@@ -19,6 +19,139 @@ class HomeController extends Controller
{
}
public function homeV2()
{
$user = auth()->user();
$this->refreshMarketSubscriptionAfterResponse($user);
$recent = RecentArt::query()->where('user_id', $user->id)->get()->map(function ($q) {
return [
'id' => $q->law?->id,
'name' => $q->law?->title,
'is_locked' => $q->law?->is_locked,
];
});
$laws = Law::orderBy('created_at')->get()->map(function ($q) {
return [
'id' => $q->id,
'name' => $q->title,
'is_locked' => $q->is_locked,
];
});
$categories = ['hagigi', 'kifari'];
$lawsByCategory = [];
foreach ($categories as $category) {
$lawsByCategory[$category] = Law::whereHas('category', function ($q) use ($category) {
$q->where('type', $category);
})->get()->map(function ($q) {
return [
'id' => $q->id,
'title' => $q->title,
'is_locked' => $q->is_locked,
'category_id' => $q->category_id,
'price' => $q->price,
'image' => $q->image,
'type' => 'law',
];
});
}
$free_law = Law::where('is_locked', false)->orderBy('created_at')->get()->map(function ($q) {
return [
'id' => $q->id,
'name' => $q->title,
'is_locked' => $q->is_locked,
];
});
$freeSubscription = $user->userSubscribers()
->whereHas('subscribe', function ($query) {
$query->where('is_free', true);
})
->where('expired_at', '>=', now())
->first();
$current_plan = null;
if ($freeSubscription) {
$current_plan = [
'id' => $freeSubscription->id,
'name' => $freeSubscription->subscribe->name,
'price' => $freeSubscription->subscribe->price,
'expired_day' => $freeSubscription->expired_at->diffInDays(now()),
'is_free' => true,
];
}
$latestSubscription = $user->userSubscribers()
->where('expired_at', '>=', now())
->latest('expired_at')
->first();
$expiredDays = UserSubscriber::query()->where('user_id', $user->id)->where('expired_at', '>=', now())->get()->sum(function ($subscriber) {
return $subscriber->expired_at->diffInDays(now());
});
if ($latestSubscription) {
$current_plan = [
'id' => $latestSubscription->id,
'name' => $latestSubscription->subscribe->name ?? 'Subscription',
'price' => $latestSubscription->subscribe->price ?? 100,
'expired_day' => $expiredDays,
'is_free' => (bool)$latestSubscription->is_free,
];
}
$unread_notifications_count = Notification::unreadForUser($user->id)->count();
$homeData = [
'recent' => $recent,
'laws' => $lawsByCategory,
'last_law' => $laws,
'free' => $free_law,
'current_plan' => $current_plan,
'unread_notifications_count' => $unread_notifications_count,
];
return $this->success([
'home' => $homeData,
'hoqoqi_list' => $this->getCategoryList(1), // Category 1
'keyfari_list' => $this->getCategoryList(2), // Category 2
]);
}
private function getCategoryList(int $categoryId)
{
$type = $categoryId === 1 ? 'hagigi' : 'kifari';
$categories = Category::with(['laws'])->where('type', $type)->get()->pluck('id');
$items = Law::query()->whereIn('category_id', $categories)->get();
$data = [];
$user = auth()->user();
foreach ($items as $item) {
$data[] = [
'id' => $item->id,
'title' => $item->title,
'number' => $item->number,
'type' => 'laws',
'is_locked' => $user->isSubscriber() !== false ? false : Law::where('id', $item->id)->first()?->is_locked,
'image' => $item?->image,
'law' => $item?->title,
'count_art' => $item->arts->count(),
'count_volums' => $item->volums->count(),
'price' => $item->price,
];
}
return $data;
}
public function index()
{
$user = auth()->user();
@@ -102,7 +235,7 @@ class HomeController extends Controller
'name' => $latestSubscription->subscribe->name ?? 'Subscription',
'price' => $latestSubscription->subscribe->price ?? 100,
'expired_day' => $expiredDays,
'is_free' => (bool) $latestSubscription->is_free,
'is_free' => (bool)$latestSubscription->is_free,
];
}
@@ -123,7 +256,7 @@ class HomeController extends Controller
$cacheKey = "home-market-refresh-user-{$user->id}";
$this->refreshMarketSubscription($user);
if (! Cache::add($cacheKey, true, now()->addHours(24))) {
if (!Cache::add($cacheKey, true, now()->addHours(24))) {
return;
}
@@ -166,6 +299,6 @@ class HomeController extends Controller
return true;
}
return ! $subscriber->last_verified_at || $subscriber->last_verified_at->lessThan(now()->subHours(12));
return !$subscriber->last_verified_at || $subscriber->last_verified_at->lessThan(now()->subHours(12));
}
}