Commit 063c409e by Patryk Czarnik

ProductRepository i odczyt w wersji tekstowej

parent 86879e1e
package sklep.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sklep.model.Product;
import sklep.repository.ProductRepository;
@Controller
public class ProductController {
@Autowired
private ProductRepository productRepository;
@GetMapping(path="/products", produces="text/plain")
@ResponseBody
public String getProductsTxt() {
StringBuilder sb = new StringBuilder("Lista produktów:\n");
List<Product> products = productRepository.findAll();
for(Product product : products) {
sb.append(" * ").append(product.getProductName())
.append(" za cenę ").append(product.getPrice())
.append('\n');
}
return sb.toString();
}
}
package sklep.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import sklep.model.Product;
public interface ProductRepository extends JpaRepository<Product, Integer> {
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment