add admin panel #20

Merged
csimonis merged 12 commits from feature/admin-panel into main 2025-02-09 14:59:31 +00:00
5 changed files with 59 additions and 3 deletions
Showing only changes of commit ea7b2e6aea - Show all commits

View File

@ -5,25 +5,29 @@ namespace App\Controller\Admin;
use App\Entity\Customer;
use App\Entity\Payment;
use App\Entity\Ticket;
use App\Repository\PaymentRepository;
use App\Repository\TicketRepository;
use EasyCorp\Bundle\EasyAdminBundle\Attribute\AdminDashboard;
use EasyCorp\Bundle\EasyAdminBundle\Config\Dashboard;
use EasyCorp\Bundle\EasyAdminBundle\Config\MenuItem;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractDashboardController;
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
#[AdminDashboard(routePath: '/admin', routeName: 'admin')]
class ViewController extends AbstractDashboardController
{
public function __construct(private AdminUrlGenerator $adminUrlGenerator)
public function __construct(private readonly PaymentRepository $paymentRepository, private readonly TicketRepository $ticketRepository)
{
}
#[Route('/admin', name: 'admin')]
public function index(): Response
{
return $this->redirect($this->adminUrlGenerator->setController(CustomerCrudController::class)->generateUrl());
return $this->render('admin/dashboard.html.twig', [
'total' => $this->paymentRepository->getTotalMoneyMade(),
'foodData' => $this->ticketRepository->getFoodData(),
]);
}
public function configureDashboard(): Dashboard
@ -33,6 +37,7 @@ class ViewController extends AbstractDashboardController
public function configureMenuItems(): iterable
{
yield MenuItem::linktoDashboard('Dashboard', 'fa fa-home');
yield MenuItem::linkToCrud('Customers', 'fa fa-users', Customer::class);
yield MenuItem::linkToCrud('Tickets', 'fa fa-ticket', Ticket::class);
yield MenuItem::linkToCrud('Payments', 'fa fa-money', Payment::class);

View File

@ -0,0 +1,13 @@
<?php
namespace App\DataObjects;
class FoodData
{
public function __construct(
public int $totalMeat = 0,
public int $totalVegetarian = 0,
public int $totalVegan = 0,
) {
}
}

View File

@ -15,4 +15,15 @@ class PaymentRepository extends ServiceEntityRepository
{
parent::__construct($registry, Payment::class);
}
public function getTotalMoneyMade(): float
{
$all = $this->findAll();
$total = 0.0;
foreach ($all as $payment) {
$total += $payment->getTotal();
}
return $total;
}
}

View File

@ -2,6 +2,7 @@
namespace App\Repository;
use App\DataObjects\FoodData;
use App\Entity\Ticket;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
@ -15,4 +16,22 @@ class TicketRepository extends ServiceEntityRepository
{
parent::__construct($registry, Ticket::class);
}
public function getFoodData(): FoodData
{
$qb = $this->createQueryBuilder('t')
->select(
'SUM(CASE WHEN t.foodType = 1 THEN 1 ELSE 0 END) as totalMeat',
'SUM(CASE WHEN t.foodType = 2 THEN 1 ELSE 0 END) as totalVegetarian',
'SUM(CASE WHEN t.foodType = 3 THEN 1 ELSE 0 END) as totalVegan'
);
$result = $qb->getQuery()->getSingleResult();
return new FoodData(
(int) $result['totalMeat'],
(int) $result['totalVegetarian'],
(int) $result['totalVegan']
);
}
}

View File

@ -0,0 +1,8 @@
{% extends '@EasyAdmin/page/content.html.twig' %}
{% block content %}
{{ total }}
meat: {{ foodData.totalMeat }}
vegetarian: {{ foodData.totalVegetarian }}
vegan: {{ foodData.totalVegan }}
{% endblock %}