File tree Expand file tree Collapse file tree 1 file changed +22
-3
lines changed Expand file tree Collapse file tree 1 file changed +22
-3
lines changed Original file line number Diff line number Diff line change 11
11
use App \Models \Api \Product ;
12
12
use App \Models \Order ;
13
13
use Illuminate \Http \Request ;
14
+ use Illuminate \Support \Facades \DB ;
14
15
use Illuminate \Support \Facades \Mail ;
15
16
16
17
class OrderController extends Controller
@@ -40,6 +41,7 @@ public function index()
40
41
public function view (Order $ order )
41
42
{
42
43
$ order ->load ('items.product ' );
44
+
43
45
return new OrderResource ($ order );
44
46
}
45
47
@@ -50,10 +52,27 @@ public function getStatuses()
50
52
51
53
public function changeStatus (Order $ order , $ status )
52
54
{
53
- $ order ->status = $ status ;
54
- $ order ->save ();
55
+ DB ::beginTransaction ();
56
+ try {
57
+ $ order ->status = $ status ;
58
+ $ order ->save ();
59
+
60
+ if ($ status === OrderStatus::Cancelled->value ) {
61
+ foreach ($ order ->items as $ item ) {
62
+ $ product = $ item ->product ;
63
+ if ($ product && $ product ->quantity !== null ) {
64
+ $ product ->quantity += $ item ->quantity ;
65
+ $ product ->save ();
66
+ }
67
+ }
68
+ }
69
+ Mail::to ($ order ->user )->send (new OrderUpdateEmail ($ order ));
70
+ } catch (\Exception $ e ) {
71
+ DB ::rollBack ();
72
+ throw $ e ;
73
+ }
55
74
56
- Mail:: to ( $ order -> user )-> send ( new OrderUpdateEmail ( $ order ) );
75
+ DB :: commit ( );
57
76
58
77
return response ('' , 200 );
59
78
}
You can’t perform that action at this time.
0 commit comments