Commit c27d9942 by Patryk Czarnik

Obsługa Customerów i Orderów - CYKLICZNE DOWIĄZANIE

parent 6b786cce
package sklep.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import sklep.model.Customer;
public interface CustomerRepository extends JpaRepository<Customer, String> {
}
package sklep.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import sklep.model.Order;
public interface OrderRepository extends JpaRepository<Order, Integer> {
}
package sklep.rest;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
import sklep.model.Customer;
import sklep.repository.CustomerRepository;
@RestController
@RequestMapping("/customers")
public class CustomerEndpoint {
@Autowired
private CustomerRepository customerRepository;
@GetMapping
public List<Customer> listaKlientow(Model model) {
return customerRepository.findAll();
}
@GetMapping("/{email}")
public Customer jedenKlient(Model model, @PathVariable("email") String email) {
return customerRepository.findById(email).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
}
}
package sklep.rest;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
import sklep.model.Order;
import sklep.repository.OrderRepository;
@RestController
@RequestMapping("/orders")
public class OrderEndpoint {
@Autowired
private OrderRepository orderRepository;
@GetMapping
public List<Order> readAll() {
return orderRepository.findAll();
}
@GetMapping("/{id}")
public Order readOne(@PathVariable("id") int id) {
Optional<Order> order = orderRepository.findById(id);
if(order.isPresent()) {
return order.get();
} else {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Nie ma zamówienia o numerze " + id);
}
}
}
...@@ -28,6 +28,7 @@ public class ProductEndpoint { ...@@ -28,6 +28,7 @@ public class ProductEndpoint {
} }
@GetMapping(produces={"application/json", "application/xml"}) @GetMapping(produces={"application/json", "application/xml"})
// Prawdę mówiąc dla formatu XML powinna być oddzielna metoda, która zwraca coś takiego jak ProductList z projektu 35.
public List<Product> getAllProducts() { public List<Product> getAllProducts() {
return productRepository.findAll(); return productRepository.findAll();
} }
......
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