api code global jain
This commit is contained in:
223
app/Repositories/Backend/Jati/JatiService.php
Normal file
223
app/Repositories/Backend/Jati/JatiService.php
Normal file
@@ -0,0 +1,223 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories\Backend\Jati;
|
||||
|
||||
use App\Constant\Constant;
|
||||
use App\Exceptions\GeneralException;
|
||||
use App\Models\Jati;
|
||||
use App\Services\BaseService;
|
||||
use Exception;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Yajra\DataTables\Facades\DataTables;
|
||||
|
||||
class JatiService extends BaseService
|
||||
{
|
||||
|
||||
/**
|
||||
* Associated Service Model.
|
||||
*/
|
||||
protected const MODEL = Jati::class;
|
||||
|
||||
/**
|
||||
* This function is use for search query in Datatable.
|
||||
* Also for sorting function we are using join query for appropriate sorting result
|
||||
* @param $request
|
||||
* @return $this|array|Builder
|
||||
*/
|
||||
public function prepareSearchQuery($request)
|
||||
{
|
||||
$searchQuery = [];
|
||||
try {
|
||||
$searchQuery = $this->query()->with('dharma')->select(
|
||||
'id',
|
||||
'name',
|
||||
'dharma_id',
|
||||
'status',
|
||||
'created_at',
|
||||
);
|
||||
|
||||
$columns = ['', 'id', 'name', 'dharma_id' ,'status', 'created_at'];
|
||||
|
||||
if (isset($request['status']) && $request['status'] != '') {
|
||||
$searchQuery = $searchQuery->where('status', $request['status']);
|
||||
}
|
||||
|
||||
if (isset($request['name']) && $request['name'] != '') {
|
||||
$searchQuery = $searchQuery->where('name', 'like', '%' . $request['name'] . '%');
|
||||
}
|
||||
|
||||
if (isset($request['order']) && $request['order'] != "") {
|
||||
$searchQuery = $searchQuery->orderBy(
|
||||
$columns[$request['order'][0]['column']],
|
||||
$request['order'][0]['dir']
|
||||
);
|
||||
}
|
||||
|
||||
} catch (Exception $ex) {
|
||||
Log::error($ex->getMessage());
|
||||
}
|
||||
return $searchQuery;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function is for the format the Datatable columns
|
||||
*
|
||||
* @param $request
|
||||
* @return object
|
||||
*/
|
||||
public function getForDataTable($request): object
|
||||
{
|
||||
$response = (object)[];
|
||||
try {
|
||||
$dataTableQuery = $this->prepareSearchQuery($request);
|
||||
// ->orderBy('id', 'DESC');
|
||||
|
||||
$response = Datatables::of($dataTableQuery)
|
||||
->addColumn(
|
||||
'checkbox_action',
|
||||
function ($jati) {
|
||||
return $jati->jati_checkbox_action;
|
||||
}
|
||||
)
|
||||
->addIndexColumn()
|
||||
->addColumn(
|
||||
'dharma_id',
|
||||
function ($jati) {
|
||||
return $jati->dharma->name ?? '-';
|
||||
}
|
||||
)
|
||||
->addColumn(
|
||||
'status',
|
||||
function ($jati) {
|
||||
return $jati->jati_status_action;
|
||||
}
|
||||
)
|
||||
->addColumn(
|
||||
'created_at',
|
||||
function ($user) {
|
||||
return (($user->created_at) ? defaultDateTimeFormat($user->created_at) : '-');
|
||||
}
|
||||
)
|
||||
->addColumn(
|
||||
'action',
|
||||
function ($jati) {
|
||||
if (
|
||||
!empty($jati->roles[0]['name']) != config('access.users.admin_role')
|
||||
|| !empty($jati->roles[0]['name']) != config('access.users.super_admin_role')
|
||||
|| loggedInUser()->isSuperAdmin()
|
||||
|| loggedInUser()->isAdmin()
|
||||
) {
|
||||
return $jati->jati_action_buttons;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
)
|
||||
->setRowId(
|
||||
function ($jati) {
|
||||
return 'recordRow-' . $jati->id;
|
||||
}
|
||||
)
|
||||
->rawColumns(['status', 'action', 'checkbox_action'])
|
||||
->make(true);
|
||||
} catch (Exception $ex) {
|
||||
Log::error($ex->getMessage());
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function is for the create jati
|
||||
*
|
||||
* @param array $request
|
||||
* @return Jati
|
||||
* @throws GeneralException
|
||||
*/
|
||||
public function create(array $request): Jati
|
||||
{
|
||||
try {
|
||||
return DB::transaction(
|
||||
function () use ($request) {
|
||||
|
||||
$jati = $this->query()->create(
|
||||
[
|
||||
'name' => $request['name'],
|
||||
'dharma_id' => $request['dharma'],
|
||||
'status' => !empty($request['status']) ? Constant::STATUS_ONE : Constant::STATUS_ZERO,
|
||||
'created_by' => !empty(loggedInUser()->id) ? loggedInUser()->id : Constant::NULL
|
||||
]
|
||||
);
|
||||
|
||||
return $jati;
|
||||
}
|
||||
);
|
||||
} catch (Exception $ex) {
|
||||
Log::error($ex->getMessage());
|
||||
throw new GeneralException(__('message.create_jati_error'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This function is for the update jati
|
||||
*
|
||||
* @param object $jati
|
||||
* @param array $request
|
||||
* @return Jati
|
||||
* @throws GeneralException
|
||||
*/
|
||||
public function update(object $jati, array $request): Jati
|
||||
{
|
||||
try {
|
||||
return DB::transaction(
|
||||
function () use ($jati, $request) {
|
||||
|
||||
$this->query()->where('id', $jati->id)->update(
|
||||
[
|
||||
'name' => !empty($request['name']) ? $request['name'] : $jati->name,
|
||||
'dharma_id' => !empty($request['dharma']) ? $request['dharma'] : $jati->dharma_id,
|
||||
'status' => isset($request['status']) ? Constant::STATUS_ONE : Constant::STATUS_ZERO,
|
||||
'updated_by' => !empty(loggedInUser()->id) ? loggedInUser()->id : $jati->updated_by,
|
||||
]
|
||||
);
|
||||
|
||||
return $jati;
|
||||
}
|
||||
);
|
||||
} catch (Exception $ex) {
|
||||
Log::error($ex->getMessage());
|
||||
throw new GeneralException(__('message.update_jati_error'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This function is for the multiple records action from the grid
|
||||
*
|
||||
* @param string $actionType
|
||||
* @param array $data
|
||||
* @return bool
|
||||
* @throws GeneralException
|
||||
*/
|
||||
public function gridActions(string $actionType, array $data): bool
|
||||
{
|
||||
try {
|
||||
switch ($actionType) {
|
||||
case Constant::STATUS_ACTIVE:
|
||||
$this->query()->whereIn('id', $data)->update(['status' => Constant::STATUS_ONE]);
|
||||
return Constant::STATUS_TRUE;
|
||||
case Constant::STATUS_INACTIVE:
|
||||
$this->query()->whereIn('id', $data)->update(['status' => Constant::STATUS_ZERO]);
|
||||
return Constant::STATUS_TRUE;
|
||||
case Constant::STATUS_DELETE:
|
||||
$this->query()->whereIn('id', $data)->delete();
|
||||
return Constant::STATUS_TRUE;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} catch (Exception $ex) {
|
||||
Log::error($ex->getMessage());
|
||||
throw new GeneralException(__('message.delete_jati_error'));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user