Added gurad names get api for guard dashboard
This commit is contained in:
124
app/Http/Controllers/ModuleGuardNameController.php
Normal file
124
app/Http/Controllers/ModuleGuardNameController.php
Normal file
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\GuardPatrolEntry;
|
||||
use App\Models\Plant;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ModuleGuardNameController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function get_guard_name_Data(Request $request)
|
||||
{
|
||||
$expectedUser = env('API_AUTH_USER');
|
||||
$expectedPw = env('API_AUTH_PW');
|
||||
$header_auth = $request->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');
|
||||
$dateHeader = $request->header('date');
|
||||
|
||||
if (!$plantName) {
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => "Plant value can't be empty"
|
||||
], 404);
|
||||
}
|
||||
else if (!$dateHeader) {
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => "Date value can't be empty"
|
||||
], 404);
|
||||
}
|
||||
|
||||
$plant = Plant::where('name', $plantName)->first();
|
||||
if (!$plant) {
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => "Plant '{$plantName}' not found!"
|
||||
], 404);
|
||||
}
|
||||
|
||||
$selectedPlant = $plant->id;
|
||||
|
||||
try
|
||||
{
|
||||
$selectedDate = Carbon::createFromFormat('d-m-Y', $dateHeader)->format('Y-m-d');
|
||||
}
|
||||
catch (\Exception $e) {
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => "Invalid date format. Expected format: d-m-Y"
|
||||
], 400);
|
||||
}
|
||||
//$selectedDate = Carbon::createFromFormat('d-m-Y', $dateHeader)->format('Y-m-d');
|
||||
|
||||
$guardNames = GuardPatrolEntry::join('guard_names', 'guard_patrol_entries.guard_name_id', '=', 'guard_names.id')
|
||||
->where('guard_patrol_entries.plant_id', $selectedPlant)
|
||||
->whereDate('guard_patrol_entries.patrol_time', $selectedDate)
|
||||
->select('guard_names.name')
|
||||
->distinct()
|
||||
->orderBy('guard_names.name')
|
||||
->pluck('name');
|
||||
|
||||
if ($guardNames->isEmpty()) {
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => 'Guard names not found for the selected plant and date.'
|
||||
], 404);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'status_code' => 'SUCCESS',
|
||||
'status_description' => $guardNames,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user