Refactor order creation logic by introducing CreateOrderService and removing the create method from OrderRepository. Update ProductRepository with stricter type declarations and fix minor typos in comments.

This commit is contained in:
2026-02-05 01:29:10 +05:00
parent d0f962220c
commit eaae8b9be9
5 changed files with 160 additions and 54 deletions

View File

@@ -1,58 +1,17 @@
<?php
declare(strict_types=1);
namespace App\Repositories\Ecommerce\Order;
use App\Events\Ecommerce\Product\Order\OrderCreated;
use App\Models\Ecommerce\Product\Order\Order;
use App\Models\Ecommerce\Product\Order\Shipping\OrderShipping;
use Illuminate\Support\Facades\DB;
class OrderRepository
{
/**
* Order repo
*/
public function __construct(
protected array $data = [],
) {}
/**
* Create new order
*/
public function create()
{
$order = Order::create($this->data);
auth()->user()->carts()->with(['product' => [
'media',
'channels',
]])->get()->each(function ($cart) use ($order) {
DB::table('order_items')->insert([
'product_name' => $cart->product->name,
'product_id' => $cart->product_id,
'order_id' => $order->id,
'channel_id' => $cart->product->channels->first()?->id ?? tmpostChannel()->id,
'quantity' => $cart->product_quantity,
'unit_price_amount' => $cart->product->price_amount,
'unit_cost_amount' => $cart->product->cost_amount,
'created_at' => now(),
'updated_at' => now(),
]);
$cart->product->update([
'stock' => $cart->product->stock - $cart->product_quantity,
]);
});
auth()->user()->carts()->delete();
OrderCreated::dispatch($order);
return $order;
}
/**
* Available times
*
* @return array<string, mixed>
*/
public static function availableTimes(): array
{