#342 Refactoring a Product Processing Service

Companies:
Sberbank
Themes:
SpringSpring CoreCode Review

Implement the TODO and refactor the code if necessary:

@Service
@RequiredArgsConstructor
@Sl4j
class ProductServiceImpl implements ProductService {
    private final ProductRepository repository;
    private OtherService otherService;
    private Map<String, String> dictionary;

    @PostConstruct
    @Transactional
    public void postConstruct(){
        dictionary = otherService.getDictionary();
    }

    @Autowired
    public void setOtherService(OtherService otherService) {
        this.otherService = otherService;
    }


    /** Method returns a list of processed Products
     *
     * @param productIds
     * @return
     */
    @Transactional
    public List<ProductProcessResult> processProducts(List<Long> productIds){
        productIds.stream().map(productIds.mapToObject)
        //TODO
    }

    @Transactional
    public Product getProduct(Long id){

        if(id == null){
            throw new UnsupportedOperationException("Not supported");
        }
        return repository.findById(id);
    }

    public ProductProcessResult process(Product product) throws IOException {
        if(product.getProductStatus() == PROCESSED){
            throw new IOException("a product in PROCESSED state cannot be processed");
        }
        otherService.process(product);
        product.setProductState(PROCESSED);
        return product;
    }
}

@Getter
@HashcodeAndEquals
public class Product {
    enum ProductStatus{
        NEW, PROCESSED
    }

    private int id;
    private ProductStatus productStatus;
    private String name;
    private Double price;
    private List<String> tags;

    Product(int id, ProductStatus productStatus, String name, Double price, List<String> tags){
        this.id = id;
        this.productStatus = productStatus;
        this.name = name;
        this.price = price;
        this.tags = tags;
    }

}

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