Files
aso/Modules/Linksehat/Http/Controllers/Api/HospitalController.php
Muhammad Fajar 302cc343fd api show hospital
2022-10-28 09:44:15 +07:00

147 lines
5.0 KiB
PHP

<?php
namespace Modules\Linksehat\Http\Controllers\Api;
use App\Helpers\Helper;
use App\Models\Organization;
use DB;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\Linksehat\Transformers\HospitalResource;
use Modules\Linksehat\Transformers\Hospitals\HospitalResourceDetail;
class HospitalController extends Controller
{
/**
* Display a listing of the resource.
* @return Renderable
*/
public function index(Request $request)
{
$hospitals = Organization::query()
->with(['currentAddress'])
->where('organizations.type', 'hospital')
->when($request->search ?? null, function ($query, $search) {
$query->where('name', 'LIKE', '%' . $search . '%');
});
if ($request->has('lat') && !empty($request->lat) && $request->has('lng') && !empty($request->lng)) {
$hospitals->leftJoin('addresses', function ($q) {
$q->on('organizations.main_address_id', '=', 'addresses.id');
$q->where('addresses.addressable_type', '=', Organization::class);
});
$hospitals->select([
"organizations.*",
// DB::raw("ST_Distance_Sphere(point(addresses.lng,addresses.lat), point(".$request->lng.",".$request->lat.")) /1000 as distance_km"),
DB::raw("6371 * acos (cos ( radians($request->lat) ) * cos( radians( addresses.lat ) ) * cos( radians( addresses.lng ) - radians($request->lng) ) + sin ( radians($request->lat) ) * sin( radians( addresses.lat ) )) as distance_km")
]);
$hospitals->orderBy('distance_km', 'asc');
} else {
$hospitals->orderBy('organizations.name', 'asc');
}
// if ($request->has('order_by')) {
// switch($request->order_by) {
// case 'distance_asc':
// if ($request->order_by == 'distance_asc' && $request->has('lat') && !empty($request->lat) && $request->has('lng') && !empty($request->lng)) {
// $hospitals->orderBy('distance_km', 'asc');
// break;
// }
// case 'distance_desc':
// if ($request->order_by == 'distance_desc' && $request->has('lat') && !empty($request->lat) && $request->has('lng') && !empty($request->lng)) {
// $hospitals->orderBy('distance_km', 'desc');
// break;
// }
// default:
// $hospitals->orderBy('organizations.name', 'asc');
// break;
// }
// }
$limit = $request->limit ?? 6;
if ($limit > 20) {
$limit = 20;
}
$hospitals = $hospitals->paginate($limit);
// dd($hospitals->toArray());
return response()->json([
'message' => 'Sukses mengambil data Rumah Sakit',
'hospitals' => Helper::paginateResources(HospitalResource::collection($hospitals))
]);
}
/**
* Show the form for creating a new resource.
* @return Renderable
*/
public function create()
{
return view('linksehat::create');
}
/**
* Store a newly created resource in storage.
* @param Request $request
* @return Renderable
*/
public function store(Request $request)
{
//
}
/**
* Show the specified resource.
* @param int $id
* @return Renderable
*/
public function show(Request $request, $id)
{
$queryHospitals = Organization::query()
->leftJoin('addresses', function ($q) {
$q->on('organizations.main_address_id', '=', 'addresses.id');
$q->where('addresses.addressable_type', '=', Organization::class);
});
if ($request->has('lat') && !empty($request->lat) && $request->has('lng') && !empty($request->lng)) {
$queryHospitals = $queryHospitals->selectRaw("organizations.*, addresses.text AS currentAddress, 6371 * acos (cos ( radians($request->lat) ) * cos( radians( addresses.lat ) ) * cos( radians( addresses.lng ) - radians($request->lng) ) + sin ( radians($request->lat) ) * sin( radians( addresses.lat ) )) as distance")->orderBy('distance', 'ASC');
}
$queryHospitals = $queryHospitals->orderBy('organizations.name', 'asc')->findOrFail($id);
return HospitalResourceDetail::make($queryHospitals);
}
/**
* Show the form for editing the specified resource.
* @param int $id
* @return Renderable
*/
public function edit($id)
{
return view('linksehat::edit');
}
/**
* Update the specified resource in storage.
* @param Request $request
* @param int $id
* @return Renderable
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
* @param int $id
* @return Renderable
*/
public function destroy($id)
{
//
}
}