This commit is contained in:
dimon8
2026-01-07 19:29:49 +00:00
parent 31b79d70c5
commit d8a8759504
14 changed files with 363 additions and 78 deletions

View File

@@ -9,9 +9,15 @@ use Illuminate\Http\Response;
class ComponentsController extends Controller
{
public function index(){
return response()->json(Component::all()->toJson());
}
public function index()
{
$components = Component::with('user', 'componentType')
->where('is_official', true)
->orWhere('created_by_user_id', auth()->id())
->get();
return response()->json($components);
}
@@ -26,49 +32,73 @@ class ComponentsController extends Controller
return response()->json($component);
}
public function create(Request $request)
{
$name = $request->get(key:'name');
$type = $request->get(key:'type');
$brand = $request->get(key:'brand');
$model = $request->get(key:'model');
$price = $request->get(key:'price');
public function store(Request $request)
{
$validated = $request->validate([
'name' => 'required|string|max:255',
'price' => 'required|numeric|min:0',
'component_type_id' => 'required|exists:component_types,id',
'specifications' => 'nullable|array',
]);
$component = new Component();
$component->name = $name;
$component->type = $type;
$component->brand = $brand;
$component->model = $model;
$component->price = $price;
$component->save();
$component = Component::create([
'name' => $validated['name'],
'price' => $validated['price'],
'component_type_id' => $validated['component_type_id'],
'specifications' => $validated['specifications'] ?? null,
'is_official' => false, // всегда false для пользователя
'created_by_user_id' => auth()->id(), // автоматически привязываем к пользователю
]);
return response()->json([
'message' => 'Компонент успешно создан.',
'component' => $component
], 201);
}
public function update(Request $request, $id)
{
$component = Component::findOrFail($id);
return response()->json($component->toJson());
// Проверяем, что компонент принадлежит пользователю и не официальный
if ($component->created_by_user_id !== auth()->id() || $component->is_official) {
return response()->json([
'message' => 'Вы не можете редактировать этот компонент.'
], 403);
}
public function update(Request $request, int $id): JsonResponse{
$validated = $request->validate([
'name' => 'required|string|max:255',
'price' => 'required|numeric|min:0',
'component_type_id' => 'required|exists:component_types,id',
'specifications' => 'nullable|array',
]);
return response()->json([
'name' => $request->get('name'),
'type' => $request->get('type'),
'brand' => $request->get('brand'),
'model' => $request->get('model'),
'price' => $request->get('price'),
], Response::HTTP_ACCEPTED);
}
$component->update($validated);
public function destroy(int $id): JsonResponse
{
// мы бы здесь написали вызов запроса delete из БД
return response()->json([
'message' => 'Компонент обновлён.',
'component' => $component
]);
}
public function destroy($id)
{
$component = Component::findOrFail($id);
// Проверяем, что компонент принадлежит пользователю и не официальный
if ($component->created_by_user_id !== auth()->id() || $component->is_official) {
return response()->json([
'success' => true,
], Response::HTTP_ACCEPTED);
'message' => 'Вы не можете удалить этот компонент.'
], 403);
}
$component->delete();
return response()->json([
'message' => 'Компонент удалён.'
]);
}