-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a011fcb
commit d4648aa
Showing
9 changed files
with
201 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
database/migrations/2024_02_23_111535_create_order_products_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
30
database/migrations/2024_02_23_111738_add_code_to_order_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters