1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Http/Controllers/ProductController.php

162 lines
3.9 KiB
PHP
Raw Normal View History

2019-04-03 02:09:22 +02:00
<?php
2019-05-11 05:32:07 +02:00
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
2019-04-03 02:09:22 +02:00
namespace App\Http\Controllers;
2019-04-03 04:34:28 +02:00
use App\Factory\ProductFactory;
2019-04-03 02:09:22 +02:00
use App\Filters\ProductFilters;
2019-04-03 04:34:28 +02:00
use App\Http\Requests\Product\CreateProductRequest;
2019-04-03 03:17:21 +02:00
use App\Http\Requests\Product\EditProductRequest;
2019-04-03 04:34:28 +02:00
use App\Http\Requests\Product\ShowProductRequest;
use App\Http\Requests\Product\StoreProductRequest;
2019-04-03 05:22:13 +02:00
use App\Http\Requests\Product\UpdateProductRequest;
2019-04-03 11:54:27 +02:00
use App\Jobs\Entity\ActionEntity;
2019-04-03 02:09:22 +02:00
use App\Models\Product;
2019-04-03 04:34:28 +02:00
use App\Repositories\ProductRepository;
2019-04-03 02:09:22 +02:00
use App\Transformers\ProductTransformer;
use App\Utils\Traits\MakesHash;
use Illuminate\Http\Request;
class ProductController extends BaseController
{
use MakesHash;
2019-04-03 04:34:28 +02:00
protected $entity_type = Product::class;
protected $entity_transformer = ProductTransformer::class;
2019-04-03 02:09:22 +02:00
2019-04-03 04:34:28 +02:00
protected $product_repo;
2019-04-03 02:09:22 +02:00
2019-04-03 03:17:21 +02:00
/**
* ProductController constructor.
*/
2019-04-03 04:34:28 +02:00
public function __construct(ProductRepository $product_repo)
2019-04-03 03:17:21 +02:00
{
parent::__construct();
2019-04-03 04:34:28 +02:00
$this->product_repo = $product_repo;
2019-04-03 03:17:21 +02:00
}
2019-04-03 02:09:22 +02:00
/**
*/
public function index(ProductFilters $filters)
{
$products = Product::filter($filters);
return $this->listResponse($products);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
2019-04-03 04:34:28 +02:00
public function create(CreateProductRequest $request)
2019-04-03 02:09:22 +02:00
{
2019-04-03 04:34:28 +02:00
$product = ProductFactory::create(auth()->user()->company()->id, auth()->user()->id);
return $this->itemResponse($product);
2019-04-03 02:09:22 +02:00
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
2019-04-03 04:34:28 +02:00
public function store(StoreProductRequest $request)
2019-04-03 02:09:22 +02:00
{
2019-04-03 04:34:28 +02:00
$product = $this->product_repo->save($request, ProductFactory::create(auth()->user()->company()->id, auth()->user()->id));
return $this->itemResponse($product);
2019-04-03 02:09:22 +02:00
}
/**
* Display the specified resource.
*
2019-04-03 11:54:27 +02:00
* @param Product $product
2019-04-03 02:09:22 +02:00
* @return \Illuminate\Http\Response
*/
2019-04-03 03:17:21 +02:00
public function show(ShowProductRequest $request, Product $product)
2019-04-03 02:09:22 +02:00
{
2019-04-03 03:17:21 +02:00
return $this->itemResponse($product);
2019-04-03 02:09:22 +02:00
}
/**
* Show the form for editing the specified resource.
*
2019-04-03 11:54:27 +02:00
* @param Product $product
2019-04-03 02:09:22 +02:00
* @return \Illuminate\Http\Response
*/
2019-04-03 03:17:21 +02:00
public function edit(EditProductRequest $request, Product $product)
2019-04-03 02:09:22 +02:00
{
2019-04-03 03:17:21 +02:00
return $this->itemResponse($product);
2019-04-03 02:09:22 +02:00
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
2019-04-03 11:54:27 +02:00
* @param Product $product
2019-04-03 02:09:22 +02:00
* @return \Illuminate\Http\Response
*/
2019-04-03 05:22:13 +02:00
public function update(UpdateProductRequest $request, Product $product)
2019-04-03 02:09:22 +02:00
{
2019-04-03 05:22:13 +02:00
$product = $this->product_repo->save($request, $product);
return $this->itemResponse($product);
2019-04-03 02:09:22 +02:00
}
/**
* Remove the specified resource from storage.
*
2019-04-03 11:54:27 +02:00
* @param Product $product
2019-04-03 02:09:22 +02:00
* @return \Illuminate\Http\Response
*/
2019-04-03 11:54:27 +02:00
public function destroy(Product $product)
2019-04-03 02:09:22 +02:00
{
2019-04-03 11:54:27 +02:00
$product->delete();
return response()->json([], 200);
}
/**
* Perform bulk actions on the list view
*
* @return Collection
*/
public function bulk()
{
$action = request()->input('action');
$ids = request()->input('ids');
$products = Product::withTrashed()->find($ids);
$products->each(function ($product, $key) use($action){
if(auth()->user()->can('edit', $product))
ActionEntity::dispatchNow($product, $action);
});
//todo need to return the updated dataset
return response()->json([], 200);
2019-04-03 02:09:22 +02:00
}
}