70 lines
2.0 KiB
PHP
70 lines
2.0 KiB
PHP
<?php namespace App\Http\Controllers\App;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\App\SubcategoryStoreRequest;
|
|
use App\Http\Requests\App\SubcategoryUpdateRequest;
|
|
use App\Models\Category;
|
|
use App\Models\Subcategory;
|
|
use Notsoweb\ApiResponse\Enums\ApiResponse;
|
|
|
|
class SubcategoryController extends Controller
|
|
{
|
|
public function index(Category $category)
|
|
{
|
|
$subcategorias = $category->subcategories()
|
|
->orderBy('name')
|
|
->paginate(config('app.pagination'));
|
|
|
|
return ApiResponse::OK->response([
|
|
'subcategories' => $subcategorias,
|
|
]);
|
|
}
|
|
|
|
public function show(Category $category, Subcategory $subcategory)
|
|
{
|
|
return ApiResponse::OK->response([
|
|
'model' => $subcategory,
|
|
]);
|
|
}
|
|
|
|
public function store(SubcategoryStoreRequest $request, Category $category)
|
|
{
|
|
if($request->isBulk()){
|
|
$subcategorias = collect($request->validated())
|
|
->map(fn($data) => $category->subcategories()->create($data));
|
|
|
|
return ApiResponse::OK->response([
|
|
'models' => $subcategorias,
|
|
]);
|
|
}
|
|
|
|
$subcategoria = $category->subcategories()->create($request->validated());
|
|
|
|
return ApiResponse::OK->response([
|
|
'model' => $subcategoria,
|
|
]);
|
|
}
|
|
|
|
public function update(SubcategoryUpdateRequest $request, Category $category, Subcategory $subcategory)
|
|
{
|
|
$subcategory->update($request->validated());
|
|
|
|
return ApiResponse::OK->response([
|
|
'model' => $subcategory->fresh(),
|
|
]);
|
|
}
|
|
|
|
public function destroy(Category $category, Subcategory $subcategory)
|
|
{
|
|
if ($subcategory->inventories()->exists()) {
|
|
return ApiResponse::BAD_REQUEST->response([
|
|
'message' => 'No se puede eliminar la subclasificación porque tiene productos asociados.'
|
|
]);
|
|
}
|
|
|
|
$subcategory->delete();
|
|
|
|
return ApiResponse::OK->response();
|
|
}
|
|
}
|