132 lines
3.4 KiB
PHP
132 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Cadastro\SujeitoAtencao;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Cadastro\SujeitoAtencao\AlergiaRequest;
|
|
use App\Models\Cadastro\SujeitoAtencao\Alergia;
|
|
use App\Models\Cadastro\SujeitoAtencao\AlergiaTipo;
|
|
use Illuminate\Http\Response;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class AlergiaController extends Controller
|
|
{
|
|
protected $modulo = 'cadastro';
|
|
protected $funcionalidade = 'alergia';
|
|
|
|
public function index()
|
|
{
|
|
$this->auth(__FUNCTION__);
|
|
|
|
$alergias = $this->get(Alergia::class)->orderBy('agente_substancia_especifica')->get();
|
|
|
|
return response()->json($alergias, Response::HTTP_OK);
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$this->auth(__FUNCTION__);
|
|
|
|
return response()->json(
|
|
[
|
|
'form' => $this->getFormData(),
|
|
],
|
|
Response::HTTP_OK
|
|
);
|
|
}
|
|
|
|
public function store(AlergiaRequest $request)
|
|
{
|
|
$this->auth(__FUNCTION__);
|
|
|
|
$alergia = Alergia::create($this->prepareData($request->all()));
|
|
|
|
return response()->json($alergia->fresh(), Response::HTTP_CREATED);
|
|
}
|
|
|
|
public function show($id)
|
|
{
|
|
$this->auth(__FUNCTION__);
|
|
|
|
return response()->json($this->get(Alergia::class, $id), Response::HTTP_OK);
|
|
}
|
|
|
|
public function edit($id)
|
|
{
|
|
$this->auth(__FUNCTION__);
|
|
|
|
$alergia = Alergia::findOrFail($id);
|
|
|
|
return response()->json(
|
|
[
|
|
'form' => $this->getFormData(),
|
|
'model' => [
|
|
'tipo_id' => $alergia->tipo_id,
|
|
'agente_substancia_especifica' => $alergia->agente_substancia_especifica,
|
|
],
|
|
],
|
|
Response::HTTP_OK
|
|
);
|
|
}
|
|
|
|
private function getFormData()
|
|
{
|
|
return [
|
|
'tipos' => AlergiaTipo::orderBy('descricao')->get(),
|
|
];
|
|
}
|
|
|
|
public function update(AlergiaRequest $request, $id)
|
|
{
|
|
$this->auth(__FUNCTION__);
|
|
|
|
$alergia = tap(Alergia::findOrFail($id))->update($this->prepareData($request->all()));
|
|
|
|
return response()->json($alergia->fresh(), Response::HTTP_OK);
|
|
}
|
|
|
|
public function prepareData($params)
|
|
{
|
|
return [
|
|
'tipo_id' => $params['tipo_id'],
|
|
'agente_substancia_especifica' => mb_strtoupper($params['agente_substancia_especifica']),
|
|
];
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
$this->auth(__FUNCTION__);
|
|
|
|
Alergia::findOrFail($id)->delete();
|
|
|
|
return response()->json(null, Response::HTTP_NO_CONTENT);
|
|
}
|
|
|
|
public function datatable()
|
|
{
|
|
$status = DB::table('cadastro_alergia')
|
|
->join('cadastro_alergia_tipo', 'cadastro_alergia_tipo.id', 'cadastro_alergia.tipo_id')
|
|
->select(
|
|
'cadastro_alergia.id',
|
|
'cadastro_alergia.agente_substancia_especifica',
|
|
'cadastro_alergia.tipo_id',
|
|
'cadastro_alergia_tipo.descricao AS tipo_descricao',
|
|
)
|
|
->whereNull('cadastro_alergia.deleted_at');
|
|
|
|
return datatables()->of($status)->skipTotalRecords()->make(true);
|
|
}
|
|
|
|
public function autocomplete()
|
|
{
|
|
$alergias = Alergia::with('tipo');
|
|
|
|
if (is_numeric(request()->tipo_id)) {
|
|
|
|
$alergias->where('tipo_id', request()->tipo_id);
|
|
}
|
|
|
|
return datatables()->of($alergias)->make(true);
|
|
}
|
|
}
|