api show hospital

This commit is contained in:
Muhammad Fajar
2022-10-28 09:44:15 +07:00
parent b477040fc1
commit 302cc343fd
4 changed files with 74 additions and 14 deletions

View File

@@ -105,7 +105,7 @@ class DoctorController extends Controller
$query->select(['availability_id', 'day']);
}])->where('practitioner_role_id', $id)->get(['id', 'start_time']);
// return $queryDoctor;
return $queryDoctor->is_chat_available;
$doctor = DoctorResourceDetail::make($queryDoctor);
$doctor['day_available'] = Helper::dailyAvailabilities($queryAvailables);
// return $doctor;

View File

@@ -9,6 +9,7 @@ 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
{
@@ -19,13 +20,13 @@ class HospitalController extends Controller
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.'%');
});
->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) {
$hospitals->leftJoin('addresses', function ($q) {
$q->on('organizations.main_address_id', '=', 'addresses.id');
$q->where('addresses.addressable_type', '=', Organization::class);
});
@@ -34,7 +35,7 @@ class HospitalController extends Controller
// 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');
@@ -62,10 +63,10 @@ class HospitalController extends Controller
if ($limit > 20) {
$limit = 20;
}
$hospitals = $hospitals->paginate($limit);
// dd($hospitals->toArray());
return response()->json([
'message' => 'Sukses mengambil data Rumah Sakit',
@@ -97,11 +98,19 @@ class HospitalController extends Controller
* @param int $id
* @return Renderable
*/
public function show($id)
public function show(Request $request, $id)
{
$hospital = Organization::query()->findOrFail($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 response()->json(HospitalResource::make($hospital));
return HospitalResourceDetail::make($queryHospitals);
}
/**

View File

@@ -28,7 +28,7 @@ class HospitalResource extends JsonResource
'distance' => $this->distance ? ($this->distance < 1 ? round($this->distance * 1000, 2) . " m" : round($this->distance, 2) . " km") : null,
'photos' => [
'title' => Str::slug($this->name),
'url' => asset('images/default-doctor-avatar.png')
'url' => asset('images/default-hospital-image.png'),
]
];
}

View File

@@ -0,0 +1,51 @@
<?php
namespace Modules\Linksehat\Transformers\Hospitals;
use App\Models\PractitionerRole;
use Illuminate\Http\Resources\Json\JsonResource;
use Str;
class HospitalResourceDetail extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request)
{
$querySpecialitys = PractitionerRole::query()
->with('speciality')
->where('organization_id', $this->id)
->whereNotNull('speciality_id')
->orderBy('speciality_id')
->groupBy('speciality_id')
->get(['speciality_id']);
foreach ($querySpecialitys as $indexSpeciality => $speciality) {
$specialitys[$indexSpeciality]['id'] = $speciality->speciality->id;
$specialitys[$indexSpeciality]['name'] = $speciality->speciality->name;
$specialitys[$indexSpeciality]['avatar'] = null;
}
if (empty($this->distance)) {
$address = $this->currentAddress->text;
} else {
$address = $this->currentAddress;
}
return [
'id' => $this->id,
'name' => $this->name,
'address' => $address ?? null,
'distance' => $this->distance ? ($this->distance < 1 ? round($this->distance * 1000, 2) . " m" : round($this->distance, 2) . " km") : null,
'photos' => [
'title' => Str::slug($this->name),
'url' => asset('images/default-hospital-image.png'),
],
'specialitys' => $specialitys
];
}
}