Skip to content

Commit

Permalink
Adding find all orders by authenticated principal
Browse files Browse the repository at this point in the history
  • Loading branch information
danvega committed Jul 27, 2023
1 parent e187569 commit 0093f50
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
10 changes: 6 additions & 4 deletions src/main/java/com/wafflecorp/store/config/DatabaseInit.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,17 @@ public void run(ApplicationArguments args) throws Exception {
productRepository.saveAll(products);

Customer dan = new Customer("Dan","Vega","danvega@gmail.com");
customerRepository.save(dan);
var andreJohnson = new Customer("Andre", "Johnson","ajohnson@gmail.com");
var customers = List.of(dan,andreJohnson);
customerRepository.saveAll(customers);

List<Order> orders = new ArrayList<>();
for (int i = 1; i <= 20; i++) {
for (int i = 1; i <= 30; i++) {
orders.add(new Order(new Random().nextInt(10),
LocalDate.now().plusDays(new Random().nextInt(30)),
OrderStatus.values()[new Random().nextInt(OrderStatus.values().length)],
products.get(new Random().nextInt(products.size()-1)),
dan));
products.get(new Random().nextInt(products.size())),
customers.get(new Random().nextInt(customers.size()))));
}
orderRepository.saveAll(orders);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,29 @@

import com.wafflecorp.store.model.Order;
import com.wafflecorp.store.repository.OrderRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.graphql.data.method.annotation.Argument;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.stereotype.Controller;

import java.security.Principal;
import java.util.List;
import java.util.Optional;

@Controller
public class OrderController {

private static final Logger log = LoggerFactory.getLogger(OrderController.class);
private final OrderRepository repository;

public OrderController(OrderRepository repository) {
this.repository = repository;
}

@QueryMapping
public Iterable<Order> allOrders() {
public List<Order> allOrders(Principal principal) {
log.info("Authenticated Principal: " + principal);
return repository.findAll();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package com.wafflecorp.store.repository;

import com.wafflecorp.store.model.Customer;
import org.springframework.data.repository.ListCrudRepository;
import org.springframework.data.repository.Repository;

import java.util.List;

public interface CustomerRepository extends Repository<Customer,Integer> {
public interface CustomerRepository extends ListCrudRepository<Customer,Integer> {

List<Customer> findAll();

Customer findByLastName(String last);

void save(Customer customer);
Customer findByLastName(String name);

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import org.springframework.data.domain.ScrollPosition;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Window;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.ListCrudRepository;

import java.util.List;

public interface OrderRepository extends CrudRepository<Order,Integer> {
public interface OrderRepository extends ListCrudRepository<Order,Integer> {

List<Order> findByCustomer(Customer customer);

Expand Down

0 comments on commit 0093f50

Please sign in to comment.