diff --git a/Backend/app/Http/Controllers/Apis/StatiscalController.php b/Backend/app/Http/Controllers/Apis/StatiscalController.php new file mode 100644 index 0000000..b1f2867 --- /dev/null +++ b/Backend/app/Http/Controllers/Apis/StatiscalController.php @@ -0,0 +1,155 @@ +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); + } +} diff --git a/Backend/app/Models/Order.php b/Backend/app/Models/Order.php index 9a7ee32..5122848 100644 --- a/Backend/app/Models/Order.php +++ b/Backend/app/Models/Order.php @@ -37,3 +37,4 @@ public function user() { return $this->belongsTo(User::class, 'user_id'); } } + diff --git a/Backend/app/Services/StatisticalService.php b/Backend/app/Services/StatisticalService.php new file mode 100644 index 0000000..672da54 --- /dev/null +++ b/Backend/app/Services/StatisticalService.php @@ -0,0 +1,37 @@ +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]; + } +} \ No newline at end of file diff --git a/Backend/config/jwt.php b/Backend/config/jwt.php index c1a9c81..92066d3 100644 --- a/Backend/config/jwt.php +++ b/Backend/config/jwt.php @@ -101,7 +101,8 @@ | */ - 'ttl' => env('JWT_TTL', 120), + 'ttl' => env('JWT_TTL', ), + 'refresh_ttl' => env('JWT_REFRESH_TTL', ), /* |-------------------------------------------------------------------------- diff --git a/Backend/routes/api.php b/Backend/routes/api.php index 724448f..c3e2414 100644 --- a/Backend/routes/api.php +++ b/Backend/routes/api.php @@ -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; /* |-------------------------------------------------------------------------- @@ -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) { diff --git a/Backend/storage/api-docs/api-docs.json b/Backend/storage/api-docs/api-docs.json index 611591e..0375b95 100644 --- a/Backend/storage/api-docs/api-docs.json +++ b/Backend/storage/api-docs/api-docs.json @@ -3961,6 +3961,176 @@ "security": [] } }, + "/api/v1/order-statistical": { + "get": { + "tags": [ + "Statiscal" + ], + "summary": "Get order statistics", + "description": "Returns the total amount of orders within a specified date range and with a status of 'delivered'.", + "operationId": "18e3fe4f40c2f8821c1a8b21873ebf91", + "parameters": [ + { + "name": "start", + "in": "query", + "description": "Start datetime in format YYYY-MM-DD HH:MM:SS", + "required": false, + "schema": { + "type": "string", + "format": "date-time", + "example": "2023-01-01 00:00:00" + } + }, + { + "name": "end", + "in": "query", + "description": "End datetime in format YYYY-MM-DD HH:MM:SS", + "required": false, + "schema": { + "type": "string", + "format": "date-time", + "example": "2023-12-31 23:59:59" + } + } + ], + "responses": { + "200": { + "description": "Success", + "content": { + "application/json": { + "schema": { + "properties": { + "data": { + "type": "number", + "example": 12345.67 + } + }, + "type": "object" + } + } + } + }, + "400": { + "description": "Bad Request", + "content": { + "application/json": { + "schema": { + "properties": { + "message": { + "type": "string", + "example": "Invalid date format" + } + }, + "type": "object" + } + } + } + }, + "500": { + "description": "Internal Server Error", + "content": { + "application/json": { + "schema": { + "properties": { + "message": { + "type": "string", + "example": "An error occurred while processing your request" + } + }, + "type": "object" + } + } + } + } + } + } + }, + "/api/v1/count-order": { + "get": { + "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.", + "operationId": "f8d5b98ad8cb4eae4df5329bd9659166", + "parameters": [ + { + "name": "user_id", + "in": "query", + "description": "Filter orders by user ID", + "required": false, + "schema": { + "type": "integer" + } + }, + { + "name": "order_status", + "in": "query", + "description": "Filter orders by status", + "required": false, + "schema": { + "type": "string", + "enum": [ + "pending", + "processing", + "delivered", + "cancelled" + ] + } + } + ], + "responses": { + "200": { + "description": "Successful operation", + "content": { + "application/json": { + "schema": { + "properties": { + "total_orders": { + "type": "integer", + "example": 50 + } + }, + "type": "object" + } + } + } + }, + "400": { + "description": "Bad request", + "content": { + "application/json": { + "schema": { + "properties": { + "message": { + "type": "string", + "example": "Invalid user ID" + } + }, + "type": "object" + } + } + } + }, + "500": { + "description": "Internal server error", + "content": { + "application/json": { + "schema": { + "properties": { + "message": { + "type": "string", + "example": "Server error occurred" + } + }, + "type": "object" + } + } + } + } + } + } + }, "/api/v1/subcategories": { "get": { "tags": [