public function signin(Request $request) { $request->request->add(array_merge($request->json()->all(), ['user_type' => 'user'])); $token = Authorizer::issueAccessToken(); $guest_token = $request->header('authorization'); Bag::where('access_token', '=', $guest_token)->update(['access_token' => $token['access_token']]); return $token; }
public function index(Request $request) { $data = $request->json()->all('bags'); $payment = Payment::findOrFail($data['payment_id']); $logistics = Logistics::findOrFail($data['logistics_id']); $bags = Bag::with('stock.product')->find($data['bags']); $order = new Order(array_except($data, 'bags')); \DB::transaction(function () use($bags, $logistics, $payment, $order, $data, $request) { $order_products = []; foreach ($bags as $bag) { $order_products[] = $bag->stock->pick($bag->quantity); $bag->stock->save(); } $order->user_id = $request->user->id; $order->calc($order_products); $order->save(); $order->products()->saveMany($order_products); Bag::destroy($data['bags']); }); return response()->created($order); }
public function store(Request $request) { $data = array_only($request->json()->all(), ['product_id', 'sku', 'quantity']); $stock = ProductStock::whereSku($data['sku'])->firstOrFail(); if ($stock->stocks < $data['quantity']) { throw new \Exception('stock not enough'); } $data['access_token'] = $request->headers->get('authorization'); $data['user_id'] = $request->user->id; $bag = Bag::firstOrNew(array_except($data, ['user_id', 'quantity'])); if ($bag->exists) { if ($stock->stocks < $data['quantity']) { throw new \Exception('stock not enough'); } $bag->quantity += $data['quantity']; } else { $bag->quantity = $data['quantity']; } $bag->user_id = $data['user_id']; $bag->save(); return response()->created($bag); }