Files
ahrommag/routes/api.php
2025-11-16 12:43:07 +03:30

122 lines
4.1 KiB
PHP

<?php
use App\Models\Blog;
use App\Models\Category;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Api\BlogLast;
use App\Http\Controllers\Api\NewsAhrom;
use App\Http\Controllers\Api\NewsController;
use App\Http\Controllers\Api\BlogsController;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "api" middleware group. Make something great!
|
*/
// Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
// return $request->user();
// });
Route::get('/news', [NewsController::class, 'index']);
Route::get('/blogs', [BlogsController::class, 'index']);
Route::get('/blogLast', [BlogLast::class, 'yesterdayPosts']);
Route::get('/news_ahrom', [NewsAhrom::class, 'index']);
Route::get('/category-blogs/{categoryId}', function ($categoryId) {
$category = Category::findOrFail($categoryId);
$categoryIds = $category->getAllDescendantIds();
$categoryIds[] = $category->id;
$blogs = Blog::query()
->where('status', 1)
->whereNull('notBlog')
->where(function ($query) use ($categoryIds) {
$query->where(function ($q) use ($categoryIds) {
foreach ($categoryIds as $categoryId) {
$q->orWhereJsonContains('category_array', (string)$categoryId);
}
});
})
->orderByDesc('published_at')
->take(50)
->get(['slug', 'subject', 'image', 'published_at'])
->map(function ($blog) {
$blog->image = env('APP_URL_IMAGE') . 'images/' . $blog->image;
return $blog;
});
return response()->json([
'blogs' => $blogs,
'category_id' => $categoryId
]);
});
Route::get('/categoryTabs/{categoryId}', function ($categoryId) {
$category = Category::findOrFail($categoryId);
$allBlogs = $category->blogs()->where('status', 1)->orderBy('published_at', 'desc')->get();
$latestBlogs = $allBlogs->whereNull('notBlog')->take(15)->map(function ($item) {
return [
'id' => $item->id,
'subject' => $item->subject,
'slug' => $item->slug,
'image' => $item->image,
'category_id' => $item->category_id,
'published_at' => jdate($item->published_at)->ago(), // <-- HERE
];
})->select('id', 'subject', 'slug', 'image', 'published_at', 'category_id');
return response()->json([
'blogs' => $latestBlogs,
'category_id' => $categoryId
]);
});
Route::get('/category/{categoryId}', function (Request $request, $categoryId) {
$resArray = [];
$is_news = $request->get('is_news') ?? 0;
if ($is_news) {
$operation = "!=";
} else {
$operation = "=";
}
$parentCategories = Category::where('parent_id', $operation, null)
->whereNull('parent_below')
->where('status', 1)
->where('is_news', $is_news ? 1 : 0)
->orderBy('title')
->get();
$resArray['parents'] = $parentCategories;
// $query = Category::query();
// if ($this->is_news) {
// $query->where('is_news', 1);
// } else {
// $query->where('is_news', 0);
// }
if ($categoryId) {
$SCategory = Category::findOrFail($categoryId);
$resArray['selected_parent'] = $SCategory;
$resArray['sub_category'] = $SCategory->children()->orderBy('priority', 'desc')->get();
}
// $SCategory = Category::findOrFail($categoryId);
return response()->json($resArray);
});
Route::get('/sub-category/{categoryId}', function (Request $request, $categoryId) {
$resArray = [];
$is_news = $request->get('is_news') ?? 0;
if ($categoryId) {
$SSCategory = Category::findOrFail($categoryId);
$resArray['sub_selected_parent'] = $SSCategory;
$resArray['sub_sub_category'] = $SSCategory->children()->orderBy('priority', 'desc')->get();
}
return response()->json($resArray);
});