1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 16:31:33 +02:00
invoiceninja/app/Http/Controllers/ProductApiController.php

196 lines
4.8 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
namespace App\Http\Controllers;
2016-01-30 10:56:27 +01:00
use App\Http\Requests\ProductRequest;
2016-05-02 15:12:37 +02:00
use App\Http\Requests\CreateProductRequest;
use App\Http\Requests\UpdateProductRequest;
2017-01-30 20:40:43 +01:00
use App\Models\Product;
use App\Ninja\Repositories\ProductRepository;
2016-01-30 10:56:27 +01:00
/**
2017-01-30 20:40:43 +01:00
* Class ProductApiController.
*/
2016-01-30 10:56:27 +01:00
class ProductApiController extends BaseAPIController
{
/**
* @var string
*/
2016-05-01 22:55:13 +02:00
protected $entityType = ENTITY_PRODUCT;
2016-02-01 03:32:12 +01:00
/**
* @var ProductRepository
*/
protected $productRepo;
/**
* ProductApiController constructor.
*
* @param ProductRepository $productRepo
*/
2016-05-02 15:12:37 +02:00
public function __construct(ProductRepository $productRepo)
2016-01-30 10:56:27 +01:00
{
2016-03-02 15:36:46 +01:00
parent::__construct();
2016-01-30 10:56:27 +01:00
2016-02-01 03:32:12 +01:00
$this->productRepo = $productRepo;
2016-01-30 10:56:27 +01:00
}
/**
2016-12-29 17:17:17 +01:00
* @SWG\Get(
* path="/products",
* summary="List products",
* operationId="listProducts",
2016-12-29 17:17:17 +01:00
* tags={"product"},
* @SWG\Response(
* response=200,
* description="A list of products",
2016-12-29 17:17:17 +01:00
* @SWG\Schema(type="array", @SWG\Items(ref="#/definitions/Product"))
* ),
* @SWG\Response(
* response="default",
* description="an ""unexpected"" error"
* )
* )
*/
2016-01-30 10:56:27 +01:00
public function index()
{
2016-05-01 22:55:13 +02:00
$products = Product::scope()
->withTrashed()
->orderBy('created_at', 'desc');
2016-01-30 11:51:52 +01:00
2016-05-02 10:38:01 +02:00
return $this->listResponse($products);
2016-01-30 10:56:27 +01:00
}
/**
* @SWG\Get(
* path="/products/{product_id}",
* summary="Retrieve a product",
* operationId="getProduct",
* tags={"product"},
* @SWG\Parameter(
* in="path",
* name="product_id",
* type="integer",
* required=true
* ),
* @SWG\Response(
* response=200,
* description="A single product",
* @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Product"))
* ),
* @SWG\Response(
* response="default",
* description="an ""unexpected"" error"
* )
* )
*/
public function show(ProductRequest $request)
{
return $this->itemResponse($request->entity());
}
/**
2016-12-29 17:17:17 +01:00
* @SWG\Post(
* path="/products",
* summary="Create a product",
* operationId="createProduct",
* tags={"product"},
2016-12-29 17:17:17 +01:00
* @SWG\Parameter(
* in="body",
* name="body",
* @SWG\Schema(ref="#/definitions/Product")
* ),
* @SWG\Response(
* response=200,
* description="New product",
* @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Product"))
* ),
* @SWG\Response(
* response="default",
* description="an ""unexpected"" error"
* )
* )
*/
2016-05-02 15:12:37 +02:00
public function store(CreateProductRequest $request)
2016-01-30 10:56:27 +01:00
{
2016-05-02 15:12:37 +02:00
$product = $this->productRepo->save($request->input());
2016-01-30 10:56:27 +01:00
2016-05-02 15:12:37 +02:00
return $this->itemResponse($product);
2016-01-30 10:56:27 +01:00
}
/**
2016-12-29 17:17:17 +01:00
* @SWG\Put(
* path="/products/{product_id}",
* summary="Update a product",
* operationId="updateProduct",
* tags={"product"},
2016-12-29 17:17:17 +01:00
* @SWG\Parameter(
* in="path",
* name="product_id",
* type="integer",
* required=true
* ),
* @SWG\Parameter(
2016-12-29 17:17:17 +01:00
* in="body",
* name="product",
2016-12-29 17:17:17 +01:00
* @SWG\Schema(ref="#/definitions/Product")
* ),
* @SWG\Response(
* response=200,
* description="Updated product",
2016-12-29 17:17:17 +01:00
* @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Product"))
* ),
* @SWG\Response(
* response="default",
* description="an ""unexpected"" error"
* )
* )
2017-01-30 20:54:09 +01:00
*
2017-01-30 20:49:42 +01:00
* @param mixed $publicId
*/
2016-05-02 15:12:37 +02:00
public function update(UpdateProductRequest $request, $publicId)
2016-01-30 10:56:27 +01:00
{
2016-05-02 15:12:37 +02:00
if ($request->action) {
return $this->handleAction($request);
2016-02-01 03:32:12 +01:00
}
2016-12-29 17:17:17 +01:00
2016-05-02 15:12:37 +02:00
$data = $request->input();
$data['public_id'] = $publicId;
$product = $this->productRepo->save($data, $request->entity());
2016-05-02 15:12:37 +02:00
return $this->itemResponse($product);
2016-01-30 10:56:27 +01:00
}
/**
* @SWG\Delete(
* path="/products/{product_id}",
* summary="Delete a product",
* operationId="deleteProduct",
* tags={"product"},
* @SWG\Parameter(
* in="path",
* name="product_id",
* type="integer",
* required=true
* ),
* @SWG\Response(
* response=200,
* description="Deleted product",
* @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Product"))
* ),
* @SWG\Response(
* response="default",
* description="an ""unexpected"" error"
* )
* )
*/
public function destroy(UpdateProductRequest $request)
{
$product = $request->entity();
$this->productRepo->delete($product);
return $this->itemResponse($product);
}
2016-01-30 10:56:27 +01:00
}