<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Product;

class Product_Controller extends Controller
{
   
    public function index()
    {
        //selcet all
        $products = product :: all();
        return response()->json($products);
    }

   
  
    public function store(Request $request)
    {
        $this->validate($request,[

                'title' => 'required',
                'amount' => 'required',                
                'description' => 'required'

        ]);

        $product = new Product();

        $product->title = $request->input('title');
        $product->amount = $request->input('amount');
        $product->photo = $request->input('photo');
        $product->description = $request->input('description');

        $product->save();
        return response()->json($product);
    }

   
    public function show($id)
    {
        $product = product :: find($id);
        return response()->json($product);
        
    }

  
  
   
    public function update(Request $request, $id)
    {
        $this->validate($request,[

                'title' => 'required',
                'amount' => 'required',  
                'photo'   => 'photo',           
                'description' => 'required'

        ]);

        $product = product :: find($id);

        $product->title = $request->input('title');
        $product->amount = $request->input('amount');
        $product->photo = $request->input('photo');
        $product->description = $request->input('description');

        $product->save();
        return response()->json($product);
    }

    
    public function destroy($id)
    {
         $product = product :: find($id);
         $product->delete();
         return response()->json("product deleted successfully");
    }
}
