Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. namespace AppServices;
  2.  
  3. use AppEncarregado;
  4. use IlluminateSupportFacadesDB;
  5. use Exception;
  6.  
  7. class RegistradorDeEncarregado
  8. {
  9.  
  10. public function desabilitarEncarregado(int $id): Encarregado
  11. {
  12. DB::beginTransaction();
  13. $result = Encarregado::where('id', $id)
  14. ->update([
  15. 'ativo' => 0]);
  16. DB::commit();
  17.  
  18. return $this->verificarUpdate($result, $id);
  19. }
  20.  
  21. private function verificarUpdate(bool $result, $id){
  22. $encarregado = Encarregado::find($id);
  23.  
  24. // Cenário perfeito
  25. if($result && isset($encarregado)){
  26. return $encarregado;
  27.  
  28. // Quando o id que vai sofrer a alteração já possui
  29. // o mesmo valor do parâmetro
  30. }else if(!$result && isset($encarregado)){
  31. throw new Exception('Registro já se encontra com o(s) parâmetro(s) de alteração.');
  32.  
  33. // Id não existente
  34. }else{
  35. throw new Exception('Não foi possível alterar o registro do encarregado.');
  36. }
  37. }
  38. }
  39.  
  40. public function disability(
  41. Request $request,
  42. RegistradorDeEncarregado $registradorDeEncarregado)
  43. {
  44. $encarregado = $registradorDeEncarregado->desabilitarEncarregado($request->id);
  45. $request->session()
  46. ->flash(
  47. 'mensagem',
  48. "Encarregado {$encarregado->nome} salvo com sucesso."
  49. );
  50.  
  51. return redirect()->route('encarregado_listar_ativos');
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement