Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 155 additions & 0 deletions Backend/app/Http/Controllers/Apis/StatiscalController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php

namespace App\Http\Controllers\Apis;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Services\StatisticalService;

class StatiscalController extends Controller
{
protected $statisticalService;

public function __construct() {
$this->statisticalService = new StatisticalService();
}

/**
* @OA\Get(
* path="/api/v1/order-statistical",
* tags={"Statiscal"},
* summary="Get order statistics",
* description="Returns the total amount of orders within a specified date range and with a status of 'delivered'.",
* @OA\Parameter(
* name="start",
* in="query",
* description="Start datetime in format YYYY-MM-DD HH:MM:SS",
* required=false,
* @OA\Schema(
* type="string",
* format="date-time",
* example="2023-01-01 00:00:00"
* )
* ),
* @OA\Parameter(
* name="end",
* in="query",
* description="End datetime in format YYYY-MM-DD HH:MM:SS",
* required=false,
* @OA\Schema(
* type="string",
* format="date-time",
* example="2023-12-31 23:59:59"
* )
* ),
* @OA\Response(
* response=200,
* description="Success",
* @OA\JsonContent(
* type="object",
* @OA\Property(
* property="data",
* type="number",
* example=12345.67
* )
* )
* ),
* @OA\Response(
* response=400,
* description="Bad Request",
* @OA\JsonContent(
* type="object",
* @OA\Property(
* property="message",
* type="string",
* example="Invalid date format"
* )
* )
* ),
* @OA\Response(
* response=500,
* description="Internal Server Error",
* @OA\JsonContent(
* type="object",
* @OA\Property(
* property="message",
* type="string",
* example="An error occurred while processing your request"
* )
* )
* )
* )
*/
public function orderStatistical(Request $request) {
$result = $this->statisticalService->orderStatistical($request);
return response()->json($result, 200);
}

/**
* @OA\Get(
* path="/api/v1/count-order",
* tags={"Statiscal"},
* summary="Count orders based on criteria",
* description="Count the number of orders based on optional criteria such as user ID and order status.",
* @OA\Parameter(
* name="user_id",
* in="query",
* description="Filter orders by user ID",
* required=false,
* @OA\Schema(
* type="integer"
* )
* ),
* @OA\Parameter(
* name="order_status",
* in="query",
* description="Filter orders by status",
* required=false,
* @OA\Schema(
* type="string",
* enum={"pending", "processing", "delivered", "cancelled"}
* )
* ),
* @OA\Response(
* response=200,
* description="Successful operation",
* @OA\JsonContent(
* type="object",
* @OA\Property(
* property="total_orders",
* type="integer",
* example=50
* )
* )
* ),
* @OA\Response(
* response=400,
* description="Bad request",
* @OA\JsonContent(
* type="object",
* @OA\Property(
* property="message",
* type="string",
* example="Invalid user ID"
* )
* )
* ),
* @OA\Response(
* response=500,
* description="Internal server error",
* @OA\JsonContent(
* type="object",
* @OA\Property(
* property="message",
* type="string",
* example="Server error occurred"
* )
* )
* )
* )
*/
public function countOrder(Request $request) {
$result = $this->statisticalService->countOrder($request);
return response()->json($result, 200);
}
}
1 change: 1 addition & 0 deletions Backend/app/Models/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ public function user() {
return $this->belongsTo(User::class, 'user_id');
}
}

37 changes: 37 additions & 0 deletions Backend/app/Services/StatisticalService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
namespace App\Services;

use App\Models\Order;

class StatisticalService {
public function orderStatistical($request) {
$start = $request->query('start');
$end = $request->query('end');

$query = Order::query();

if ($request->has('start') && $request->has('end')) {
$query->whereBetween('order_date', [$start, $end]);
}

$query->where('status', 'delivered');

$orderTotal = $query->sum('total_amount');

return ['data' => $orderTotal];
}

public function countOrder($request) {
$status = $request->query('status');

$query = Order::query();

if ($request->has('status')) {
$query->where('status', $status);
}

$orderTotal = $query->count();

return ['data' => $orderTotal];
}
}
3 changes: 2 additions & 1 deletion Backend/config/jwt.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@
|
*/

'ttl' => env('JWT_TTL', 120),
'ttl' => env('JWT_TTL', ),
'refresh_ttl' => env('JWT_REFRESH_TTL', ),

/*
|--------------------------------------------------------------------------
Expand Down
4 changes: 4 additions & 0 deletions Backend/routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use App\Http\Controllers\Apis\BrandController;
use App\Http\Controllers\Apis\OrderController;
use App\Http\Controllers\Apis\OrderDetailController;
use App\Http\Controllers\Apis\StatiscalController;

/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -82,6 +83,9 @@

Route::get('/order-details', [OrderDetailController::class, 'index'])->middleware('jwt.auth');
Route::get('/orders/{id}/order-details', [OrderDetailController::class, 'getOrderId'])->middleware('jwt.auth');

Route::get('/order-statistical', [StatiscalController::class, 'orderStatistical']);
Route::get('/order-count', [StatiscalController::class, 'countOrder']);
});

Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
Expand Down
Loading