Files
aso/Modules/Linksehat/Http/Controllers/Api/DashboardController.php
2022-10-31 07:09:24 +07:00

66 lines
2.9 KiB
PHP

<?php
namespace Modules\Linksehat\Http\Controllers\Api;
use App\Models\Organization;
use App\Models\PractitionerRole;
use Illuminate\Routing\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Modules\Linksehat\Transformers\Dashboard\DoctorResource as DoctorResourceDashboard;
use Modules\Linksehat\Transformers\Dashboard\HospitalResource as HospitalResourceDashboard;
use Modules\Linksehat\Transformers\Dashboard\ArticleResource as ArticleResourceDashboard;
use Symfony\Component\HttpFoundation\Response;
class DashboardController extends Controller
{
public function index(Request $request, $query, $limit = 20)
{
if ($query === 'doctors') {
$queryDoctors = PractitionerRole::query()
->with([
'person' => function ($query) {
$query->select(['name']);
},
'speciality' => function ($query) {
$query->select(['id', 'name']);
},
])
->where('active', 1)
->whereNot('speciality_id')
->get(['id', 'speciality_id'])
->random($limit);
$data = DoctorResourceDashboard::collection($queryDoctors);
} elseif ($query === 'hospitals') {
$queryHospitals = Organization::query()
->leftJoin('addresses', function ($query) {
$query->on('organizations.main_address_id', '=', 'addresses.id');
$query->where('addresses.addressable_type', '=', Organization::class);
})
->where('organizations.type', 'hospital')
->where('organizations.status', 'active')
->when($request->lat && $request->lng, function ($query) use ($request) {
$query->selectRaw("organizations.id, organizations.name, 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");
$query->orderBy('distance', 'ASC');
}, function ($query) {
$query->select(['organizations.id', 'organizations.name', 'addresses.text AS currentAddress']);
$query->orderBy('organizations.name', 'asc');
})
->limit($limit)
->get();
$data = HospitalResourceDashboard::collection($queryHospitals);
} elseif ($query === 'articles') {
$data = ArticleResourceDashboard::collection(json_decode(Http::get('https://linksehat.com/api/medical-assistance-articles')));
}
return response()->json([
'status' => 'success',
'statusCode' => Response::HTTP_OK,
'message' => 'Data berhasil di ambil',
'data' => $data,
]);
}
}