1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Events\GuitarWasCreated; |
6
|
|
|
use App\Events\GuitarWasDeleted; |
7
|
|
|
use App\Events\GuitarWasUpdated; |
8
|
|
|
use App\Events\SaleWasCreated; |
9
|
|
|
use App\Guitar; |
10
|
|
|
use App\Purchase; |
11
|
|
|
use App\Rack; |
12
|
|
|
use App\Sale; |
13
|
|
|
use App\Shop; |
14
|
|
|
use Illuminate\Http\Request; |
15
|
|
|
use Illuminate\Http\Response; |
16
|
|
|
|
17
|
|
|
class GuitarController extends Controller |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* GuitarController constructor. |
21
|
|
|
*/ |
22
|
|
|
public function __construct() |
23
|
|
|
{ |
24
|
|
|
$this->middleware('auth'); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* List the guitars. |
29
|
|
|
* @return Response |
30
|
|
|
*/ |
31
|
|
|
public function index() |
32
|
|
|
{ |
33
|
|
|
return view('guitars.index'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Save the guitar. |
38
|
|
|
* |
39
|
|
|
* @param $id |
40
|
|
|
* @param Request $request |
41
|
|
|
* |
42
|
|
|
* @return Response |
43
|
|
|
*/ |
44
|
|
|
public function store($id, Request $request) |
45
|
|
|
{ |
46
|
|
|
$purchase = Purchase::findOrFail($id); |
47
|
|
|
if ( ! $purchase->hasArrived() || ! $purchase->isPendingStorage()) { |
48
|
|
|
return response(['purchase' => ['The purchase has not yet arrived or doesn\'t require addition of guitars.']], |
|
|
|
|
49
|
|
|
422); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$this->validate($request, [ |
53
|
|
|
'rack_id' => 'required|exists:racks,id,make_id,' . $purchase->make_id, |
54
|
|
|
'model_id' => 'required|exists:models,id,make_id,' . $purchase->make_id, |
55
|
|
|
'colour' => 'required|string|max:255', |
56
|
|
|
'damaged' => 'boolean', |
57
|
|
|
'condition' => 'required_if:damaged,true', |
58
|
|
|
'price' => 'required|numeric|min:1', |
59
|
|
|
]); |
60
|
|
|
|
61
|
|
|
$rack = Rack::find($request->input('rack_id')); |
62
|
|
View Code Duplication |
if ($rack->used >= $rack->capacity) { |
|
|
|
|
63
|
|
|
return response(['rack_id' => ['The selected rack is full.']], 422); |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$guitar = Guitar::create($request->only(['rack_id', 'model_id', 'colour', 'price']) + [ |
67
|
|
|
'make_id' => $purchase->make_id, |
68
|
|
|
'purchase_id' => $purchase->id, |
69
|
|
|
'damaged' => $request->input('damaged'), |
70
|
|
|
'condition' => $request->input('damaged') ? $request->input('condition') : null |
71
|
|
|
]); |
72
|
|
|
|
73
|
|
|
event(new GuitarWasCreated($guitar)); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Update the guitar. |
78
|
|
|
* |
79
|
|
|
* @param $id |
80
|
|
|
* @param Request $request |
81
|
|
|
* |
82
|
|
|
* @return Response |
83
|
|
|
*/ |
84
|
|
|
public function update($id, Request $request) |
85
|
|
|
{ |
86
|
|
|
$guitar = Guitar::findOrFail($id); |
87
|
|
|
$this->validate($request, [ |
88
|
|
|
'rack_id' => 'required|exists:racks,id,make_id,' . $guitar->make_id, |
89
|
|
|
'model_id' => 'required|exists:models,id,make_id,' . $guitar->make_id, |
90
|
|
|
'colour' => 'required|string|max:255', |
91
|
|
|
'damaged' => 'boolean', |
92
|
|
|
'condition' => 'required_if:damaged,true', |
93
|
|
|
'price' => 'required|numeric|min:1', |
94
|
|
|
]); |
95
|
|
|
|
96
|
|
|
$rack = Rack::find($request->input('rack_id')); |
97
|
|
View Code Duplication |
if ($rack->used >= $rack->capacity) { |
|
|
|
|
98
|
|
|
return response(['rack_id' => ['The selected rack is full.']], 422); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$guitar->update($request->only(['rack_id', 'model_id', 'colour', 'price']) + [ |
102
|
|
|
'damaged' => $request->input('damaged'), |
103
|
|
|
'condition' => $request->input('damaged') ? $request->input('condition') : null |
104
|
|
|
]); |
105
|
|
|
event(new GuitarWasUpdated($guitar)); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Sell the guitar. |
110
|
|
|
* |
111
|
|
|
* @param $id |
112
|
|
|
* @param Request $request |
113
|
|
|
* |
114
|
|
|
* @return Response |
115
|
|
|
*/ |
116
|
|
|
public function sale($id, Request $request) |
117
|
|
|
{ |
118
|
|
|
$guitar = Guitar::findOrFail($id); |
119
|
|
|
|
120
|
|
|
if ($guitar->isSold()) { |
121
|
|
|
return response(['guitar_id' => ['This guitar has already been sold.']], 422); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$this->validate($request, [ |
125
|
|
|
'customer_type' => 'required|in:individual,shop', |
126
|
|
|
'name' => 'required|string|max:255', |
127
|
|
|
'address' => 'required|string|max:255', |
128
|
|
|
]); |
129
|
|
|
|
130
|
|
|
$shop = null; |
131
|
|
|
if ($request->input('customer_type') == 'shop') { |
132
|
|
|
$shop = Shop::create($request->only(['name', 'address']))->id; |
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$sale = Sale::create($request->only(['name', 'address']) + ['guitar_id' => $id, 'shop_id' => $shop]); |
136
|
|
|
|
137
|
|
|
event(new SaleWasCreated($sale)); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Delete the guitar. |
142
|
|
|
* |
143
|
|
|
* @param $id |
144
|
|
|
*/ |
145
|
|
|
public function destroy($id) |
146
|
|
|
{ |
147
|
|
|
$guitar = Guitar::findOrFail($id); |
148
|
|
|
event(new GuitarWasDeleted($guitar)); |
149
|
|
|
$guitar->delete(); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: