feat(admin): add dashboard, analytics and user controllers

This commit is contained in:
dualfroz committed 2026-08-25 19:06:01 +02:00
1 parent 1346766a73
commit d63bbcb29a
3 files changed
+81

No files matched your search

@@ -0,0 +1,22 @@
<?php
namespace Bazaar\Controllers\Admin;
use Bazaar\Controllers\Controller;
use Bazaar\Core\Response;
final class AnalyticsController extends Controller
{
public function index(): Response
{
return $this->view('admin.analytics', [
'salesByDay' => $this->ctx->analytics->salesByDay(14),
'topProducts' => $this->ctx->analytics->topProducts(5),
'averageOrderCents' => $this->ctx->analytics->averageOrderValueCents(),
'unitsSold' => $this->ctx->analytics->unitsSold(),
'couponUsage' => $this->ctx->analytics->couponUsage(),
'revenueCents' => $this->ctx->orders->revenueCents(),
'lowStock' => $this->ctx->products->lowStock(5),
], 'admin.layout');
}
}
@@ -0,0 +1,23 @@
<?php
namespace Bazaar\Controllers\Admin;
use Bazaar\Controllers\Controller;
use Bazaar\Core\Response;
final class DashboardController extends Controller
{
public function index(): Response
{
return $this->view('admin.dashboard', [
'productCount' => $this->ctx->products->count(),
'pendingCount' => $this->ctx->orders->countByStatus('pending'),
'paidCount' => $this->ctx->orders->countByStatus('paid'),
'fulfilledCount' => $this->ctx->orders->countByStatus('fulfilled'),
'orderCount' => $this->ctx->orders->count(),
'revenueCents' => $this->ctx->orders->revenueCents(),
'lowStock' => $this->ctx->products->lowStock(5),
'recentOrders' => $this->ctx->orders->recent(6),
], 'admin.layout');
}
}
+36
View File
@@ -0,0 +1,36 @@
<?php
namespace Bazaar\Controllers\Admin;
use Bazaar\Controllers\Controller;
use Bazaar\Core\Response;
use Bazaar\Support\Role;
final class UserController extends Controller
{
public function index(): Response
{
return $this->view('admin.users.index', [
'users' => $this->ctx->users->all(),
'roles' => Role::all(),
], 'admin.layout');
}
public function updateRole(string $id): Response
{
$userId = (int) $id;
$role = (string) $this->ctx->request->input('role', Role::customer);
if (!in_array($role, Role::all(), true)) {
return $this->redirect('/admin/users');
}
if ($userId === (int) $this->ctx->auth->id()) {
$this->ctx->session->flash('error', 'You cannot change your own role.');
return $this->redirect('/admin/users');
}
$this->ctx->users->setRole($userId, $role);
$this->ctx->session->flash('success', 'Role updated.');
return $this->redirect('/admin/users');
}
}