#292 Code Review of an Equipment Lookup Service

Companies:
Digitalleague
Themes:
SpringSpring CoreCode Review

Perform a code review:

Java
@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

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in