From 658dea8e728a1e2acd2433811e0e0d04269747c6 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Fri, 11 Jul 2025 15:32:53 +0530 Subject: [PATCH] Added all get api for modules data --- .../Controllers/ModuleChartController.php | 103 +++++++++++++ .../Controllers/ModuleFilterController.php | 88 +++++++++++ .../ModuleFilterDataController.php | 144 ++++++++++++++++++ .../Controllers/ModulePlantController.php | 94 ++++++++++++ .../Controllers/ModulePlantLineController.php | 118 ++++++++++++++ routes/api.php | 15 ++ 6 files changed, 562 insertions(+) create mode 100644 app/Http/Controllers/ModuleChartController.php create mode 100644 app/Http/Controllers/ModuleFilterController.php create mode 100644 app/Http/Controllers/ModuleFilterDataController.php create mode 100644 app/Http/Controllers/ModulePlantController.php create mode 100644 app/Http/Controllers/ModulePlantLineController.php diff --git a/app/Http/Controllers/ModuleChartController.php b/app/Http/Controllers/ModuleChartController.php new file mode 100644 index 0000000..b860e08 --- /dev/null +++ b/app/Http/Controllers/ModuleChartController.php @@ -0,0 +1,103 @@ +header('Authorization'); + $expectedToken = $expectedUser . ':' . $expectedPw; + + if ("Bearer " . $expectedToken != $header_auth) + { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Invalid authorization token!' + ], 403); + } + + $moduleName = $request->header('module-name'); + + if (empty($moduleName)) + { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Module Name can't be empty!" + ], 400); + } + + $exists = ModuleList::where('module_name', $moduleName)->exists(); + + if (!$exists) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Module Name '{$moduleName}' not found in system!" + ], 404); + } + + // Fetch all unique dashboard names for the given module_name + // $dashboardNames = ModuleList::where('module_name', $moduleName) + // ->distinct() + // ->pluck('dashboard_name'); + $dashboardNames = ModuleList::where('module_name', $moduleName) + ->orderBy('created_at', 'asc') + ->get() + ->unique('dashboard_name') + ->pluck('dashboard_name') + ->values(); // reset array keys + + + return response()->json([ + 'status_code' => 'SUCCESS', + 'status_description' => $dashboardNames + ]); + + } + + /** + * Store a newly created resource in storage. + */ + public function store(Request $request) + { + // + } + + /** + * Display the specified resource. + */ + public function show(string $id) + { + // + } + + /** + * Update the specified resource in storage. + */ + public function update(Request $request, string $id) + { + // + } + + /** + * Remove the specified resource from storage. + */ + public function destroy(string $id) + { + // + } +} diff --git a/app/Http/Controllers/ModuleFilterController.php b/app/Http/Controllers/ModuleFilterController.php new file mode 100644 index 0000000..6a3bf00 --- /dev/null +++ b/app/Http/Controllers/ModuleFilterController.php @@ -0,0 +1,88 @@ +header('Authorization'); + $expectedToken = $expectedUser . ':' . $expectedPw; + + if ("Bearer " . $expectedToken != $header_auth) + { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Invalid authorization token!' + ], 403); + } + + $lineName = $request->header('line-name'); + + if (empty($lineName)) + { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Line name can't be empty!" + ], 400); + } + + $filterNames = ModuleList::orderBy('created_at', 'asc') + ->get() + ->unique('filter_name') + ->pluck('filter_name') + ->filter() + ->values(); + + return response()->json([ + 'status_code' => 'SUCCESS', + 'status_description' => $filterNames + ], 200); + } + + /** + * Store a newly created resource in storage. + */ + public function store(Request $request) + { + // + } + + /** + * Display the specified resource. + */ + public function show(string $id) + { + // + } + + /** + * Update the specified resource in storage. + */ + public function update(Request $request, string $id) + { + // + } + + /** + * Remove the specified resource from storage. + */ + public function destroy(string $id) + { + // + } +} diff --git a/app/Http/Controllers/ModuleFilterDataController.php b/app/Http/Controllers/ModuleFilterDataController.php new file mode 100644 index 0000000..9416933 --- /dev/null +++ b/app/Http/Controllers/ModuleFilterDataController.php @@ -0,0 +1,144 @@ +header('Authorization'); + $expectedToken = $expectedUser . ':' . $expectedPw; + + if ("Bearer " . $expectedToken != $header_auth) + { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Invalid authorization token!' + ], 403); + } + + $moduleName = $request->header('module-name'); + $chartName = $request->header('chart-name'); + $plantName = $request->header('plant-name'); + $lineName = $request->header('line-name'); + $filterName = $request->header('filter-name'); + + $requiredHeaders = [ + 'module-name', + 'chart-name', + 'plant-name', + 'line-name', + 'filter-name', + ]; + + $missingHeaders = []; + + foreach ($requiredHeaders as $header) { + if (empty($request->header($header))) { + $missingHeaders[] = $header; + } + } + + if (!empty($missingHeaders)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Missing required headers: ' . implode(', ', $missingHeaders) + ], 400); + } + + // Fetch plant ID + $plant = Plant::where('name', $plantName)->first(); + if (!$plant) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Plant '{$plantName}' not found!" + ], 404); + } + + // Fetch line ID + $line = Line::where('name', $lineName)->where('plant_id', $plant->id)->first(); + if (!$line) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Line '{$lineName}' not found for plant '{$plantName}'" + ], 404); + } + + $activeFilter = strtolower(trim($request->header('filter-name'))); + + if ($activeFilter == 'yesterday') { + $startDate = now()->subDay()->setTime(8, 0, 0); + $endDate = now()->setTime(8, 0, 0); + } + elseif ($activeFilter == 'this_week') { + $startDate = now()->startOfWeek()->setTime(8, 0, 0); + $endDate = now()->endOfWeek()->addDay()->setTime(8, 0, 0); + } + elseif ($activeFilter == 'this_month') { + $startDate = now()->startOfMonth(); + $endDate = now()->endOfMonth(); + } + else { // today or default + $startDate = now()->setTime(8, 0, 0); + $endDate = now()->copy()->addDay()->setTime(8, 0, 0); + } + + $rowCount = ProductionQuantity::where('plant_id', $plant->id) + ->where('line_id', $line->id) + ->whereBetween('created_at', [$startDate, $endDate]) + ->count(); + + return response()->json([ + 'status_code' => 'SUCCESS', + 'status_description' => $rowCount + ], 200); + } + + /** + * Store a newly created resource in storage. + */ + public function store(Request $request) + { + // + } + + /** + * Display the specified resource. + */ + public function show(string $id) + { + // + } + + /** + * Update the specified resource in storage. + */ + public function update(Request $request, string $id) + { + // + } + + /** + * Remove the specified resource from storage. + */ + public function destroy(string $id) + { + // + } +} diff --git a/app/Http/Controllers/ModulePlantController.php b/app/Http/Controllers/ModulePlantController.php new file mode 100644 index 0000000..5acaaf9 --- /dev/null +++ b/app/Http/Controllers/ModulePlantController.php @@ -0,0 +1,94 @@ +header('Authorization'); + $expectedToken = $expectedUser . ':' . $expectedPw; + + if ("Bearer " . $expectedToken != $header_auth) + { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Invalid authorization token!' + ], 403); + } + + $plantName = $request->header('plant-name'); + + if (empty($plantName)) + { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Plant Name can't be empty!" + ], 400); + } + + $headerValue = $request->header('plant-name'); + + if ($headerValue != 'Plant List') { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Invalid value for 'plant-name' header!" + ], 400); + } + + $plantNames = Plant::orderBy('created_at', 'asc') + ->pluck('name') + ->values(); + + return response()->json([ + 'status_code' => 'SUCCESS', + 'status_description' => $plantNames + ]); + } + + /** + * Store a newly created resource in storage. + */ + public function store(Request $request) + { + // + } + + /** + * Display the specified resource. + */ + public function show(string $id) + { + // + } + + /** + * Update the specified resource in storage. + */ + public function update(Request $request, string $id) + { + // + } + + /** + * Remove the specified resource from storage. + */ + public function destroy(string $id) + { + // + } +} diff --git a/app/Http/Controllers/ModulePlantLineController.php b/app/Http/Controllers/ModulePlantLineController.php new file mode 100644 index 0000000..ccea0c4 --- /dev/null +++ b/app/Http/Controllers/ModulePlantLineController.php @@ -0,0 +1,118 @@ +header('Authorization'); + $expectedToken = $expectedUser . ':' . $expectedPw; + + if ("Bearer " . $expectedToken != $header_auth) + { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Invalid authorization token!' + ], 403); + } + + $plantName = $request->header('plant-name'); + + if (empty($plantName)) + { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Plant Name can't be empty!" + ], 400); + } + + $plant = Plant::where('name', $plantName)->first(); + + if (!$plant) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Plant '{$plantName}' not found!" + ], 404); + } + + // // Get line names for the plant + // $lineNames = Line::where('plant_id', $plant->id) + // ->orderBy('created_at', 'asc') + // ->pluck('name') + // ->prepend('All Lines') + // ->values(); + + // return response()->json([ + // 'status_code' => 'SUCCESS', + // 'status_description' => $lineNames, + // ], 200); + + $lineNames = Line::where('plant_id', $plant->id) + ->orderBy('created_at', 'asc') + ->pluck('name'); + + if ($lineNames->isEmpty()) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "No lines found for plant '{$plantName}'" + ], 404); + } + + // Prepend 'All Lines' + $lineNames->prepend('All Lines')->values(); + + return response()->json([ + 'status_code' => 'SUCCESS', + 'status_description' => $lineNames, + ], 200); + + } + + /** + * Store a newly created resource in storage. + */ + public function store(Request $request) + { + // + } + + /** + * Display the specified resource. + */ + public function show(string $id) + { + // + } + + /** + * Update the specified resource in storage. + */ + public function update(Request $request, string $id) + { + // + } + + /** + * Remove the specified resource from storage. + */ + public function destroy(string $id) + { + // + } +} diff --git a/routes/api.php b/routes/api.php index 0b31a70..ad95c31 100644 --- a/routes/api.php +++ b/routes/api.php @@ -1,7 +1,12 @@