api code global jain
This commit is contained in:
148
app/Repositories/Backend/Hospital/HospitalService.php
Normal file
148
app/Repositories/Backend/Hospital/HospitalService.php
Normal file
@@ -0,0 +1,148 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories\Backend\Hospital;
|
||||
|
||||
use App\Constant\Constant;
|
||||
use App\Models\Hospital;
|
||||
use App\Services\BaseService;
|
||||
use Carbon\Carbon;
|
||||
use Exception;
|
||||
use Yajra\DataTables\Facades\DataTables;
|
||||
|
||||
class HospitalService extends BaseService
|
||||
{
|
||||
/**
|
||||
* Associated Service Model.
|
||||
*/
|
||||
protected const MODEL = Hospital::class;
|
||||
|
||||
/**
|
||||
* @param $request
|
||||
* @return array
|
||||
*/
|
||||
public function storeHospital($request): array
|
||||
{
|
||||
$response = [];
|
||||
$photo = '';
|
||||
|
||||
if (!empty($request['photo'])) {
|
||||
$imageName = uploadImage($request, 'photo', Constant::HOSPITAL_IMAGE_UPLOAD_PATH);
|
||||
$photo = $imageName['image_name'] ?? Constant::NULL;
|
||||
}
|
||||
|
||||
$result = $this->query()->create([
|
||||
'name' => $request['name'],
|
||||
'contact_name' => $request['contact_name'],
|
||||
'email' => $request['email'],
|
||||
'alternative_email' => $request['alter_email'],
|
||||
'number' => $request['number'],
|
||||
'address' => $request['address'],
|
||||
'area' => $request['area'],
|
||||
'city' => $request['city'],
|
||||
'state' => $request['state'],
|
||||
'photo' => $photo,
|
||||
'benefits' => $request['benefits'],
|
||||
]);
|
||||
|
||||
$response['status'] = ($result) ? Constant::STATUS_TRUE : Constant::STATUS_FALSE;
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $request
|
||||
* @return object
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getListing($request): object
|
||||
{
|
||||
$dataTableQuery = $this->prepareQuery($request->all());
|
||||
|
||||
return DataTables::of($dataTableQuery)
|
||||
->addColumn(
|
||||
'checkbox_action',
|
||||
function ($hospital) {
|
||||
return $hospital?->hospital_checkbox_action;
|
||||
}
|
||||
)
|
||||
->addColumn(
|
||||
'hospital_status',
|
||||
function ($hospital) {
|
||||
return $hospital?->hospital_status_action;
|
||||
}
|
||||
)
|
||||
->editColumn(
|
||||
'created_at',
|
||||
function ($hospital) {
|
||||
return Carbon::parse($hospital?->created_at)->format('d-m-Y');
|
||||
}
|
||||
)
|
||||
->addColumn(
|
||||
'action',
|
||||
function ($hospital) {
|
||||
return $hospital?->hospital_action_buttons;
|
||||
}
|
||||
)
|
||||
->addIndexColumn()
|
||||
->rawColumns(['action', 'checkbox_action', 'hospital_status'])
|
||||
->make(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $request
|
||||
* @return object
|
||||
*/
|
||||
public function prepareQuery($request): object
|
||||
{
|
||||
$searchQuery = $this->query();
|
||||
|
||||
$columns = ['', 'name', 'contact_name', 'email', 'city', 'state','state', 'created_at'];
|
||||
|
||||
$searchQuery = $searchQuery->when(array_key_exists('name', $request), function ($q) use ($request) {
|
||||
return $q->where('name', 'like', '%' . $request['name'] . '%');
|
||||
});
|
||||
|
||||
if (isset($request['order']) && $request['order'] != "") {
|
||||
$searchQuery = $searchQuery->orderBy(
|
||||
$columns[$request['order'][0]['column']],
|
||||
$request['order'][0]['dir']
|
||||
);
|
||||
}
|
||||
|
||||
return $searchQuery;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $request
|
||||
* @param $id
|
||||
* @return array
|
||||
*/
|
||||
public function updateHospital($request, $id): array
|
||||
{
|
||||
$response = [];
|
||||
$hospital = $this->query()->where('id', $id)->first();
|
||||
$photo = $hospital?->photo;
|
||||
|
||||
if (!empty($request['photo'])) {
|
||||
$imageName = uploadImage($request, 'photo', Constant::HOSPITAL_IMAGE_UPLOAD_PATH);
|
||||
$photo = $imageName['image_name'] ?? Constant::NULL;
|
||||
}
|
||||
$result = $hospital->update([
|
||||
'name' => $request['name'],
|
||||
'contact_name' => $request['contact_name'],
|
||||
'email' => $request['email'],
|
||||
'alternative_email' => $request['alter_email'],
|
||||
'number' => $request['number'],
|
||||
'address' => $request['address'],
|
||||
'area' => $request['area'],
|
||||
'city' => $request['city'],
|
||||
'state' => $request['state'],
|
||||
'photo' => $photo,
|
||||
'benefits' => $request['benefits'],
|
||||
]);
|
||||
|
||||
$response['status'] = ($result) ? Constant::STATUS_TRUE : Constant::STATUS_FALSE;
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user