laravel-api / app / Http / Controllers / ItemController.php
ItemController.php
Raw
<?php

namespace App\Http\Controllers;

use App\Models\Item;
use Illuminate\Support\Facades\Storage;
use App\Models\Category;
use Illuminate\Support\Str;
use Illuminate\Http\Request;

class ItemController extends Controller
{
    /**
     * Display a listing of the items.
     *
     * 
     */
    public function showAllItems()
    {
        return item::all();
    }

    /**
     * Store a newly created item in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function addItem(Request $request, $id)
    {
        $category = Category::find($id);
        
        if($category == null){
            return response()->json(['category not found'], 404);
        }

        $item = new Item();

        $request->validate([
            'name'=> 'required',
            'price'=> 'required',
            'description'=> 'required',
            'image' => 'required|file|image|max:2048',
            'amount'=> 'required',
        ]);
        if ($request->hasFile('image')){

            // use it to make the item able to store image
            $storage = Storage::disk('public');
            // Give the image a name 
            $imageName= Str::random(32).".".$request->image->getClientOriginalExtension();
            //save it in public folder
            $storage->put($imageName, file_get_contents($request->image));
        }

        $item = Item::create([
            'name'=> $request->name,
            'description'=> $request->description,
            'price'=> $request->price,
            'image'=> $imageName,
            'amount'=> $request->amount,
            'category_id'=> $id,
        ]);

        $category->items()->save($item);
        return response()->json(['item added'], 200);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function getItemById($id)
    {
        $item = Item::find($id);

        if($item != null){
            return $item;
        } else {
            return response()->json(['item not found'], 404);
        }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    // function can update item now but the tested request is Post and(_method : put) must be sent with the request
    public function updateItem(Request $request, $id)
    {
        $item = Item::find($id);

        if(!$item) {
            return response()->json(['Item Not Found'], 404);
        } 

        $item->name= $request->name;
        $item->price= $request->price;
        $item->description= $request->description;
        $item->amount= $request->amount;

        if($request->image) {
            //Public Storage
            $storage = Storage::disk('public');

            // Delete old image
            if($storage->exists($request->image)){
                $storage->delete($request->image);
            }
            // Give image a name
            $imageName= Str::random(32).".".$request->image->getClientOriginalExtension();
            $item->image= $imageName;

            //save in public folder
            $storage->put($imageName, file_get_contents($request->image));
        }
        $item->save();
        return response()->json(['Item Updated'], 200);

    }

    /**
     * Remove the specified item from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function deleteItem($id)
    {
        $item = Item::find($id);

        if($item != null){
            $item->delete();
            return response()->json("item deleted");

        } else {
            return response()->json(['item not found'], 404);
        }
    }

    // Increase amount by 1 
    public function increaseAmount(Request $request, $id)
    {
        $item = Item::find($id);
        if(!$item) {
            return response()->json(['Item Not Found'], 404);
        } 
        $item->amount++;
        $item->save();
        return $item;
    }

    // Decrease amount by 1 
    public function decreaseAmount(Request $request, $id)
    {
        $item = Item::find($id);
        if(!$item) {
            return response()->json(['Item Not Found'], 404);
        } 
        if($item->amount > 0){
            $item->amount--;
            $item->save();
            return $item;
        }
        else{
            return response()->json(['cant be less than zero']);
        }
        
}



}