Skip to content

Commit

Permalink
Add -> add api orders
Browse files Browse the repository at this point in the history
  • Loading branch information
soheilkhaledabdi committed Feb 23, 2024
1 parent a011fcb commit d4648aa
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 4 deletions.
31 changes: 31 additions & 0 deletions app/Constants/OrderConstants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Constants;

class OrderConstants
{
public const ACTIVE = 'active';

public const CREATED = 'created';

public const SUCCESS = 'success';

public const FAILED = 'failed';

public const CANCELLED = 'cancelled';




public static function getStatusFromId($id)
{
switch ($id) {
case 1:
return self::SUCCESS;
case 2:
return self::FAILED;
default:
return self::ACTIVE;
}
}
}
18 changes: 15 additions & 3 deletions app/Http/Controllers/api/BasketController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Http\Resources\Basket\BasketResource;
use App\Models\Basket;
use App\Models\Order;
use App\Models\OrderProduct;
use App\Traits\BaseApiResponse;
use Illuminate\Http\JsonResponse;

Expand Down Expand Up @@ -68,9 +69,11 @@ public function delete(BasketDeleteRequest $request): JsonResponse
public function buy(BasketBuyRequest $request)
{
$validated = $request->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'
]);
Expand All @@ -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');
}
}
51 changes: 51 additions & 0 deletions app/Http/Controllers/api/v1/OrderController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace App\Http\Controllers\api\v1;

use App\Constants\OrderConstants;
use App\Http\Controllers\Controller;
use App\Traits\BaseApiResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class OrderController extends Controller
{
use BaseApiResponse;


public function index(Request $request)
{
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');
} catch (\Exception $e) {
Log::error('Error retrieving orders: ' . $e->getMessage());
return $this->error('An error occurred while fetching orders.');
}
}
}
14 changes: 13 additions & 1 deletion app/Models/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
22 changes: 22 additions & 0 deletions app/Models/OrderProduct.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class OrderProduct extends Model
{
use HasFactory;

protected $fillable = [
'order_id',
'product_id',
'count'
];

public function product()
{
return $this->belongsTo(Product::class);
}
}
5 changes: 5 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,9 @@ public function baskets()
{
return $this->hasMany(Basket::class);
}

public function orders()
{
return $this->hasMany(Order::class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('order_products', function (Blueprint $table) {
$table->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');
}
};
30 changes: 30 additions & 0 deletions database/migrations/2024_02_23_111738_add_code_to_order_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('orders', function (Blueprint $table) {
$table->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');
});
}
};
4 changes: 4 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit d4648aa

Please sign in to comment.