Skip to content

Commit

Permalink
Update -> edit api get orders
Browse files Browse the repository at this point in the history
  • Loading branch information
soheilkhaledabdi committed Feb 23, 2024
1 parent 4fce6b5 commit 900567a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 29 deletions.
12 changes: 12 additions & 0 deletions app/Constants/OrderConstants.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,16 @@ public static function getStatusFromId($id)
return self::ACTIVE;
}
}

public static function getStatusFromString($status)
{
switch ($status) {
case self::SUCCESS:
return 1;
case self::FAILED:
return 2;
default:
return self::ACTIVE;
}
}
}
77 changes: 48 additions & 29 deletions app/Http/Controllers/api/v1/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,59 @@ class OrderController extends Controller
{
use BaseApiResponse;


public function index(Request $request)
public function index()
{
try {
$status = OrderConstants::getStatusFromId($request->get('status', 0));
$orders = auth()->user()->orders()
->where('status', $status)
->with('products.product', 'address')
->get()
->map(function ($order) {
return [
'code' => $order->code,
'products' => $order->products->map(function ($product) {
return [
'title' => $product->product->title,
'image' => $product->product->image
];
}),
'shipping_type' => $order->method,
'status' => $order->status,
'address' => [
"address" => $order->address->address,
"city" => $order->address->city,
"county" => $order->address->county,
"state" => $order->address->state,
],
'created_at' => $order->created_at,
];
});

return $this->success($orders, 'Order', 'Order list completed');
$ordersActive = $this->getOrdersByStatus(OrderConstants::ACTIVE);
$ordersSuccess = $this->getOrdersByStatus(OrderConstants::SUCCESS);
$ordersFail = $this->getOrdersByStatus(OrderConstants::FAILED);

return $this->success([
'active' => $ordersActive,
'success' => $ordersSuccess,
'fail' => $ordersFail
], 'Order', 'Order list completed');
} catch (\Exception $e) {
Log::error('Error retrieving orders: ' . $e->getMessage());
return $this->error('An error occurred while fetching orders.');
}
}

protected function getOrdersByStatus($status)
{
return auth()->user()->orders()
->where('status', $status)
->with('products.product', 'address')
->get()
->map(function ($order) {
return [
'code' => $order->code,
'products' => $this->mapProducts($order->products),
'shipping_type' => $order->method,
'status' => OrderConstants::getStatusFromString($order->status),
'address' => $this->mapAddress($order->address),
'created_at' => $order->created_at,
];
});
}

protected function mapProducts($products)
{
return $products->map(function ($product) {
return [
'title' => $product->product->title,
'image' => $product->product->image
];
});
}

protected function mapAddress($address)
{
return [
"address" => $address->address,
"city" => $address->city,
"county" => $address->county,
"state" => $address->state,
];
}
}

0 comments on commit 900567a

Please sign in to comment.