Skip to content

Commit

Permalink
Orders endpoint: Make sure we get the customer_id in the request (#79)
Browse files Browse the repository at this point in the history
* Make sure we get the customer_id in the request

* wip

Signed-off-by: Sam Poyigi <[email protected]>

* refactor

Signed-off-by: Sam Poyigi <[email protected]>

* wip

Signed-off-by: Sam Poyigi <[email protected]>

* Move logic to repository layer

Signed-off-by: Sam Poyigi <[email protected]>

* wip

Signed-off-by: Sam Poyigi <[email protected]>

* wip

Signed-off-by: Sam Poyigi <[email protected]>

Signed-off-by: Sam Poyigi <[email protected]>
Co-authored-by: Sam Poyigi <[email protected]>
  • Loading branch information
ryanmitchell and sampoyigi authored Aug 28, 2022
1 parent 62df17d commit 174f927
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 42 deletions.
7 changes: 3 additions & 4 deletions actions/RestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Igniter\Api\Classes\AbstractRepository;
use Igniter\Api\Traits\RestExtendable;
use Igniter\Flame\Exception\ValidationException;
use Illuminate\Support\Facades\Request;
use System\Classes\ControllerAction;

/**
Expand Down Expand Up @@ -175,7 +174,7 @@ protected function createTransformer()
*/
protected function validateRequest($requestMethod)
{
$requestData = Request::$requestMethod();
$requestData = request()->$requestMethod();

try {
if ($requestMethod == 'query')
Expand All @@ -187,9 +186,9 @@ protected function validateRequest($requestMethod)
$request->setController($this->controller);
});

app()->make($requestClass);
$request = app()->make($requestClass);

return $requestData;
return $request->$requestMethod();
}

return $this->controller->restValidate($requestData);
Expand Down
33 changes: 4 additions & 29 deletions apiresources/Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Igniter\Api\ApiResources;

use Igniter\Api\Classes\ApiController;
use Illuminate\Support\Facades\Request;

/**
* Orders API Controller
Expand Down Expand Up @@ -37,33 +36,9 @@ public function restExtendQuery($query)
return $query;
}

public function store()
{
if (($token = $this->getToken()) && $token->isForCustomer())
Request::merge(['customer_id' => $token->tokenable_id]);

return $this->asExtension('RestController')->store();
}

public function update($recordId)
{
if (($token = $this->getToken()) && $token->isForCustomer())
Request::merge(['customer_id' => $token->tokenable_id]);

return $this->asExtension('RestController')->update($recordId);
}

public function restAfterSave($model)
{
$requireSave = false;
foreach (['order_date', 'order_time', 'location_id', 'processed', 'order_total'] as $field) {
if ($fieldValue = Request::get($field, false)) {
$model->$field = $fieldValue;
$requireSave = true;
}
}

if ($orderMenus = (array)Request::get('order_menus', [])) {
if ($orderMenus = (array)request()->input('order_menus', [])) {
$model->addOrderMenus(json_decode(json_encode($orderMenus)));

$total_items = 0;
Expand All @@ -74,10 +49,10 @@ public function restAfterSave($model)
$model->total_items = $total_items;
}

if ($orderTotals = (array)Request::get('order_totals', []))
if ($orderTotals = (array)request()->input('order_totals', []))
$model->addOrderTotals(json_decode(json_encode($orderTotals), true));

if ($orderStatus = Request::get('status_id', false))
$model->updateOrderStatus($orderStatus, ['comment' => Request::get('status_comment', null)]);
if ($orderStatus = request()->input('status_id', false))
$model->updateOrderStatus($orderStatus, ['comment' => request()->input('status_comment', null)]);
}
}
9 changes: 0 additions & 9 deletions apiresources/Reservations.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Igniter\Api\ApiResources;

use Igniter\Api\Classes\ApiController;
use Illuminate\Support\Facades\Request;

/**
* Reservations API Controller
Expand Down Expand Up @@ -36,12 +35,4 @@ public function restExtendQuery($query)

return $query;
}

public function store()
{
if (($token = $this->getToken()) && $token->isForCustomer())
Request::merge(['customer_id' => $token->tokenable_id]);

return $this->asExtension('RestController')->store();
}
}
3 changes: 3 additions & 0 deletions apiresources/requests/OrderRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function attributes()
'address.state' => lang('igniter.cart::default.checkout.label_state'),
'address.postcode' => lang('igniter.cart::default.checkout.label_postcode'),
'address.country_id' => lang('igniter.cart::default.checkout.label_country'),
'customer_id' => lang('igniter.api::default.orders.label_customer_id'),
];
}

Expand All @@ -37,12 +38,14 @@ public function rules()
'email' => ['sometimes', 'required', 'email:filter', 'max:96'],
'comment' => ['max:500'],
'payment' => ['sometimes', 'required', 'alpha_dash'],
'customer_id' => ['integer'],
];

if ($method == 'post') {
$rules['first_name'][] = 'required';
$rules['last_name'][] = 'required';
$rules['order_type'][] = 'required';
$rules['customer_id'][] = 'required';
}

if (Request::input('order_type', 'collection') == 'delivery') {
Expand Down
4 changes: 4 additions & 0 deletions language/en/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,8 @@
'alert_auth_restricted' => 'The API token doesn\'t have permissions to perform the request.',
'alert_token_restricted' => 'The API token doesn\'t have the right abilities to perform this action',
'alert_validation_failed' => 'Failed to validate request parameters',

'orders' => [
'label_customer_id' => 'Customer ID',
],
];

0 comments on commit 174f927

Please sign in to comment.