From 89598f8cfc1afa06e610befa7a233e33815f1a13 Mon Sep 17 00:00:00 2001 From: Xiaoguang Sun Date: Sun, 28 Apr 2024 15:01:51 +0000 Subject: [PATCH] Some small refactoring 1. A small refactor to app/Exceptions/Handler.php 2. Try count 6 different metrics at once to save some database roundtrips. Signed-off-by: Xiaoguang Sun --- app/Exceptions/Handler.php | 16 +++++----------- app/Http/Controllers/HomeController.php | 19 ++++++++++++------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 7bb2a56a..eebf2a20 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -50,17 +50,11 @@ public function render($request, Throwable $exception) { // return parent::render($request, $exception); if ($exception instanceof HttpExceptionInterface) { - if (env('APP_ENV') === 'production' && $exception->getCode() == 403) { - return response()->view('errors.403', [], 403); - } - if (env('APP_ENV') === 'production' && $exception->getCode() == 404) { - return response()->view('errors.404', [], 404); - } - if (env('APP_ENV') === 'production' && $exception->getCode() == 419) { - return response()->view('errors.419', [], 419); - } - if (env('APP_ENV') === 'production' && $exception->getCode() == 500) { - return response()->view('errors.500', [], 500); + if (env('APP_ENV') === 'production') { + $code = $exception->getCode(); + if (in_array($code, [403, 404, 419, 500])) { + return response()->view("errors.{$code}", [], $code); + } } } diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index aff6b910..145e3014 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -30,13 +30,18 @@ public function index() $userRoleCount = $user->getRoleNames()->count(); // Display total customers, suppliers, quotes, orders, NC - $data['customers_count'] = DB::table('companies')->where('statu_customer', '=', '2')->count(); - $data['suppliers_count'] = DB::table('companies')->where('statu_supplier', '=', '2')->count(); - $data['quotes_count'] = DB::table('quotes')->count(); - $data['orders_count'] = DB::table('orders')->count(); - $data['quality_non_conformities_count'] = DB::table('quality_non_conformities')->count(); - $data['user_count'] = DB::table('users')->count(); - + $all_count = DB::table('users')->selectRaw("'user_count' as type, count(*) as count") + ->unionAll(DB::table('companies')->selectRaw("'customers_count' as type, count(*) as count")->where('statu_customer', '=', '2')) + ->unionAll(DB::table('companies')->selectRaw("'suppliers_count' as type, count(*) as count")->where('statu_supplier', '=', '2')) + ->unionAll(DB::table('quotes')->selectRaw("'quotes_count' as type, count(*) as count")) + ->unionAll(DB::table('orders')->selectRaw("'orders_count' as type, count(*) as count")) + ->unionAll(DB::table('quality_non_conformities')->selectRaw("'quality_non_conformities_count' as type, count(*) as count")) + ->get(); + $data = $all_count->reduce(function($data, $count) { + $data[$count->type] = $count->count; + return $data; + }, []); + $Announcement = Announcements::latest()->first(); //Estimated Budgets data for chart