Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Admin dashboard cards #402

Merged
merged 10 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
Empty file added ,
Empty file.
Empty file.
Empty file added ResponseHTTP_OK,
Empty file.
78 changes: 78 additions & 0 deletions app/Http/Controllers/Api/V1/Admin/AdminDashboardController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace App\Http\Controllers\Api\V1\Admin;

use App\Http\Controllers\Controller;
use App\Models\Order;
use App\Models\User;
use App\Models\Product;
use Illuminate\Http\Response;

class AdminDashboardController extends Controller
{
public function getStatistics()
{
$currentMonth = now()->startOfMonth();
$lastMonth = now()->subMonth()->startOfMonth();

// Revenue statistics
$currentMonthRevenue = Order::where('created_at', '>=', $currentMonth)
->sum('total_amount');
$lastMonthRevenue = Order::whereBetween('created_at', [$lastMonth, $currentMonth])
->sum('total_amount');
$revenuePercentageDifference = $this->calculatePercentageDifference($currentMonthRevenue, $lastMonthRevenue);

// User statistics
$totalUsers = User::count();
$lastMonthUsers = User::where('created_at', '<', $currentMonth)->count();
$userPercentageDifference = $this->calculatePercentageDifference($totalUsers, $lastMonthUsers);

// Product statistics
$totalProducts = Product::count();
$lastMonthProducts = Product::where('created_at', '<', $currentMonth)->count();
$productPercentageDifference = $this->calculatePercentageDifference($totalProducts, $lastMonthProducts);

// Lifetime sales statistics
$lifetimeSales = Order::sum('total_amount');
$lastMonthSales = Order::where('created_at', '<', $currentMonth)->sum('total_amount');
$salesPercentageDifference = $this->calculatePercentageDifference($lifetimeSales, $lastMonthSales);

return response()->json([
'message' => 'Dashboard statistics retrieved successfully',
'status_code' => Response::HTTP_OK,
'data' => [
'total_revenue' => [
'current_month' => $currentMonthRevenue,
'previous_month' => $lastMonthRevenue,
'percentage_difference' => round($revenuePercentageDifference, 2) . '%',
],
'total_users' => [
'current_month' => $totalUsers,
'previous_month' => $lastMonthUsers,
'percentage_difference' => round($userPercentageDifference, 2) . '%',
],
'total_products' => [
'current_month' => $totalProducts,
'previous_month' => $lastMonthProducts,
'percentage_difference' => round($productPercentageDifference, 2) . '%',
],
'lifetime_sales' => [
'current_month' => $lifetimeSales,
'previous_month' => $lastMonthSales,
'percentage_difference' => round($salesPercentageDifference, 2) . '%',
],
]
]);

}

private function calculatePercentageDifference($current, $previous)
{
if ($previous > 0 && $current > 0) {
return (($current - $previous) / $previous) * 100;
} elseif ($current > 0 && $previous === 0) {
return 100;
}
return 0;
}
}
152 changes: 115 additions & 37 deletions resources/docs/documentation.json
Original file line number Diff line number Diff line change
Expand Up @@ -500,53 +500,131 @@
},
"/api/v1/statistics": {
"get": {
"tags": [
"Dashboard"
],
"summary": "Get dashboard statistics",
"description": "Retrieve the statistics for the dashboard, including total revenue, subscriptions, and sales.",
"responses": {
"200": {
"description": "Successful operation",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/inline_response_200_5"
"tags": [
"Admin Dashboard"
],
"summary": "Get admin dashboard statistics",
"description": "Retrieve the statistics for the admin dashboard, including total revenue, users, products, and lifetime sales.",
"responses": {
"200": {
"description": "Successful operation",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"message": {
"type": "string",
"example": "Dashboard statistics retrieved successfully"
},
"status_code": {
"type": "integer",
"example": 200
},
"data": {
"type": "object",
"properties": {
"revenue": {
"type": "object",
"properties": {
"current_month": {
"type": "number",
"example": 45000
},
"previous_month": {
"type": "number",
"example": 37500
},
"percentage_difference": {
"type": "string",
"example": "20%"
}
}
},
"application/xml": {
"schema": {
"$ref": "#/components/schemas/inline_response_200_5"
"users": {
"type": "object",
"properties": {
"current_month": {
"type": "integer",
"example": 4000
},
"previous_month": {
"type": "integer",
"example": 3636
},
"percentage_difference": {
"type": "string",
"example": "10%"
}
}
}
},
"400": {
"description": "Bad Request",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/inline_response_400"
}
}
}
},
"500": {
"description": "Internal Server Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/inline_response_500_3"
},
"products": {
"type": "object",
"properties": {
"current_month": {
"type": "integer",
"example": 1000
},
"previous_month": {
"type": "integer",
"example": 833
},
"percentage_difference": {
"type": "string",
"example": "20%"
}
}
},
"lifetime_sales": {
"type": "object",
"properties": {
"current_month": {
"type": "number",
"example": 450000
},
"previous_month": {
"type": "number",
"example": 180000
},
"percentage_difference": {
"type": "string",
"example": "150%"
}
}
}
}
}
}
}
}
}
},
"security": [
{
"bearerAuth": []
"400": {
"description": "Bad Request",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/inline_response_400"
}
]
}
}
},
"500": {
"description": "Internal Server Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/inline_response_500_3"
}
}
}
}
},
"security": [
{
"bearerAuth": []
}
]
}
},
"/api/v1/export/{format}": {
Expand Down
4 changes: 2 additions & 2 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@
use App\Http\Controllers\QuestController;
use App\Http\Controllers\UserNotificationController;
use Illuminate\Support\Facades\Route;


use App\Http\Controllers\Api\V1\Admin\AdminDashboardController;
/*
|--------------------------------------------------------------------------
| API Routes
Expand Down Expand Up @@ -235,6 +234,7 @@
Route::delete('/blogs/{id}', [BlogController::class, 'destroy']);
Route::get('/waitlists', [WaitListController::class, 'index']);
Route::apiResource('squeeze-pages', SqueezePageCoontroller::class);
Route::get('/statistics', [AdminDashboardController::class, 'getStatistics']);
});

Route::post('/waitlists', [WaitListController::class, 'store']);
Expand Down
Loading