<commit_before>package ch.wisv.payments.rest;

import ch.wisv.payments.exception.ProductLimitExceededException;
import ch.wisv.payments.model.Order;
import ch.wisv.payments.model.OrderRequest;
import ch.wisv.payments.model.Product;
import ch.wisv.payments.rest.repository.OrderRepository;
import ch.wisv.payments.rest.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class OrderServiceImpl implements OrderService {

    private ProductRepository productRepository;
    private OrderRepository orderRepository;

    private String defaultReturnUrl;

    @Autowired
    public OrderServiceImpl(ProductRepository productRepository,
                            OrderRepository orderRepository) {
        this.productRepository = productRepository;
        this.orderRepository = orderRepository;

        //TODO move to properties
        this.defaultReturnUrl = "https://ch.tudelft.nl/payments/complete";
    }

    @Override
    public Order createOrderFromRequest(OrderRequest request) throws RuntimeException {
        List<Product> products = request.getProductKeys().stream()
                .map(key -> productRepository.findOneByKey(key))
                .filter(Optional::isPresent)
                .map(Optional::get).collect(Collectors.toList());

        // Throw exceptions if product limits are exceeded
        validateProductLimits(products);

        Order order = new Order(products, request.getName(), request.getEmail());

        if (request.getReturnUrl() != null) {
            order.setReturnURL(request.getReturnUrl());
        } else {
            // Get the return URL for any of the products, or get the application default
            String returnUrl = products.stream()
                    .filter(p -> p.getReturnUrl() != null)
                    .map(Product::getReturnUrl).findAny()
                    .orElse(defaultReturnUrl);

            order.setReturnURL(returnUrl);
        }

        return orderRepository.save(order);<commit_msg>Added isEmpty check before Order creation<commit_after>package ch.wisv.payments.rest;

import ch.wisv.payments.exception.EmptyOrderException;
import ch.wisv.payments.exception.ProductLimitExceededException;
import ch.wisv.payments.model.Order;
import ch.wisv.payments.model.OrderRequest;
import ch.wisv.payments.model.Product;
import ch.wisv.payments.rest.repository.OrderRepository;
import ch.wisv.payments.rest.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class OrderServiceImpl implements OrderService {

    private ProductRepository productRepository;
    private OrderRepository orderRepository;

    private String defaultReturnUrl;

    @Autowired
    public OrderServiceImpl(ProductRepository productRepository,
                            OrderRepository orderRepository) {
        this.productRepository = productRepository;
        this.orderRepository = orderRepository;

        //TODO move to properties
        this.defaultReturnUrl = "https://ch.tudelft.nl/payments/complete";
    }

    @Override
    public Order createOrderFromRequest(OrderRequest request) throws RuntimeException {
        List<Product> products = request.getProductKeys().stream()
                .map(key -> productRepository.findOneByKey(key))
                .filter(Optional::isPresent)
                .map(Optional::get).collect(Collectors.toList());

        if (products.isEmpty()) {
            throw new EmptyOrderException("No (valid) products in this order");
        }

        // Throw exceptions if product limits are exceeded
        validateProductLimits(products);

        Order order = new Order(products, request.getName(), request.getEmail());

        if (request.getReturnUrl() != null) {
            order.setReturnURL(request.getReturnUrl());
        } else {
            // Get the return URL for any of the products, or get the application default
            String returnUrl = products.stream()
                    .filter(p -> p.getReturnUrl() != null)
                    .map(Product::getReturnUrl).findAny()
                    .orElse(defaultReturnUrl);

            order.setReturnURL(returnUrl);
        }

        return orderRepository.save(order);
{'fraction_non_alphanumeric': 0.09292798620094868, 'fraction_numerical': 0.0, 'mean_word_length': 3.0409407665505226, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5393134', 'n_tokens_mistral': 1203, 'n_tokens_neox': 1172, 'n_words': 250}