80 lines
2.1 KiB
PHP
80 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\V1;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Material;
|
|
use Illuminate\Http\Request;
|
|
|
|
class MaterialController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
if ($request->has('category_id')) {
|
|
return Material::where('category_id', $request->category_id)->get();
|
|
}
|
|
return Material::all();
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$validated = $request->validate([
|
|
'category_id' => 'required|exists:categories,id',
|
|
'title' => 'required|string|max:255',
|
|
'author' => 'required|string|max:255',
|
|
'description' => 'nullable|string',
|
|
'cover_image_path' => 'nullable|string',
|
|
'file_path' => 'required|string',
|
|
'tags' => 'nullable|array',
|
|
]);
|
|
|
|
$material = Material::create($validated);
|
|
|
|
return response()->json($material, 201);
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show(Material $material)
|
|
{
|
|
return $material;
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(Request $request, Material $material)
|
|
{
|
|
$validated = $request->validate([
|
|
'category_id' => 'sometimes|required|exists:categories,id',
|
|
'title' => 'sometimes|required|string|max:255',
|
|
'author' => 'sometimes|required|string|max:255',
|
|
'description' => 'nullable|string',
|
|
'cover_image_path' => 'nullable|string',
|
|
'file_path' => 'sometimes|required|string',
|
|
'tags' => 'nullable|array',
|
|
]);
|
|
|
|
$material->update($validated);
|
|
|
|
return response()->json($material);
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(Material $material)
|
|
{
|
|
$material->delete();
|
|
|
|
return response()->json(null, 204);
|
|
}
|
|
}
|