-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat : use vlads BaseJpaRepository and polish
- Loading branch information
1 parent
867e94e
commit d3d5206
Showing
29 changed files
with
344 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
jpa/boot-data-customsequence/src/main/java/com/example/custom/sequence/config/JpaConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.example.custom.sequence.config; | ||
|
||
import io.hypersistence.utils.spring.repository.BaseJpaRepositoryImpl; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; | ||
|
||
@Configuration | ||
@EnableJpaRepositories( | ||
repositoryBaseClass = BaseJpaRepositoryImpl.class, | ||
basePackages = "com.example.custom.sequence.repositories") | ||
public class JpaConfig {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 69 additions & 1 deletion
70
...-data-customsequence/src/main/java/com/example/custom/sequence/mapper/CustomerMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,80 @@ | ||
package com.example.custom.sequence.mapper; | ||
|
||
import com.example.custom.sequence.entities.Customer; | ||
import com.example.custom.sequence.entities.Order; | ||
import com.example.custom.sequence.model.request.CustomerRequest; | ||
import com.example.custom.sequence.model.response.CustomerResponse; | ||
import com.example.custom.sequence.repositories.OrderRepository; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class CustomerMapper { | ||
|
||
private final OrderMapper orderMapper; | ||
private final OrderRepository orderRepository; | ||
|
||
public CustomerMapper(OrderMapper orderMapper, OrderRepository orderRepository) { | ||
this.orderMapper = orderMapper; | ||
this.orderRepository = orderRepository; | ||
} | ||
|
||
public CustomerResponse mapToResponse(Customer saved) { | ||
return new CustomerResponse(saved.getId(), saved.getText()); | ||
return new CustomerResponse( | ||
saved.getId(), | ||
saved.getText(), | ||
orderMapper.mapToResponseListWithOutCustomer(saved.getOrders())); | ||
} | ||
|
||
public Customer mapToEntity(CustomerRequest customerRequest) { | ||
Customer customer = new Customer(customerRequest.text()); | ||
customerRequest | ||
.orders() | ||
.forEach(orderRequest -> customer.addOrder(orderMapper.mapToEntity(orderRequest))); | ||
return customer; | ||
} | ||
|
||
public void updateCustomerFromRequest(CustomerRequest customerRequest, Customer foundCustomer) { | ||
foundCustomer.setText(customerRequest.text()); | ||
List<Order> removedOrders = new ArrayList<>(foundCustomer.getOrders()); | ||
List<Order> ordersFromRequest = | ||
customerRequest.orders().stream() | ||
.map( | ||
orderRequest -> | ||
orderMapper.mapToEntityWithCustomer( | ||
orderRequest, foundCustomer)) | ||
.collect(Collectors.toList()); | ||
removedOrders.removeAll(ordersFromRequest); | ||
|
||
for (Order removedOrder : removedOrders) { | ||
foundCustomer.removeOrder(removedOrder); | ||
} | ||
|
||
List<Order> newOrders = new ArrayList<>(ordersFromRequest); | ||
newOrders.removeAll(foundCustomer.getOrders()); | ||
|
||
ordersFromRequest.removeAll(newOrders); | ||
|
||
for (Order existingOrder : ordersFromRequest) { | ||
existingOrder.setCustomer(foundCustomer); | ||
// manually set the id of the existing order to avoid creating a new order instead of | ||
// updating the existing one | ||
for (Order foundOrder : foundCustomer.getOrders()) { | ||
if (foundOrder.getText().equals(existingOrder.getText())) { | ||
existingOrder.setId(foundOrder.getId()); | ||
break; | ||
} | ||
} | ||
Order mergedOrder = orderRepository.merge(existingOrder); | ||
foundCustomer | ||
.getOrders() | ||
.set(foundCustomer.getOrders().indexOf(mergedOrder), mergedOrder); | ||
} | ||
|
||
for (Order newOrder : newOrders) { | ||
foundCustomer.addOrder(newOrder); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
...stomsequence/src/main/java/com/example/custom/sequence/model/request/CustomerRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.example.custom.sequence.model.request; | ||
|
||
import jakarta.validation.constraints.NotEmpty; | ||
import java.util.List; | ||
|
||
public record CustomerRequest( | ||
@NotEmpty(message = "Text cannot be empty") String text, List<OrderRequest> orders) {} |
8 changes: 8 additions & 0 deletions
8
...-customsequence/src/main/java/com/example/custom/sequence/model/request/OrderRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.example.custom.sequence.model.request; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotEmpty; | ||
|
||
public record OrderRequest( | ||
@NotEmpty(message = "Text cannot be empty") String text, | ||
@NotBlank(message = "CustomerId cannot be blank") String customerId) {} |
5 changes: 4 additions & 1 deletion
5
...omsequence/src/main/java/com/example/custom/sequence/model/response/CustomerResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
package com.example.custom.sequence.model.response; | ||
|
||
public record CustomerResponse(String id, String text) {} | ||
import java.util.List; | ||
|
||
public record CustomerResponse( | ||
String id, String text, List<OrderResponseWithOutCustomer> orderResponses) {} |
3 changes: 3 additions & 0 deletions
3
...rc/main/java/com/example/custom/sequence/model/response/CustomerResponseWithOutOrder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package com.example.custom.sequence.model.response; | ||
|
||
public record CustomerResponseWithOutOrder(String id, String text) {} |
2 changes: 1 addition & 1 deletion
2
...ustomsequence/src/main/java/com/example/custom/sequence/model/response/OrderResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
package com.example.custom.sequence.model.response; | ||
|
||
public record OrderResponse(String id, String text, CustomerResponse customer) {} | ||
public record OrderResponse(String id, String text, CustomerResponseWithOutOrder customer) {} |
3 changes: 3 additions & 0 deletions
3
...rc/main/java/com/example/custom/sequence/model/response/OrderResponseWithOutCustomer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package com.example.custom.sequence.model.response; | ||
|
||
public record OrderResponseWithOutCustomer(String id, String text) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 6 additions & 2 deletions
8
...ustomsequence/src/main/java/com/example/custom/sequence/repositories/OrderRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,17 @@ | ||
package com.example.custom.sequence.repositories; | ||
|
||
import com.example.custom.sequence.entities.Order; | ||
import io.hypersistence.utils.spring.repository.BaseJpaRepository; | ||
import java.util.Optional; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.ListPagingAndSortingRepository; | ||
import org.springframework.data.repository.query.Param; | ||
|
||
public interface OrderRepository extends JpaRepository<Order, String> { | ||
public interface OrderRepository | ||
extends BaseJpaRepository<Order, String>, ListPagingAndSortingRepository<Order, String> { | ||
|
||
@Query("select o from Order o join fetch o.customer where o.id = :id") | ||
Optional<Order> findById(@Param("id") String id); | ||
|
||
void deleteAllInBatch(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.