This commit is contained in:
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Cadastro\Estabelecimento;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Cadastro\Estabelecimento\SalaRequest;
|
||||
use App\Models\Cadastro\Estabelecimento\Sala;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class SalaController extends Controller
|
||||
{
|
||||
protected $modulo = 'cadastro';
|
||||
protected $funcionalidade = 'sala';
|
||||
|
||||
public function datatable()
|
||||
{
|
||||
$salas = DB::table('cadastro_sala AS sala')
|
||||
->join('cadastro_centro_custo AS centro_custo', 'centro_custo.id', 'sala.centro_custo_id')
|
||||
->join('cadastro_setor AS setor', 'setor.id', 'centro_custo.setor_id')
|
||||
->select([
|
||||
'sala.id',
|
||||
'sala.nome',
|
||||
'setor.nome as setor_nome'
|
||||
])
|
||||
->whereNull('sala.deleted_at')
|
||||
->where('centro_custo.infraestrutura_id', $this->getCurrentLotacao()->infraestrutura_id)
|
||||
->orderBy('setor.nome')
|
||||
->orderBy('sala.nome')
|
||||
->get();
|
||||
|
||||
return response()->json($salas, Response::HTTP_OK);
|
||||
}
|
||||
|
||||
public function index()
|
||||
{
|
||||
$this->auth(__FUNCTION__);
|
||||
|
||||
$salas = $this->get(Sala::class)->orderBy('nome')->get();
|
||||
|
||||
return response()->json($salas, Response::HTTP_OK);
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
$this->auth(__FUNCTION__);
|
||||
|
||||
$sala = new Sala;
|
||||
$sala->fill(array_fill_keys($sala->getFillable(), null));
|
||||
|
||||
return response()->json(
|
||||
[
|
||||
'form' => $this->getFormData(),
|
||||
'model' => $sala->makeHidden('id')
|
||||
],
|
||||
Response::HTTP_OK
|
||||
);
|
||||
}
|
||||
|
||||
public function store(SalaRequest $request)
|
||||
{
|
||||
$this->auth(__FUNCTION__);
|
||||
|
||||
$sala = Sala::create($this->getParams($request))->fresh();
|
||||
|
||||
return response()->json($sala, Response::HTTP_CREATED);
|
||||
}
|
||||
|
||||
public function show($id)
|
||||
{
|
||||
$this->auth(__FUNCTION__);
|
||||
|
||||
return response()->json($this->get(Sala::class, $id), Response::HTTP_OK);
|
||||
}
|
||||
|
||||
public function edit($id)
|
||||
{
|
||||
$this->auth(__FUNCTION__);
|
||||
|
||||
$sala = Sala::findOrFail($id);
|
||||
|
||||
return response()->json(
|
||||
[
|
||||
'form' => $this->getFormData(),
|
||||
'model' => $sala->makeHidden('id'),
|
||||
],
|
||||
Response::HTTP_OK
|
||||
);
|
||||
}
|
||||
|
||||
private function getFormData()
|
||||
{
|
||||
$lotacao = $this->getCurrentLotacao();
|
||||
|
||||
return [
|
||||
'unidade' => $lotacao->infraestrutura->unidade->nome_fantasia,
|
||||
'infraestrutura' => $lotacao->infraestrutura->nome,
|
||||
'centros_custo' => $lotacao->infraestrutura->centrosCusto
|
||||
->map(fn ($centroCusto) => [
|
||||
'id' => $centroCusto->id,
|
||||
'setor' => $centroCusto->setor->nome
|
||||
])
|
||||
];
|
||||
}
|
||||
|
||||
public function update(SalaRequest $request, $id)
|
||||
{
|
||||
$this->auth(__FUNCTION__);
|
||||
|
||||
$sala = tap(Sala::findOrFail($id))->update($this->getParams($request));
|
||||
|
||||
return response()->json($sala->fresh(), Response::HTTP_OK);
|
||||
}
|
||||
|
||||
private function getParams($request)
|
||||
{
|
||||
$params = $request->all();
|
||||
|
||||
$params['nome'] = Str::upper($params['nome']);
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
public function destroy($id)
|
||||
{
|
||||
$this->auth(__FUNCTION__);
|
||||
|
||||
Sala::findOrFail($id)->delete();
|
||||
|
||||
return response()->json(null, Response::HTTP_NO_CONTENT);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user