#399 Code Review of a User Exercise Report
Perform a code review:
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
@Sl4j
public class UserController {
private final ExerciseService exerciseService;
private final UserRepository userRepository;
@RequestMapping("/get-report")
public Map<String, Double> getReport() {
log.info("get report");
var users = userRepository.findAll();
Map<String, Double> result = new HashMap<>();
users.forEach(
user -> {
var exercises = exerciseService.getUserPerformance(user.id);
var performance = exercises
.stream()
.mapToDouble(t -> Duration.between(t.getStartTime(), t.getEndTime()).toMinutes())
.average().orElse(0);
result.put(user.getName(), performance);
}
);
return result;
}
}
@Data
class User {
Long id;
String name;
}
@Data
class Exercise {
Long id;
@NotNull
LocalDateTime startTime = LocalDateTime.now();
LocalDateTime endTime;
}
@Service
@RequiredArgsConstructor
class ExerciseService {
private final ExerciseRepository exerciseRepository;
List<Exercise> getUserPerformance(Long userId) {
return exerciseRepository.findByUserId(userId);
}
}
interface ExerciseRepository {
List<Exercise> findByUserId(Long userId);
}
interface UserRepository {
List<User> findAll();
}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