feat: add sub category for media
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\SubCategory;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class SubCategoryController extends Controller
|
||||
{
|
||||
public function index(Request $request, $categoryId = null)
|
||||
{
|
||||
$query = SubCategory::with('category')->withCount('media');
|
||||
|
||||
$categoryId = $categoryId ?? $request->input('category_id');
|
||||
|
||||
if ($categoryId) {
|
||||
$query->where('category_id', $categoryId);
|
||||
}
|
||||
|
||||
return response()->json(
|
||||
$query->orderBy('name')->get()
|
||||
);
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'category_id' => 'required|exists:categories,id',
|
||||
'name' => 'required|string|max:255',
|
||||
]);
|
||||
|
||||
$existing = SubCategory::where('category_id', $data['category_id'])
|
||||
->where('name', $data['name'])
|
||||
->first();
|
||||
|
||||
if ($existing) {
|
||||
return response()->json([
|
||||
'message' => 'A subcategory with this name already exists in this category',
|
||||
'errors' => ['name' => ['The subcategory name must be unique within this category.']],
|
||||
], 422);
|
||||
}
|
||||
|
||||
$subCategory = SubCategory::create($data);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Subcategory created successfully',
|
||||
'sub_category' => $subCategory->load('category'),
|
||||
], 201);
|
||||
}
|
||||
|
||||
public function show($id)
|
||||
{
|
||||
$subCategory = SubCategory::with('category')->withCount('media')->findOrFail($id);
|
||||
|
||||
return response()->json($subCategory);
|
||||
}
|
||||
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
$subCategory = SubCategory::findOrFail($id);
|
||||
|
||||
$data = $request->validate([
|
||||
'category_id' => 'sometimes|exists:categories,id',
|
||||
'name' => 'sometimes|string|max:255',
|
||||
]);
|
||||
|
||||
$categoryId = $data['category_id'] ?? $subCategory->category_id;
|
||||
$name = $data['name'] ?? $subCategory->name;
|
||||
|
||||
$existing = SubCategory::where('category_id', $categoryId)
|
||||
->where('name', $name)
|
||||
->where('id', '!=', $subCategory->id)
|
||||
->first();
|
||||
|
||||
if ($existing) {
|
||||
return response()->json([
|
||||
'message' => 'A subcategory with this name already exists in this category',
|
||||
'errors' => ['name' => ['The subcategory name must be unique within this category.']],
|
||||
], 422);
|
||||
}
|
||||
|
||||
$subCategory->update($data);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Subcategory updated successfully',
|
||||
'sub_category' => $subCategory->load('category'),
|
||||
]);
|
||||
}
|
||||
|
||||
public function destroy($id)
|
||||
{
|
||||
$subCategory = SubCategory::findOrFail($id);
|
||||
$subCategory->delete();
|
||||
|
||||
return response()->json(['message' => 'Subcategory deleted successfully']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user