#292 Code Review of an Equipment Lookup Service
Perform a code review:
@Service
class EquipmentService {
@Autowired
private FurnitureRepository furnitureRepository;
@Autowired
private PcRepository pcRepository;
private Set<Equipment> requestedData = new HashSet<>();
public void validateRequest(UUID id) throws NotFoundException {
var furniture = furnitureRepository.findById(id);
if (furniture != null) {
requestedData.add(furniture);
return;
}
var pc = pcRepository.findById(id);
if (pc == null)
throw new NotFoundException(id);
requestedData.add(pc);
}
public Set<Equipment> getRequestedData() {
var result = new HashSet<Equipment>(requestedData);
requestedData = new HashSet<>();
return result;
}
}
@RestController
class EquipmentController {
@Setter(onMethod = @Autowired)
private EquipmentService equipmentService;
@GetMapping("/equipments/{ids}")
public Set<Equipment> getEquipment(@PathVariable("ids") List<UUID> ids) {
ids.forEach(equipmentService::validateRequest);
return equipmentService.getRequestedData();
}
}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