/** * @SWG\Api( * path="/order/add", * @SWG\Operation( * nickname="Add new order", * method="POST", * summary="Add new order", * notes="Returns order", * type="Order", * authorizations={}, * @SWG\Parameter( * name="Body", * description="Body", * required=true, * type="OrdersBody", * paramType="body", * allowMultiple=false * ), * @SWG\ResponseMessage(code=500, message="Internal server error") * ) * ) */ public function add() { $statusCode = 200; $params = \Input::all(); if ($params) { $errors = []; $mainValidator = Validator::make($params, ['customer_id' => 'required|numeric|exists:customers,id', 'total' => 'required|numeric|min:1', 'items' => 'required|array']); if ($mainValidator->fails()) { $response = ['error' => ['main' => $mainValidator->errors()]]; $statusCode = 500; } else { $itemsValidator = []; $itemsAddonsValidator = []; foreach ($params['items'] as $itemKey => $item) { $itemsValidator[$itemKey] = Validator::make($item, ['photo_id' => 'required|numeric|exists:photos,id', 'qty' => 'required|numeric|min:1', 'price_per_item' => 'required|numeric', 'format_id' => 'required|numeric|exists:formats,id', 'addons' => 'required|array']); foreach ($item['addons'] as $addon) { $itemsAddonsValidator[$itemKey][] = Validator::make($addon, ['id' => 'required|numeric|exists:addons,id', 'qty' => 'required|numeric|min:1']); } } foreach ($itemsValidator as $itemKey => $validator) { if ($validator->fails()) { $errors['items'][$itemKey + 1] = $validator->errors(); } else { foreach ($itemsAddonsValidator[$itemKey] as $addonKey => $validator) { if ($validator->fails()) { $errors['items'][$itemKey + 1]['addons'][$addonKey + 1] = $validator->errors(); } } } } if ($errors) { $response = ['error' => $errors]; $statusCode = 500; } else { $orderModel = new Models\Order(); $orderModel->getRepository()->saveFromArray($params); $orderView = new ModelViews\Order($orderModel); $response = $orderView->get(); } } } else { $response = ['error' => 'Empty or invalid body']; $statusCode = 500; } return \Response::json($response, $statusCode); }