#508 Code Review of Interview Service
Perform a code review.
What happens if the network fails at the line "// network failure" and what should be done?
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import java.util.List;
import java.util.Map;
@Component
public class InterviewService {
private final ScoreRepository scoreRepository;
private final TransactionTemplate transactionTemplate;
private final InterviewScoreMLService interviewScoreMLService;
private final ObjectMapper objectMapper = new ObjectMapper();
public InterviewService(ScoreRepository scoreRepository,
TransactionTemplate transactionTemplate,
InterviewScoreMLService interviewScoreMLService) {
this.scoreRepository = scoreRepository;
this.transactionTemplate = transactionTemplate;
this.interviewScoreMLService = interviewScoreMLService;
}
/**
* The method calculates how many points the candidate earned,
* saves the result to the database and sends a callback to an external service
*/
public void process(Candidate c) {
transactionTemplate.executeWithoutResult(status -> {
Score s = interviewScoreMLService.compute(c);
String body = objectMapper.writeValueAsString(Map.of(c.getName(), s));
Mono<ResponseEntity<Void>> request = WebClient.create()
.post()
.body(BodyInserters.fromValue(body))
.retrieve()
.toBodilessEntity();
scoreRepository.saveScore(s);
});
// network failure
}
}
class Candidate {
private final String name;
private final List<Integer> exercisesSolvedId;
public Candidate(String name, List<Integer> exercisesSolvedId) {
this.name = name;
this.exercisesSolvedId = exercisesSolvedId;
}
public String getName() {
return name;
}
public List<Integer> getExercisesSolvedId() {
return exercisesSolvedId;
}
}
class Score {
private final String name;
private final int score;
public Score(String name, int score) {
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public int getScore() {
return score;
}
}
Question review
Sign in to start the dialogue
The mentor keeps the context of the current question and your previous messages.
Sign in
Available with Premium
This is just one of the solutions available with Premium. And more:
- 250+ advanced lectures
- 1300+ practice exercises
- 500+ solutions to problems from real interviews
- Personalized Java Hub certificates
- Expanded access to the AI mentor