diff --git a/src/main/java/com/cheise_proj/auditing/Address.java b/src/main/java/com/cheise_proj/auditing/Address.java
index 20c8b1b..dc2eead 100644
--- a/src/main/java/com/cheise_proj/auditing/Address.java
+++ b/src/main/java/com/cheise_proj/auditing/Address.java
@@ -1,5 +1,6 @@
package com.cheise_proj.auditing;
+import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import lombok.*;
@@ -24,17 +25,23 @@ class Address {
@Column(name = "zip_code")
private String zipCode;
- @ManyToOne
+ @JsonIgnore
+ @ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "customer_id")
+ @ToString.Exclude
private Customer customer;
- static Address of(CustomerDto.CustomerAddress customerAddress) {
+ @Column(name = "customer_id", insertable = false, updatable = false)
+ private Long customerId;
+
+ static Address of(CustomerDto.CustomerAddress customerAddress, Customer customer) {
return Address.builder()
.city(customerAddress.city())
.streetAddress(customerAddress.streetAddress())
.stateCode(customerAddress.stateCode())
.country(customerAddress.country())
.zipCode(customerAddress.zipCode())
+ .customer(customer)
.build();
}
diff --git a/src/main/java/com/cheise_proj/auditing/Customer.java b/src/main/java/com/cheise_proj/auditing/Customer.java
index b9def59..df04e58 100644
--- a/src/main/java/com/cheise_proj/auditing/Customer.java
+++ b/src/main/java/com/cheise_proj/auditing/Customer.java
@@ -33,23 +33,21 @@ class Customer {
private String emailAddress;
@ToString.Exclude
- @OneToMany(mappedBy = "customer", orphanRemoval = true)
+ @OneToMany(mappedBy = "customer", orphanRemoval = true, cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set
addresses;
static Customer of(CustomerDto.CreateCustomer customer) {
- Customer customerEntity = Customer.builder()
+ return Customer.builder()
.firstName(customer.firstName())
.lastName(customer.lastName())
.emailAddress(customer.emailAddress())
.build();
- customerEntity.setAddresses(customer.customerAddress());
- return customerEntity;
}
- void setAddresses(Set customerAddresses) {
+ void setAddresses(Set customerAddresses, Customer customer) {
if (customerAddresses == null) return;
this.addresses = (this.addresses == null) ? new LinkedHashSet<>() : this.addresses;
- Set addressSet = customerAddresses.stream().map(Address::of).collect(Collectors.toSet());
+ Set addressSet = customerAddresses.stream().map(customerAddress -> Address.of(customerAddress, customer)).collect(Collectors.toSet());
this.addresses.addAll(addressSet);
}
}
diff --git a/src/main/java/com/cheise_proj/auditing/CustomerController.java b/src/main/java/com/cheise_proj/auditing/CustomerController.java
index fe5addb..7c5d564 100644
--- a/src/main/java/com/cheise_proj/auditing/CustomerController.java
+++ b/src/main/java/com/cheise_proj/auditing/CustomerController.java
@@ -1,14 +1,15 @@
package com.cheise_proj.auditing;
import jakarta.validation.Valid;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.UriComponentsBuilder;
import java.net.URI;
+import java.util.List;
+import java.util.Map;
@RestController
@RequestMapping("/customers")
@@ -25,4 +26,22 @@ ResponseEntity createCustomer(@RequestBody @Valid CustomerDto.CreateCustome
URI location = UriComponentsBuilder.fromPath("/customers/{id}").buildAndExpand(customer.getId()).toUri();
return ResponseEntity.created(location).build();
}
+
+ @GetMapping
+ ResponseEntity