Commit 1ec6407a by Patryk Czarnik

REST dla klasy Order

parent 0d04f0ab
{
"orderId": 1,
"deliveryDate": null,
"orderDate": "2021-11-20T11:30:00.000+00:00",
"status": "PAID",
"orderProducts": [
{
"id": {
"orderId": 1,
"productId": 2
},
"actualPrice": 2400.00,
"actualVat": 0.23,
"quantity": 3,
"product": {
"productId": 2,
"description": "Odkurzacz o wyglądzie przypominającym Matiza",
"price": 805.00,
"productName": "odkurzacz",
"vat": 0.23
}
},
{
"id": {
"orderId": 1,
"productId": 1
},
"actualPrice": 2900.00,
"actualVat": 0.23,
"quantity": 1,
"product": {
"productId": 1,
"description": "Pralka szybkoobrotowa",
"price": 2501.00,
"productName": "pralka",
"vat": 0.23
}
}
],
"customer": {
"customerEmail": "ala@example.com",
"address": "Jasna 14/16",
"city": "Warszawa",
"customerName": "Ala Kowalska",
"phoneNumber": "123123123",
"postalCode": "01-235"
}
}
\ No newline at end of file
...@@ -7,6 +7,8 @@ import jakarta.validation.constraints.Pattern; ...@@ -7,6 +7,8 @@ import jakarta.validation.constraints.Pattern;
import java.util.List; import java.util.List;
import com.fasterxml.jackson.annotation.JsonIgnore;
/** /**
* The persistent class for the customers database table. * The persistent class for the customers database table.
...@@ -39,6 +41,7 @@ public class Customer implements Serializable { ...@@ -39,6 +41,7 @@ public class Customer implements Serializable {
//bi-directional many-to-one association to Order //bi-directional many-to-one association to Order
@OneToMany(mappedBy="customer") @OneToMany(mappedBy="customer")
@JsonIgnore
private List<Order> orders; private List<Order> orders;
public Customer() { public Customer() {
......
...@@ -8,6 +8,8 @@ import jakarta.validation.constraints.Min; ...@@ -8,6 +8,8 @@ import jakarta.validation.constraints.Min;
import java.math.BigDecimal; import java.math.BigDecimal;
import com.fasterxml.jackson.annotation.JsonIgnore;
/** /**
* The persistent class for the order_products database table. * The persistent class for the order_products database table.
...@@ -37,6 +39,7 @@ public class OrderProduct implements Serializable { ...@@ -37,6 +39,7 @@ public class OrderProduct implements Serializable {
//bi-directional many-to-one association to Order //bi-directional many-to-one association to Order
@ManyToOne @ManyToOne
@JoinColumn(name="order_id", insertable=false, updatable=false) @JoinColumn(name="order_id", insertable=false, updatable=false)
@JsonIgnore
private Order order; private Order order;
//uni-directional many-to-one association to Product //uni-directional many-to-one association to Product
......
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 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("/rest/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);
}
}
}
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