diff --git a/app/Constants/OrderConstants.php b/app/Constants/OrderConstants.php new file mode 100644 index 0000000..2494ada --- /dev/null +++ b/app/Constants/OrderConstants.php @@ -0,0 +1,31 @@ +validated(); - if (auth()->user()->baskets()->where('status', 'created')->count() == 0){ + if (auth()->user()->baskets()->where('status', 'created')->count() == 0) { return $this->success(null, 'Empty', 'Your shopping cart is empty'); - } + } + $products = auth()->user()->baskets()->where('status', 'created')->get(); + auth()->user()->baskets()->where('status', 'created')->update([ 'status' => 'paid' ]); @@ -87,12 +90,21 @@ public function buy(BasketBuyRequest $request) $method = 'express'; } - Order::query()->create([ + $order = Order::query()->create([ + 'code' => rand(), 'user_id' => auth()->user()->id, 'address_id' => $validated['address_id'], 'method' => $method ]); + foreach($products as $product){ + OrderProduct::query()->create([ + 'order_id' => $order->id, + 'product_id' => $product->id, + 'count' => $product->count + ]); + } + return $this->success(null, 'Success', 'Your purchase was successful'); } } diff --git a/app/Http/Controllers/api/v1/OrderController.php b/app/Http/Controllers/api/v1/OrderController.php new file mode 100644 index 0000000..1eeab48 --- /dev/null +++ b/app/Http/Controllers/api/v1/OrderController.php @@ -0,0 +1,51 @@ +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'); + } catch (\Exception $e) { + Log::error('Error retrieving orders: ' . $e->getMessage()); + return $this->error('An error occurred while fetching orders.'); + } + } +} diff --git a/app/Models/Order.php b/app/Models/Order.php index 5805a87..3ecbe4c 100644 --- a/app/Models/Order.php +++ b/app/Models/Order.php @@ -10,7 +10,19 @@ class Order extends Model use HasFactory; protected $fillable = [ + 'code', 'user_id', - 'address_id' + 'address_id', + 'status' ]; + + + public function products(){ + return $this->hasMany(OrderProduct::class); + } + + public function address() + { + return $this->belongsTo(Address::class); + } } diff --git a/app/Models/OrderProduct.php b/app/Models/OrderProduct.php new file mode 100644 index 0000000..fc13ffc --- /dev/null +++ b/app/Models/OrderProduct.php @@ -0,0 +1,22 @@ +belongsTo(Product::class); + } +} diff --git a/app/Models/User.php b/app/Models/User.php index 721cf7d..e068d0f 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -91,4 +91,9 @@ public function baskets() { return $this->hasMany(Basket::class); } + + public function orders() + { + return $this->hasMany(Order::class); + } } diff --git a/database/migrations/2024_02_23_111535_create_order_products_table.php b/database/migrations/2024_02_23_111535_create_order_products_table.php new file mode 100644 index 0000000..dd483d2 --- /dev/null +++ b/database/migrations/2024_02_23_111535_create_order_products_table.php @@ -0,0 +1,30 @@ +id(); + $table->foreignId('order_id'); + $table->foreignId('product_id'); + $table->integer('count')->default(1); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('order_products'); + } +}; diff --git a/database/migrations/2024_02_23_111738_add_code_to_order_table.php b/database/migrations/2024_02_23_111738_add_code_to_order_table.php new file mode 100644 index 0000000..87efea9 --- /dev/null +++ b/database/migrations/2024_02_23_111738_add_code_to_order_table.php @@ -0,0 +1,30 @@ +string('code')->after('id')->nullable(); + $table->enum('status',['active','created','success','failed','cancelled'])->after('code')->default('success'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('orders', function (Blueprint $table) { + $table->dropColumn('code'); + $table->dropColumn('status'); + }); + } +}; diff --git a/routes/api.php b/routes/api.php index 09dd72c..ee9d72f 100644 --- a/routes/api.php +++ b/routes/api.php @@ -7,6 +7,7 @@ use App\Http\Controllers\api\LikeController; use App\Http\Controllers\api\ProductController; use App\Http\Controllers\api\ProfileController; +use App\Http\Controllers\api\v1\OrderController; use Illuminate\Support\Facades\Route; Route::prefix('v1')->group(function () { @@ -39,6 +40,9 @@ Route::post('buy',[BasketController::class, 'buy'])->name('api.basket.buy'); }); + Route::prefix('orders')->group(function(){ + Route::get('',[OrderController::class,'index']); + }); Route::get('address', [ProfileController::class, 'address'])->name('api.address'); Route::post('address', [ProfileController::class, 'store_address'])->name('api.address.store');