<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Categorie;

class CategorieController extends Controller
{
    public function listCategorie()
    {
        $categories = Categorie::all();
        return response()->json($categories);
    }

    public function insCategorie(Request $request)
    {
        $category = new Categorie();
        $category->cat_name = $request->cat_name;
        $category->save();

        return response()->json(['message' => 'Categoria inserita con successo'], 201);
    }

    public function updateCategorie(Request $request, $id)
    {
        $category = Categorie::findOrFail($id);
        $category->cat_name = $request->cat_name;
        $category->save();

        return response()->json(['message' => 'Categoria aggiornata con successo']);
    }

    public function deleteCategorie($id)
    {
        $category = Categorie::findOrFail($id);
        $category->delete();

        return response()->json(['message' => 'Categoria eliminata con successo']);
    }
}
