|
5 | 5 | import eu.happycoders.shop.model.product.Product;
|
6 | 6 | import eu.happycoders.shop.model.product.ProductId;
|
7 | 7 | import io.quarkus.arc.lookup.LookupIfProperty;
|
| 8 | +import jakarta.annotation.PostConstruct; |
8 | 9 | import jakarta.enterprise.context.ApplicationScoped;
|
9 |
| -import jakarta.persistence.EntityManager; |
10 |
| -import jakarta.persistence.EntityManagerFactory; |
11 |
| -import jakarta.persistence.TypedQuery; |
| 10 | +import jakarta.transaction.Transactional; |
12 | 11 | import java.util.List;
|
13 | 12 | import java.util.Optional;
|
14 | 13 |
|
|
21 | 20 | @ApplicationScoped
|
22 | 21 | public class JpaProductRepository implements ProductRepository {
|
23 | 22 |
|
24 |
| - private final EntityManagerFactory entityManagerFactory; |
| 23 | + private final JpaProductPanacheRepository panacheRepository; |
25 | 24 |
|
26 |
| - public JpaProductRepository(EntityManagerFactory entityManagerFactory) { |
27 |
| - this.entityManagerFactory = entityManagerFactory; |
28 |
| - createDemoProducts(); |
| 25 | + public JpaProductRepository(JpaProductPanacheRepository panacheRepository) { |
| 26 | + this.panacheRepository = panacheRepository; |
29 | 27 | }
|
30 | 28 |
|
31 |
| - private void createDemoProducts() { |
| 29 | + @PostConstruct |
| 30 | + void createDemoProducts() { |
32 | 31 | DemoProducts.DEMO_PRODUCTS.forEach(this::save);
|
33 | 32 | }
|
34 | 33 |
|
35 | 34 | @Override
|
| 35 | + @Transactional |
36 | 36 | public void save(Product product) {
|
37 |
| - try (EntityManager entityManager = entityManagerFactory.createEntityManager()) { |
38 |
| - entityManager.getTransaction().begin(); |
39 |
| - entityManager.merge(ProductMapper.toJpaEntity(product)); |
40 |
| - entityManager.getTransaction().commit(); |
41 |
| - } |
| 37 | + panacheRepository.getEntityManager().merge(ProductMapper.toJpaEntity(product)); |
42 | 38 | }
|
43 | 39 |
|
44 | 40 | @Override
|
| 41 | + @Transactional |
45 | 42 | public Optional<Product> findById(ProductId productId) {
|
46 |
| - try (EntityManager entityManager = entityManagerFactory.createEntityManager()) { |
47 |
| - ProductJpaEntity jpaEntity = entityManager.find(ProductJpaEntity.class, productId.value()); |
48 |
| - return ProductMapper.toModelEntityOptional(jpaEntity); |
49 |
| - } |
| 43 | + ProductJpaEntity jpaEntity = panacheRepository.findById(productId.value()); |
| 44 | + return ProductMapper.toModelEntityOptional(jpaEntity); |
50 | 45 | }
|
51 | 46 |
|
52 | 47 | @Override
|
| 48 | + @Transactional |
53 | 49 | public List<Product> findByNameOrDescription(String queryString) {
|
54 |
| - try (EntityManager entityManager = entityManagerFactory.createEntityManager()) { |
55 |
| - TypedQuery<ProductJpaEntity> query = |
56 |
| - entityManager |
57 |
| - .createQuery( |
58 |
| - "from ProductJpaEntity where name like :query or description like :query", |
59 |
| - ProductJpaEntity.class) |
60 |
| - .setParameter("query", "%" + queryString + "%"); |
| 50 | + List<ProductJpaEntity> entities = |
| 51 | + panacheRepository |
| 52 | + .find("name like ?1 or description like ?1", "%" + queryString + "%") |
| 53 | + .list(); |
61 | 54 |
|
62 |
| - List<ProductJpaEntity> entities = query.getResultList(); |
63 |
| - |
64 |
| - return ProductMapper.toModelEntities(entities); |
65 |
| - } |
| 55 | + return ProductMapper.toModelEntities(entities); |
66 | 56 | }
|
67 | 57 | }
|
0 commit comments