-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
56 changed files
with
1,843 additions
and
212 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...rio_pedagogico/backend/application/controllers/autentication/AutenticationController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.obervatorio_pedagogico.backend.application.controllers.autentication; | ||
|
||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.CrossOrigin; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import com.obervatorio_pedagogico.backend.application.services.autenticacao.AutenticacaoService; | ||
import com.obervatorio_pedagogico.backend.domain.model.usuario.Usuario; | ||
import com.obervatorio_pedagogico.backend.infrastructure.utils.httpResponse.ResponseService; | ||
import com.obervatorio_pedagogico.backend.presentation.dto.auth.AuthResponse; | ||
import com.obervatorio_pedagogico.backend.presentation.dto.auth.LoginRequest; | ||
import com.obervatorio_pedagogico.backend.presentation.dto.usuario.UsuarioDto; | ||
import com.obervatorio_pedagogico.backend.presentation.shared.Response; | ||
|
||
import lombok.AllArgsConstructor; | ||
|
||
@RestController | ||
@RequestMapping("/observatorio-pedagogico/api/login") | ||
@AllArgsConstructor | ||
@CrossOrigin | ||
public class AutenticationController { | ||
|
||
private AutenticacaoService autenticacaoService; | ||
|
||
private ResponseService responseService; | ||
|
||
@PostMapping | ||
public ResponseEntity<Response<AuthResponse>> login(@RequestBody LoginRequest authRequest) { | ||
AuthResponse authResponse = autenticacaoService.login(authRequest); | ||
|
||
return responseService.ok(authResponse); | ||
} | ||
|
||
@PostMapping("/cadastrar") | ||
public ResponseEntity<Response<Usuario>> cadastrar(@RequestBody UsuarioDto usuarioDto) { | ||
Usuario usuario = autenticacaoService.cadastrar(usuarioDto); | ||
|
||
return responseService.ok(usuario); | ||
} | ||
} |
92 changes: 85 additions & 7 deletions
92
...m/obervatorio_pedagogico/backend/application/controllers/extracao/ExtracaoController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,112 @@ | ||
package com.obervatorio_pedagogico.backend.application.controllers.extracao; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.CrossOrigin; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import com.obervatorio_pedagogico.backend.application.services.extracao.ExtracaoService; | ||
import com.obervatorio_pedagogico.backend.application.services.extracao.ExtracaoThreadService; | ||
import com.obervatorio_pedagogico.backend.domain.model.extracao.Extracao; | ||
import com.obervatorio_pedagogico.backend.domain.model.extracao.ExtracaoThread; | ||
import com.obervatorio_pedagogico.backend.domain.model.extracao.Extracao.Status; | ||
import com.obervatorio_pedagogico.backend.infrastructure.utils.httpResponse.ResponseService; | ||
import com.obervatorio_pedagogico.backend.infrastructure.utils.modelMapper.ModelMapperService; | ||
import com.obervatorio_pedagogico.backend.presentation.dto.extracao.ExtracaoRequest; | ||
import com.obervatorio_pedagogico.backend.presentation.dto.extracao.ExtracaoResponse; | ||
import com.obervatorio_pedagogico.backend.presentation.dto.extracao.ExtracaoResponseResumido; | ||
import com.obervatorio_pedagogico.backend.presentation.dto.extracao.ExtracaoThreadResponse; | ||
import com.obervatorio_pedagogico.backend.presentation.shared.Response; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import lombok.AllArgsConstructor; | ||
|
||
@RestController | ||
@RequestMapping("/observatorio-pedagogico/api/extracao") | ||
@AllArgsConstructor | ||
@CrossOrigin | ||
public class ExtracaoController { | ||
|
||
private ExtracaoService extracaoService; | ||
|
||
private ExtracaoThreadService extracaoThreadService; | ||
|
||
private ModelMapperService modelMapperService; | ||
|
||
private ResponseService responseService; | ||
|
||
@PostMapping("/enviar") | ||
public ResponseEntity<Response<ExtracaoResponse>> enviar(ExtracaoRequest extracaoRequest) { | ||
Extracao extracaoModel = extracaoService.cadastrar(extracaoRequest); | ||
ExtracaoResponse extracaoResponse = modelMapperService.convert(extracaoModel, ExtracaoResponse.class); | ||
return responseService.create(extracaoResponse); | ||
extracaoService.adicionarNaFila(extracaoRequest); | ||
return responseService.create(null); | ||
} | ||
|
||
@GetMapping("/envio-status/{id}") | ||
public ResponseEntity<Response<ExtracaoThreadResponse>> getEnvioStatus(@PathVariable Long id){ | ||
ExtracaoThread extracaoThread = extracaoThreadService.getById(id); | ||
|
||
ExtracaoThreadResponse response = modelMapperService.convert(extracaoThread, ExtracaoThreadResponse.class); | ||
|
||
return responseService.ok(response); | ||
} | ||
|
||
@GetMapping("/envio-status") | ||
public ResponseEntity<Response<List<ExtracaoThreadResponse>>> getEnvioStatus(){ | ||
List<ExtracaoThread> extracaoThreadList = extracaoThreadService.getAll(); | ||
|
||
List<ExtracaoThreadResponse> responseList = new ArrayList<>(); | ||
extracaoThreadList.stream().forEach(thread -> responseList.add(modelMapperService.convert(thread, ExtracaoThreadResponse.class))); | ||
|
||
return responseService.ok(responseList); | ||
} | ||
|
||
@GetMapping() | ||
public ResponseEntity<Response<List<ExtracaoResponseResumido>>> getTodos(){ | ||
List<Extracao> extracoes = extracaoService.getTodos(); | ||
|
||
return responseService.ok(extracoes.stream() | ||
.map(element -> modelMapperService | ||
.convert(element, ExtracaoResponseResumido.class)) | ||
.collect(Collectors.toList())); | ||
} | ||
|
||
@GetMapping("/get-by-id/{id}") | ||
public ResponseEntity<Response<ExtracaoResponseResumido>> getById(@PathVariable Long id){ | ||
Extracao extracao = extracaoService.getById(id); | ||
|
||
return responseService.ok(modelMapperService.convert(extracao, ExtracaoResponseResumido.class)); | ||
} | ||
|
||
@GetMapping("/get-by-status") | ||
public ResponseEntity<Response<List<ExtracaoResponseResumido>>> getByStatus(Status status){ | ||
List<Extracao> extracoes = extracaoService.getByStatus(status); | ||
|
||
return responseService.ok(extracoes.stream() | ||
.map(element -> modelMapperService | ||
.convert(element, ExtracaoResponseResumido.class)) | ||
.collect(Collectors.toList())); | ||
} | ||
|
||
@GetMapping("/get-by-periodo-letivo") | ||
public ResponseEntity<Response<List<ExtracaoResponseResumido>>> getByPeriodoLetivo(String periodoLetivo) { | ||
List<Extracao> extracoes = extracaoService.getByPeriodoLetivo(periodoLetivo); | ||
|
||
return responseService.ok(extracoes.stream() | ||
.map(element -> modelMapperService | ||
.convert(element, ExtracaoResponseResumido.class)) | ||
.collect(Collectors.toList())); | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
public ResponseEntity<Response<String>> deletaExtracao(@PathVariable Long id){ | ||
extracaoService.deletaExtracao(id); | ||
return responseService.ok("deletado-com-sucesso"); | ||
} | ||
} |
135 changes: 135 additions & 0 deletions
135
...obervatorio_pedagogico/backend/application/services/autenticacao/AutenticacaoService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package com.obervatorio_pedagogico.backend.application.services.autenticacao; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.Optional; | ||
|
||
import org.springframework.security.authentication.AuthenticationManager; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.stereotype.Service; | ||
|
||
import com.obervatorio_pedagogico.backend.application.services.usuario.FuncionarioCopedService; | ||
import com.obervatorio_pedagogico.backend.application.services.usuario.ProfessorService; | ||
import com.obervatorio_pedagogico.backend.application.services.usuario.UsuarioService; | ||
import com.obervatorio_pedagogico.backend.application.services.utils.EmailService; | ||
import com.obervatorio_pedagogico.backend.domain.exceptions.LoginInvalidoException; | ||
import com.obervatorio_pedagogico.backend.domain.exceptions.UsuarioNaoPermitidoException; | ||
import com.obervatorio_pedagogico.backend.domain.model.usuario.FuncionarioCoped; | ||
import com.obervatorio_pedagogico.backend.domain.model.usuario.Professor; | ||
import com.obervatorio_pedagogico.backend.domain.model.usuario.Usuario; | ||
import com.obervatorio_pedagogico.backend.infrastructure.security.auth.JwtUtils; | ||
import com.obervatorio_pedagogico.backend.infrastructure.security.auth.SharUtils; | ||
import com.obervatorio_pedagogico.backend.infrastructure.security.service.SecurityService; | ||
import com.obervatorio_pedagogico.backend.infrastructure.utils.modelMapper.ModelMapperService; | ||
import com.obervatorio_pedagogico.backend.presentation.dto.auth.AuthResponse; | ||
import com.obervatorio_pedagogico.backend.presentation.dto.auth.LoginRequest; | ||
import com.obervatorio_pedagogico.backend.presentation.dto.usuario.UsuarioDto; | ||
import com.obervatorio_pedagogico.backend.presentation.dto.usuario.UsuarioDto.Tipo; | ||
|
||
import lombok.AllArgsConstructor; | ||
|
||
@Service | ||
@AllArgsConstructor | ||
public class AutenticacaoService { | ||
|
||
private EmailService emailService; | ||
|
||
private SecurityService securityService; | ||
|
||
private FuncionarioCopedService funcionarioCopedService; | ||
|
||
private ProfessorService professorService; | ||
|
||
private UsuarioService usuarioService; | ||
|
||
private AuthenticationManager authenticationManager; | ||
|
||
private SharUtils sharUtils; | ||
|
||
private JwtUtils jwtUtils; | ||
|
||
private ModelMapperService modelMapperService; | ||
|
||
public AuthResponse login(LoginRequest authRequest) { | ||
|
||
if (!emailService.isDominioValido(authRequest.getEmail())) { | ||
throw new RuntimeException("Dominio inválido"); | ||
} | ||
|
||
String senhaShar = null; | ||
try { | ||
senhaShar = sharUtils.shar256(authRequest.getSenha()); | ||
} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
try { | ||
authenticationManager.authenticate( | ||
new UsernamePasswordAuthenticationToken(authRequest.getEmail(), senhaShar)); | ||
} catch (AuthenticationException authenticationException) { | ||
throw new LoginInvalidoException(); | ||
} | ||
|
||
Optional<Usuario> usuarioOp = usuarioService.buscarUsuarioByEmail(authRequest.getEmail()); | ||
|
||
if (usuarioOp.isPresent() && !usuarioOp.get().isPermitido()) { | ||
throw new UsuarioNaoPermitidoException(); | ||
} | ||
|
||
UserDetails loadedUser = securityService.loadUserByUsername(authRequest.getEmail()); | ||
|
||
String token = jwtUtils.generateToken(loadedUser); | ||
|
||
return new AuthResponse(token); | ||
} | ||
|
||
public Usuario cadastrar(UsuarioDto usuarioDto) { | ||
this.validarCadastro(usuarioDto); | ||
|
||
boolean isPrimeiro = !usuarioService.existeUsuarioCadastrado(); | ||
|
||
String senha = null; | ||
try { | ||
senha = sharUtils.shar256(usuarioDto.getSenha()); | ||
} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
if (usuarioDto.getTipo().equals(Tipo.COPED)) { | ||
FuncionarioCoped funcionarioCoped = modelMapperService.convert(usuarioDto, FuncionarioCoped.class); | ||
funcionarioCoped.setSenha(senha); | ||
|
||
if (isPrimeiro) { | ||
funcionarioCoped.setPermitido(true); | ||
funcionarioCoped.setEsperaCadastro(false); | ||
} | ||
|
||
return funcionarioCopedService.salvar(funcionarioCoped); | ||
} else { | ||
Professor professor = modelMapperService.convert(usuarioDto, Professor.class); | ||
professor.setSenha(senha); | ||
|
||
if (isPrimeiro) { | ||
professor.setPermitido(true); | ||
professor.setEsperaCadastro(false); | ||
} | ||
|
||
return professorService.salvar(professor); | ||
} | ||
} | ||
|
||
private void validarCadastro(UsuarioDto usuarioDto) { | ||
if (!emailService.isDominioValido(usuarioDto.getEmail())) { | ||
throw new RuntimeException("Dominio inválido"); | ||
} | ||
|
||
if ( | ||
professorService.buscarPorEmail(usuarioDto.getEmail()).isPresent() || | ||
funcionarioCopedService.buscarPorEmail(usuarioDto.getEmail()).isPresent() | ||
) { | ||
throw new RuntimeException("Já existe"); | ||
} | ||
} | ||
} |
Oops, something went wrong.