diff --git a/src/main/java/com/wafflecorp/store/order/OrderController.java b/src/main/java/com/wafflecorp/store/order/OrderController.java index 833396d..d1762d8 100644 --- a/src/main/java/com/wafflecorp/store/order/OrderController.java +++ b/src/main/java/com/wafflecorp/store/order/OrderController.java @@ -20,9 +20,12 @@ public OrderController(OrderRepository repository) { this.repository = repository; } + /* + You could ask for only the authenticated principal if spring security is on the classpath + allOrders(Principal principal) + */ @QueryMapping - public List allOrders(Principal principal) { - log.info("Authenticated Principal: " + principal); + public List allOrders() { return repository.findAll(); } diff --git a/src/main/java/com/wafflecorp/store/product/Product.java b/src/main/java/com/wafflecorp/store/product/Product.java index 02ada49..dc13b2a 100644 --- a/src/main/java/com/wafflecorp/store/product/Product.java +++ b/src/main/java/com/wafflecorp/store/product/Product.java @@ -1,7 +1,11 @@ package com.wafflecorp.store.product; +import com.wafflecorp.store.order.Order; import jakarta.persistence.*; +import java.util.HashSet; +import java.util.Set; + @Entity public class Product { @@ -11,8 +15,8 @@ public class Product { private String title; @Column(name = "description") private String desc; -// @OneToMany(mappedBy = "product") -// private Set orders = new HashSet<>(); + @OneToMany(mappedBy = "product") + private Set orders = new HashSet<>(); public Product() {} @@ -51,13 +55,13 @@ public void setDesc(String desc) { this.desc = desc; } -// public Set getOrders() { -// return orders; -// } -// -// public void setOrders(Set orders) { -// this.orders = orders; -// } + public Set getOrders() { + return orders; + } + + public void setOrders(Set orders) { + this.orders = orders; + } @Override public String toString() { diff --git a/src/main/java/com/wafflecorp/store/product/ProductController.java b/src/main/java/com/wafflecorp/store/product/ProductController.java index ee2a7c1..414c659 100644 --- a/src/main/java/com/wafflecorp/store/product/ProductController.java +++ b/src/main/java/com/wafflecorp/store/product/ProductController.java @@ -15,10 +15,12 @@ public class ProductController { private final ProductRepository repository; private final OrderRepository orderRepository; + private final ProductService productService; - public ProductController(ProductRepository repository, OrderRepository orderRepository) { + public ProductController(ProductRepository repository, OrderRepository orderRepository, ProductService productService) { this.repository = repository; this.orderRepository = orderRepository; + this.productService = productService; } @QueryMapping @@ -26,8 +28,6 @@ public List allProducts() { return repository.findAll(); } - - @QueryMapping public Optional getProduct(@Argument Integer id) { return Optional.ofNullable(repository.findById(id).orElseThrow(ProductNotFoundException::new)); @@ -36,10 +36,9 @@ public Optional getProduct(@Argument Integer id) { @Secured("ROLE_ADMIN") @MutationMapping public Product createProduct(@Argument ProductInput productInput) { - return repository.save(new Product(productInput.title(), productInput.desc())); + return productService.create(new Product(productInput.title(), productInput.desc())); } - // @SchemaMapping // public List orders(Product product) { // return orderRepository.findAllByProductId(product.getId()); diff --git a/src/main/java/com/wafflecorp/store/product/ProductService.java b/src/main/java/com/wafflecorp/store/product/ProductService.java index 5ddb4ff..4d6a896 100644 --- a/src/main/java/com/wafflecorp/store/product/ProductService.java +++ b/src/main/java/com/wafflecorp/store/product/ProductService.java @@ -8,12 +8,12 @@ public class ProductService { private final ProductRepository productRepository; - public ProductService(ProductRepository productRepository) { + ProductService(ProductRepository productRepository) { this.productRepository = productRepository; } @PreAuthorize("hasRole('ADMIN')") - public Product create(Product product) { + Product create(Product product) { return productRepository.save(product); } } diff --git a/src/test/java/com/wafflecorp/store/controller/ProductControllerTest.java b/src/test/java/com/wafflecorp/store/controller/ProductControllerTest.java index cbdd31d..6fa6715 100644 --- a/src/test/java/com/wafflecorp/store/controller/ProductControllerTest.java +++ b/src/test/java/com/wafflecorp/store/controller/ProductControllerTest.java @@ -2,14 +2,18 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; +import com.wafflecorp.store.order.OrderRepository; import com.wafflecorp.store.product.Product; import com.wafflecorp.store.product.ProductController; import com.wafflecorp.store.product.ProductRepository; +import com.wafflecorp.store.product.ProductService; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.mockito.Mock; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.graphql.GraphQlTest; import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.annotation.Import; import org.springframework.graphql.test.tester.GraphQlTester; import java.io.IOException; @@ -25,16 +29,26 @@ class ProductControllerTest { @Autowired - private GraphQlTester graphQlTester; + GraphQlTester graphQlTester; + + @MockBean + ProductRepository productRepository; + @MockBean - private ProductRepository productRepository; - private List products = new ArrayList<>(); + OrderRepository orderRepository; + + @MockBean + ProductService productService; + + List products = new ArrayList<>(); @Test public void contextLoads() { assertNotNull(graphQlTester); assertNotNull(productRepository); + assertNotNull(orderRepository); + assertNotNull(productService); } @BeforeEach