feat: add sub category deep link
This commit is contained in:
@@ -177,7 +177,7 @@ public function publicShow($id)
|
||||
public function grouped($id)
|
||||
{
|
||||
$category = Category::with(['subcategories' => function ($q) {
|
||||
$q->orderBy('name')->with(['media' => function ($m) {
|
||||
$q->where('is_deeplink_only', false)->orderBy('name')->with(['media' => function ($m) {
|
||||
// Deep-link-only media stays out of the normal category screen.
|
||||
$m->where('is_deeplink_only', false)
|
||||
->with(['image', 'detailImage', 'tags'])->latest();
|
||||
|
||||
@@ -7,12 +7,21 @@
|
||||
|
||||
class SubCategoryController extends Controller
|
||||
{
|
||||
// Admin listing of deep-link (hidden) subcategories: ?deeplink_only=1 returns
|
||||
// only the hidden records so the admin can manage their links. The normal
|
||||
// listing hides deeplink-only subcategories.
|
||||
public function index(Request $request, $categoryId = null)
|
||||
{
|
||||
$query = SubCategory::with('category')->withCount(['media' => fn ($q) => $q->where('is_deeplink_only', false)]);
|
||||
|
||||
$categoryId = $categoryId ?? $request->input('category_id');
|
||||
|
||||
if ($request->boolean('deeplink_only')) {
|
||||
$query->where('is_deeplink_only', true);
|
||||
} else {
|
||||
$query->where('is_deeplink_only', false);
|
||||
}
|
||||
|
||||
if ($categoryId) {
|
||||
$query->where('category_id', $categoryId);
|
||||
}
|
||||
@@ -28,6 +37,7 @@ public function store(Request $request)
|
||||
'category_id' => 'required|exists:categories,id',
|
||||
'name' => 'required|string|max:255',
|
||||
'description' => 'nullable|string',
|
||||
'is_deeplink_only' => 'nullable|boolean',
|
||||
]);
|
||||
|
||||
$existing = SubCategory::where('category_id', $data['category_id'])
|
||||
@@ -41,6 +51,8 @@ public function store(Request $request)
|
||||
], 422);
|
||||
}
|
||||
|
||||
$data['is_deeplink_only'] = $data['is_deeplink_only'] ?? false;
|
||||
|
||||
$subCategory = SubCategory::create($data);
|
||||
|
||||
return response()->json([
|
||||
@@ -56,6 +68,38 @@ public function show($id)
|
||||
return response()->json($subCategory);
|
||||
}
|
||||
|
||||
// Public (no auth) deep link lookup by ID. Serves any subcategory — hidden
|
||||
// (deeplink-only) ones included — so https://aramejan.com/subcategory/{id}
|
||||
// resolves for every shareable subcategory. A visible subcategory only lists
|
||||
// its visible media; inside a hidden one everything is shown, since the deep
|
||||
// link is the only way in.
|
||||
public function publicShow($id)
|
||||
{
|
||||
$subCategory = SubCategory::with('category')->findOrFail($id);
|
||||
$includeHiddenMedia = (bool) $subCategory->is_deeplink_only;
|
||||
|
||||
$mediaQuery = $subCategory->media()
|
||||
->with(['image', 'detailImage', 'tags'])
|
||||
->latest();
|
||||
|
||||
if (! $includeHiddenMedia) {
|
||||
$mediaQuery->where('is_deeplink_only', false);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'sub_category' => [
|
||||
'id' => $subCategory->id,
|
||||
'name' => $subCategory->name,
|
||||
'description' => $subCategory->description,
|
||||
'category_id' => $subCategory->category_id,
|
||||
'category' => $subCategory->category,
|
||||
'is_deeplink_only' => $subCategory->is_deeplink_only,
|
||||
'deep_link' => $subCategory->deep_link,
|
||||
],
|
||||
'medias' => $mediaQuery->get(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
$subCategory = SubCategory::findOrFail($id);
|
||||
@@ -64,6 +108,7 @@ public function update(Request $request, $id)
|
||||
'category_id' => 'sometimes|exists:categories,id',
|
||||
'name' => 'sometimes|string|max:255',
|
||||
'description' => 'nullable|string',
|
||||
'is_deeplink_only' => 'nullable|boolean',
|
||||
]);
|
||||
|
||||
$categoryId = $data['category_id'] ?? $subCategory->category_id;
|
||||
@@ -81,6 +126,10 @@ public function update(Request $request, $id)
|
||||
], 422);
|
||||
}
|
||||
|
||||
if (array_key_exists('is_deeplink_only', $data)) {
|
||||
$subCategory->is_deeplink_only = $data['is_deeplink_only'];
|
||||
}
|
||||
|
||||
$subCategory->update($data);
|
||||
|
||||
return response()->json([
|
||||
|
||||
Reference in New Issue
Block a user