1
|
|
|
<?php namespace App\Http\Controllers; |
2
|
|
|
|
3
|
|
|
use App\Exceptions\RepositoryException; |
4
|
|
|
use App\Repositories\ProductRepository; |
5
|
|
|
use App\Repositories\CategoryRepository; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use Input; |
8
|
|
|
use Response; |
9
|
|
|
|
10
|
|
|
class ProductController extends Controller |
11
|
|
|
{ |
12
|
|
|
protected $repository; |
13
|
|
|
|
14
|
|
|
public function __construct(ProductRepository $repository) |
15
|
|
|
{ |
16
|
|
|
$this->repository = $repository; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Get all products in REST format |
21
|
|
|
* |
22
|
|
|
* @link GET /app/stock/product/ |
23
|
|
|
*/ |
24
|
|
|
public function index() |
25
|
|
|
{ |
26
|
|
|
$products = $this->repository->all(); |
27
|
|
|
return response()->json($this->repository->APIFormat($products), 200); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Store a new product in the DB |
32
|
|
|
* |
33
|
|
|
* @link POST /app/stock/product/ |
34
|
|
|
*/ |
35
|
|
|
public function store(Request $request, CategoryRepository $categoryRepository) |
36
|
|
|
{ |
37
|
|
|
$categoryRepository->get($request->input('category')); |
38
|
|
|
$product = $this->repository->store($request->all()); |
39
|
|
|
|
40
|
|
|
return response()->json(['id' => $product->id], 200); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get a product in REST format |
45
|
|
|
* |
46
|
|
|
* @link GET /app/stock/product/{id} |
47
|
|
|
*/ |
48
|
|
|
public function show($id) |
49
|
|
|
{ |
50
|
|
|
$product = $this->repository->get($id); |
51
|
|
|
|
52
|
|
|
if (is_null($product)) { |
53
|
|
|
return response()->json(['code' => RepositoryException::RESOURCE_NOT_FOUND, |
54
|
|
|
'message' => 'Product with ID ' . $id . ' not found'], 404); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return response()->json($this->repository->APIFormat($product), 200); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Update the specified product in the DB |
62
|
|
|
* |
63
|
|
|
* @link PUT /app/stock/product/{id} |
64
|
|
|
*/ |
65
|
|
|
public function update(Request $request, $id) |
66
|
|
|
{ |
67
|
|
|
$product = $this->repository->update($id, $request->all()); |
68
|
|
|
return response()->json($this->repository->APIFormat($product), 200); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Remove the specified product from storage. |
73
|
|
|
* |
74
|
|
|
* @link DELETE /app/stock/product/{id} |
75
|
|
|
*/ |
76
|
|
|
public function destroy($id) |
77
|
|
|
{ |
78
|
|
|
$product = $this->repository->softDelete($id); |
79
|
|
|
return response()->json($this->repository->APIFormat($product), 200); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
} |
83
|
|
|
|