| 1 | package com.edtech.controller; | |
| 2 | ||
| 3 | import com.edtech.model.User; | |
| 4 | import com.edtech.model.UserRole; | |
| 5 | import com.edtech.repository.UserRepository; | |
| 6 | import com.edtech.service.LaboratoryTokenService; | |
| 7 | import java.util.Map; | |
| 8 | import java.util.Optional; | |
| 9 | import org.springframework.http.ResponseEntity; | |
| 10 | import org.springframework.security.core.annotation.AuthenticationPrincipal; | |
| 11 | import org.springframework.security.core.userdetails.UserDetails; | |
| 12 | import org.springframework.web.bind.annotation.GetMapping; | |
| 13 | import org.springframework.web.bind.annotation.PostMapping; | |
| 14 | import org.springframework.web.bind.annotation.RequestBody; | |
| 15 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 16 | import org.springframework.web.bind.annotation.RestController; | |
| 17 | ||
| 18 | /** Javadoc. */ | |
| 19 | @RestController | |
| 20 | @RequestMapping("/api/v1/laboratory") | |
| 21 | public class LaboratoryController { | |
| 22 | ||
| 23 | private final LaboratoryTokenService laboratoryTokenService; | |
| 24 | private final UserRepository userRepository; | |
| 25 | ||
| 26 | public LaboratoryController( | |
| 27 | LaboratoryTokenService laboratoryTokenService, UserRepository userRepository) { | |
| 28 | this.laboratoryTokenService = laboratoryTokenService; | |
| 29 | this.userRepository = userRepository; | |
| 30 | } | |
| 31 | ||
| 32 | /** Javadoc. */ | |
| 33 | @GetMapping("/token") | |
| 34 | public ResponseEntity<?> getLaboratoryToken(@AuthenticationPrincipal UserDetails userDetails) { | |
| 35 | Optional<User> userOpt = userRepository.findByEmailIgnoreCase(userDetails.getUsername()); | |
| 36 |
2
1. getLaboratoryToken : negated conditional → KILLED 2. getLaboratoryToken : negated conditional → KILLED |
if (userOpt.isEmpty() || userOpt.get().getRole() != UserRole.ADVISOR) { |
| 37 |
1
1. getLaboratoryToken : replaced return value with null for com/edtech/controller/LaboratoryController::getLaboratoryToken → KILLED |
return ResponseEntity.status(403) |
| 38 | .body(Map.of("error", "Apenas orientadores podem gerar tokens do laboratorio")); | |
| 39 | } | |
| 40 | ||
| 41 | String token = laboratoryTokenService.generateToken(userOpt.get().getId()); | |
| 42 |
1
1. getLaboratoryToken : replaced return value with null for com/edtech/controller/LaboratoryController::getLaboratoryToken → KILLED |
return ResponseEntity.ok(Map.of("token", token, "expires_in", "Final da semana")); |
| 43 | } | |
| 44 | ||
| 45 | /** Javadoc. */ | |
| 46 | @PostMapping("/join") | |
| 47 | public ResponseEntity<?> joinLaboratory( | |
| 48 | @AuthenticationPrincipal UserDetails userDetails, @RequestBody Map<String, String> request) { | |
| 49 | String token = request.get("token"); | |
| 50 |
2
1. joinLaboratory : negated conditional → KILLED 2. joinLaboratory : negated conditional → KILLED |
if (token == null || token.isEmpty()) { |
| 51 |
1
1. joinLaboratory : replaced return value with null for com/edtech/controller/LaboratoryController::joinLaboratory → KILLED |
return ResponseEntity.badRequest().body(Map.of("error", "O campo token e obrigatorio")); |
| 52 | } | |
| 53 | ||
| 54 | Optional<User> advisorOpt = laboratoryTokenService.findAdvisorByToken(token); | |
| 55 |
1
1. joinLaboratory : negated conditional → KILLED |
if (advisorOpt.isEmpty()) { |
| 56 |
1
1. joinLaboratory : replaced return value with null for com/edtech/controller/LaboratoryController::joinLaboratory → KILLED |
return ResponseEntity.status(400).body(Map.of("error", "Token invalido ou expirado")); |
| 57 | } | |
| 58 | ||
| 59 | Optional<User> currentUserOpt = userRepository.findByEmailIgnoreCase(userDetails.getUsername()); | |
| 60 |
1
1. joinLaboratory : negated conditional → KILLED |
if (currentUserOpt.isEmpty()) { |
| 61 |
1
1. joinLaboratory : replaced return value with null for com/edtech/controller/LaboratoryController::joinLaboratory → KILLED |
return ResponseEntity.status(401).build(); |
| 62 | } | |
| 63 | ||
| 64 | User currentUser = currentUserOpt.get(); | |
| 65 | User advisor = advisorOpt.get(); | |
| 66 | ||
| 67 |
1
1. joinLaboratory : removed call to com/edtech/model/User::setInstitutionId → KILLED |
currentUser.setInstitutionId(advisor.getInstitutionId()); |
| 68 | userRepository.save(currentUser); | |
| 69 | ||
| 70 |
1
1. joinLaboratory : replaced return value with null for com/edtech/controller/LaboratoryController::joinLaboratory → KILLED |
return ResponseEntity.ok( |
| 71 | Map.of( | |
| 72 | "message", "Vinculado ao laboratorio com sucesso", | |
| 73 | "advisor_name", advisor.getName(), | |
| 74 | "institution_id", advisor.getInstitutionId())); | |
| 75 | } | |
| 76 | } | |
Mutations | ||
| 36 |
1.1 2.2 |
|
| 37 |
1.1 |
|
| 42 |
1.1 |
|
| 50 |
1.1 2.2 |
|
| 51 |
1.1 |
|
| 55 |
1.1 |
|
| 56 |
1.1 |
|
| 60 |
1.1 |
|
| 61 |
1.1 |
|
| 67 |
1.1 |
|
| 70 |
1.1 |