fix resources hospital
This commit is contained in:
@@ -45,10 +45,10 @@ class DashboardController extends Controller
|
|||||||
->active()
|
->active()
|
||||||
->hospital()
|
->hospital()
|
||||||
->when($request->lat && $request->lng, function ($query) use ($request) {
|
->when($request->lat && $request->lng, function ($query) use ($request) {
|
||||||
$query->selectRaw("organizations.id, organizations.name, addresses.text AS address, cities.name AS city_name, 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");
|
$query->selectRaw("organizations.*, addresses.text AS address, addresses.lat, addresses.lng, cities.name AS city_name, 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");
|
||||||
$query->orderBy('distance', 'ASC');
|
$query->orderBy('distance', 'ASC');
|
||||||
}, function ($query) {
|
}, function ($query) {
|
||||||
$query->select(['organizations.id', 'organizations.name', 'addresses.text AS address', 'cities.name AS city_name']);
|
$query->select(['organizations.*', 'addresses.text AS address', 'addresses.lat', 'addresses.lng', 'cities.name AS city_name']);
|
||||||
$query->orderBy('organizations.name', 'asc');
|
$query->orderBy('organizations.name', 'asc');
|
||||||
})
|
})
|
||||||
->limit($limit)
|
->limit($limit)
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ namespace Modules\Linksehat\Http\Controllers\Api;
|
|||||||
|
|
||||||
use App\Helpers\Helper;
|
use App\Helpers\Helper;
|
||||||
use App\Models\Organization;
|
use App\Models\Organization;
|
||||||
|
use App\Models\Speciality;
|
||||||
use Illuminate\Contracts\Support\Renderable;
|
use Illuminate\Contracts\Support\Renderable;
|
||||||
use Illuminate\Database\Eloquent\Builder;
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
@@ -100,23 +101,41 @@ class HospitalController extends Controller
|
|||||||
public function show(Request $request, $id)
|
public function show(Request $request, $id)
|
||||||
{
|
{
|
||||||
$queryHospitals = Organization::query()
|
$queryHospitals = Organization::query()
|
||||||
|
->with(['practitionerRoles' => function ($query) {
|
||||||
|
$query->select(['organization_id', 'speciality_id'])
|
||||||
|
->whereNot('speciality_id')
|
||||||
|
->groupBy(['speciality_id'])
|
||||||
|
->orderBy('speciality_id');
|
||||||
|
}])
|
||||||
->when($request->lat && $request->lng, function (Builder $query) use ($request) {
|
->when($request->lat && $request->lng, function (Builder $query) use ($request) {
|
||||||
$query->selectRaw("organizations*, addresses.text AS address, cities.name AS city_name, 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");
|
$query->getQuery()
|
||||||
$query->orderBy('distance', 'ASC');
|
->selectRaw("organizations.*, addresses.text AS address, addresses.lat, addresses.lng, cities.name AS city_name, 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")
|
||||||
}, function ($query) {
|
->orderBy('distance', 'ASC');
|
||||||
$query->select(['organizations.*', 'addresses.text AS address', 'cities.name AS city_name']);
|
}, function (Builder $query) {
|
||||||
$query->orderBy('organizations.name', 'asc');
|
$query->getQuery()
|
||||||
|
->select(['organizations.*', 'addresses.text AS address', 'addresses.lat', 'addresses.lng', 'cities.name AS city_name'])
|
||||||
|
->groupBy(['organizations.id'])
|
||||||
|
->orderBy('organizations.name');
|
||||||
})
|
})
|
||||||
->leftJoin('addresses', function ($q) {
|
->leftJoin('addresses', function ($query) {
|
||||||
$q->on('organizations.main_address_id', '=', 'addresses.id');
|
$query->on('organizations.main_address_id', '=', 'addresses.id');
|
||||||
$q->where('addresses.addressable_type', '=', Organization::class);
|
|
||||||
})
|
})
|
||||||
->leftJoin('cities', function ($query) {
|
->leftJoin('cities', function ($query) {
|
||||||
$query->on('addresses.city_id', '=', 'cities.id');
|
$query->on('addresses.city_id', '=', 'cities.id');
|
||||||
});
|
})
|
||||||
$queryHospitals = $queryHospitals->orderBy('organizations.name', 'asc')->findOrFail($id);
|
->where('organizations.id', $id)
|
||||||
|
->where('addresses.addressable_type', '=', Organization::class)
|
||||||
|
->findOrFail($id);
|
||||||
|
|
||||||
return Helper::responseJson(new HospitalResource($queryHospitals));
|
foreach ($queryHospitals->practitionerRoles as $practitionerRole) {
|
||||||
|
$specialitiesId[] = $practitionerRole->speciality_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
$queryHospitals['specialities'] = Speciality::query()
|
||||||
|
->whereIn('id', $specialitiesId)
|
||||||
|
->get();
|
||||||
|
|
||||||
|
return Helper::responseJson(['hospitals' => new HospitalResource($queryHospitals)]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -2,8 +2,8 @@
|
|||||||
|
|
||||||
namespace Modules\Linksehat\Transformers\Hospital;
|
namespace Modules\Linksehat\Transformers\Hospital;
|
||||||
|
|
||||||
use App\Models\PractitionerRole;
|
|
||||||
use Illuminate\Http\Resources\Json\JsonResource;
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
|
use Modules\Linksehat\Transformers\Speciality\SpecialityForHospitalDetailResource;
|
||||||
|
|
||||||
class HospitalResource extends JsonResource
|
class HospitalResource extends JsonResource
|
||||||
{
|
{
|
||||||
@@ -15,20 +15,6 @@ class HospitalResource extends JsonResource
|
|||||||
*/
|
*/
|
||||||
public function toArray($request)
|
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'] = asset('images/default-specialisasi-image.png');
|
|
||||||
}
|
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'id' => $this->id,
|
'id' => $this->id,
|
||||||
'name' => $this->name,
|
'name' => $this->name,
|
||||||
@@ -45,7 +31,10 @@ class HospitalResource extends JsonResource
|
|||||||
'photos' => [
|
'photos' => [
|
||||||
'title' => $this->name,
|
'title' => $this->name,
|
||||||
'photo_url' => url('images/default-hospital-image.png'),
|
'photo_url' => url('images/default-hospital-image.png'),
|
||||||
]
|
],
|
||||||
|
$this->mergeWhen($this->specialities, [
|
||||||
|
'specialities' => SpecialityForHospitalDetailResource::collection($this->specialities)
|
||||||
|
])
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,45 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Modules\Linksehat\Transformers\Hospital;
|
|
||||||
|
|
||||||
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'] = asset('images/default-specialisasi-image.png');
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Linksehat\Transformers\Speciality;
|
||||||
|
|
||||||
|
use App\Helpers\Helper;
|
||||||
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
|
|
||||||
|
class SpecialityForHospitalDetailResource extends JsonResource
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Transform the resource into an array.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Http\Request
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function toArray($request)
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => $this->id,
|
||||||
|
'name' => 'Spesialis ' . $this->name,
|
||||||
|
'avatar' => url('images/default-hospital-image.png'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -36,7 +36,7 @@ class SpecialityResource extends JsonResource
|
|||||||
'specialis' => 'Spesialis ' . (isset($this->speciality->name) ? $this->speciality->name : $this->speciality_name),
|
'specialis' => 'Spesialis ' . (isset($this->speciality->name) ? $this->speciality->name : $this->speciality_name),
|
||||||
'experience' => rand(5, 12),
|
'experience' => rand(5, 12),
|
||||||
'rating' => rand(20, 100),
|
'rating' => rand(20, 100),
|
||||||
'price' => Helper::currencyIdrFormat($prices) ?? null,
|
'price' => $prices ? Helper::currencyIdrFormat($prices) : null,
|
||||||
]),
|
]),
|
||||||
$this->mergeWhen($this->name, [
|
$this->mergeWhen($this->name, [
|
||||||
'title' => 'Spesialis ' . $this->name,
|
'title' => 'Spesialis ' . $this->name,
|
||||||
|
|||||||
Reference in New Issue
Block a user