File manager - Edit - /var/www/html/portal/app/Http/Controllers/IndicadoresTi/GerenciarAssuntosController.php
Back
<?php namespace App\Http\Controllers\IndicadoresTi; use App\Http\Controllers\Controller; use App\Models\IndicadoresTi\Assunto; use App\Models\IndicadoresTi\Setor; use App\Models\IndicadoresTi\Subassunto; use App\Http\Services\IndicadoresTi\GerenciarAssuntosService; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\View\View; class GerenciarAssuntosController extends Controller { public function __construct(private readonly GerenciarAssuntosService $service) {} public function index(): View { $this->autorizarDiretor(); return view('admin.indicadoresTi.gerenciar', [ 'headLine' => 'Gerenciar Assuntos e Subassuntos', 'setores' => $this->service->getSetoresComAssuntos(), ]); } public function storeAssunto(Request $request): RedirectResponse { $this->autorizarDiretor(); $dados = $request->validate([ 'nome' => ['required', 'string', 'max:255'], 'setor_id' => ['required', 'exists:setores_ti,id'], ], [ 'nome.required' => 'Informe o nome do assunto.', 'setor_id.required' => 'Selecione o setor.', 'setor_id.exists' => 'Setor inválido.', ]); if ($this->service->assuntoJaExiste($dados['setor_id'], $dados['nome'])) { return back()->withErrors(['nome_assunto' => 'Já existe um assunto com esse nome neste setor.'])->withInput(); } $this->service->criarAssunto($dados); return back()->with('success', 'Assunto criado com sucesso.'); } public function updateAssunto(Request $request, Assunto $assunto): RedirectResponse { $this->autorizarDiretor(); $dados = $request->validate([ 'nome' => ['required', 'string', 'max:255'], ], [ 'nome.required' => 'Informe o nome do assunto.', ]); if ($this->service->assuntoJaExiste($assunto->setor_id, $dados['nome'], $assunto->id)) { return back()->withErrors(['nome_assunto_' . $assunto->id => 'Já existe um assunto com esse nome neste setor.'])->withInput(); } $this->service->atualizarAssunto($assunto, $dados); return back()->with('success', 'Assunto atualizado com sucesso.'); } public function destroyAssunto(Assunto $assunto): RedirectResponse { $this->autorizarDiretor(); if (!$this->service->excluirAssunto($assunto)) { return back()->withErrors(['assunto' => 'Remova todos os subassuntos antes de excluir este assunto.']); } return back()->with('success', 'Assunto excluído com sucesso.'); } public function storeSubassunto(Request $request): RedirectResponse { $this->autorizarDiretor(); $dados = $request->validate([ 'nome' => ['required', 'string', 'max:255'], 'assunto_id' => ['required', 'exists:assuntos_ti,id'], ], [ 'nome.required' => 'Informe o nome do subassunto.', 'assunto_id.required' => 'Selecione o assunto.', 'assunto_id.exists' => 'Assunto inválido.', ]); if ($this->service->subassuntoJaExiste($dados['assunto_id'], $dados['nome'])) { return back()->withErrors(['nome_subassunto' => 'Já existe um subassunto com esse nome neste assunto.'])->withInput(); } $this->service->criarSubassunto($dados); return back()->with('success', 'Subassunto criado com sucesso.'); } public function updateSubassunto(Request $request, Subassunto $subassunto): RedirectResponse { $this->autorizarDiretor(); $dados = $request->validate([ 'nome' => ['required', 'string', 'max:255'], ], [ 'nome.required' => 'Informe o nome do subassunto.', ]); if ($this->service->subassuntoJaExiste($subassunto->assunto_id, $dados['nome'], $subassunto->id)) { return back()->withErrors(['nome_subassunto_' . $subassunto->id => 'Já existe um subassunto com esse nome neste assunto.'])->withInput(); } $this->service->atualizarSubassunto($subassunto, $dados); return back()->with('success', 'Subassunto atualizado com sucesso.'); } public function destroySubassunto(Subassunto $subassunto): RedirectResponse { $this->autorizarDiretor(); $this->service->excluirSubassunto($subassunto); return back()->with('success', 'Subassunto excluído com sucesso.'); } public function storeSetor(Request $request): RedirectResponse { $this->autorizarDiretor(); $dados = $request->validate( ['nome' => ['required', 'string', 'max:255']], ['nome.required' => 'Informe o nome do setor.'] ); if ($this->service->setorJaExiste($dados['nome'])) { return back()->withErrors(['nome_setor' => 'Já existe um setor com esse nome.'])->withInput(); } $this->service->criarSetor($dados); return back()->with('success', 'Setor criado com sucesso.'); } public function updateSetor(Request $request, Setor $setor): RedirectResponse { $this->autorizarDiretor(); $dados = $request->validate( ['nome' => ['required', 'string', 'max:255']], ['nome.required' => 'Informe o nome do setor.'] ); if ($this->service->setorJaExiste($dados['nome'], $setor->id)) { return back()->withErrors(['nome_setor_' . $setor->id => 'Já existe um setor com esse nome.'])->withInput(); } $this->service->atualizarSetor($setor, $dados); return back()->with('success', 'Setor atualizado com sucesso.'); } public function destroySetor(Setor $setor): RedirectResponse { $this->autorizarDiretor(); if (!$this->service->excluirSetor($setor)) { return back()->withErrors(['setor' => 'Remova todos os assuntos antes de excluir este setor.']); } return back()->with('success', 'Setor excluído com sucesso.'); } private function autorizarDiretor(): void { $permissoes = Auth::user()->permissoes->pluck('permissao'); if (!$permissoes->contains('DIRETOR_TI') && !$permissoes->contains('SYSADMIN')) { abort(403); } } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.28 | Generation time: 0.02 |
proxy
|
phpinfo
|
Settings