package net.safedata.springboot.training.d03s03.service;

import net.safedata.springboot.training.d03s03.model.Product;
import net.safedata.springboot.training.d03s03.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductService {

    private static final int THROWING_ID = 13;

    private final ProductRepository productRepository;

    @Autowired
    public ProductService(final ProductRepository productRepository) {
        this.productRepository = productRepository;
    }

    @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
    public Product getProduct(final int id) {
        if (id == THROWING_ID) {
            throw new IllegalArgumentException("There is no product with the ID " + THROWING_ID);
        }

        return productRepository.findOne(id);
    }

    @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
    public List<Product> getProducts() {
        final List<Product> products = new ArrayList<>();
        productRepository.findAll().forEach(products::add);
        return products;
    }

    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public String saveProduct(final Product product) {
        productRepository.save(product);
        return "OK";
    }
}

@@ -444,45 +444,195 @@
 java
-.util.ArrayList;%0Aimport java.util.Lis
+x.annotation.PostConstruct;%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0Aimport java.util.function.Function;%0Aimport java.util.stream.Collectors;%0Aimport java.util.stream.StreamSuppor
 t;%0A%0A
@@ -912,32 +912,176 @@
 ository;%0A    %7D%0A%0A
+    @PostConstruct%0A    public void init() %7B%0A        saveProduct(new Product(1, %22Tablet%22));%0A        saveProduct(new Product(2, %22Phone%22));%0A    %7D%0A%0A
     @Transaction
@@ -1075,32 +1075,45 @@
  @Transactional(
+%0A            
 readOnly = true,
@@ -1104,32 +1104,44 @@
 readOnly = true,
+%0A           
  propagation = P
@@ -1151,32 +1151,37 @@
 agation.SUPPORTS
+%0A    
 )%0A    public Pro
@@ -1418,32 +1418,45 @@
  @Transactional(
+%0A            
 readOnly = true,
@@ -1455,16 +1455,28 @@
  = true,
+%0A           
  propaga
@@ -1502,16 +1502,21 @@
 SUPPORTS
+%0A    
 )%0A    pu
@@ -1719,16 +1719,29 @@
 ctional(
+%0A            
 readOnly
@@ -1749,16 +1749,28 @@
 = false,
+%0A           
  propaga
@@ -1796,16 +1796,21 @@
 REQUIRED
+%0A    
 )%0A    pu
@@ -1870,60 +1870,433 @@
     
-productRepository.save(product);%0A        return %22OK%22
+// a lot of processing goes in here, before actually saving the product :)%0A        productRepository.save(product);%0A        return %22OK%22;%0A    %7D%0A%0A    @Transactional(%0A            readOnly = true,%0A            propagation = Propagation.SUPPORTS%0A    )%0A    public List%3CProduct%3E getAll() %7B%0A        return StreamSupport.stream(productRepository.findAll().spliterator(), false)%0A                            .collect(Collectors.toList())
 ;%0A  

Add two products on init