From 23ff3bc2fc1ffa701f17f9daa312cf6df7ab065a Mon Sep 17 00:00:00 2001 From: "emanuel.trandafir" Date: Fri, 22 Mar 2024 15:37:18 +0100 Subject: [PATCH] BAEL-7547: small fix --- .../events/rewards/LoyalCustomersRepository.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/rewards/LoyalCustomersRepository.java b/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/rewards/LoyalCustomersRepository.java index 764924232d..c4afae0d5d 100644 --- a/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/rewards/LoyalCustomersRepository.java +++ b/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/rewards/LoyalCustomersRepository.java @@ -13,18 +13,21 @@ public class LoyalCustomersRepository { public Optional find(String customerId) { return customers.stream() - .filter(it -> it.customerId().equals(customerId)) - .findFirst(); + .filter(it -> it.customerId().equals(customerId)) + .findFirst(); } public void awardPoints(String customerId, int points) { - var customer = find(customerId).orElseThrow(); + var customer = find(customerId) + .orElseGet(() -> save(new LoyalCustomer(customerId, 0))); + customers.remove(customer); customers.add(customer.addPoints(points)); } - public void save(String customerId) { - customers.add(new LoyalCustomer(customerId, 0)); + public LoyalCustomer save(LoyalCustomer customer) { + customers.add(customer); + return customer; } public boolean isLoyalCustomer(String customerId) {