From 0d19c7a5363113b0fb96591a2743094a9859220a Mon Sep 17 00:00:00 2001 From: Roman Nimets Date: Sun, 5 May 2024 18:06:24 +0200 Subject: [PATCH] Reapply "fix: Refactor Layered Architecture pattern #2936" This reverts commit 54aee49b04d22a3dfc2a9b41d1009b7a80d0ab1c. --- layers/src/main/java/com/iluwatar/layers/Runner.java | 10 ++++++++++ layers/src/main/java/dto/CakeInfo.java | 9 ++++----- layers/src/main/java/dto/CakeLayerInfo.java | 10 ++++------ layers/src/main/java/dto/CakeToppingInfo.java | 11 ++++------- 4 files changed, 22 insertions(+), 18 deletions(-) diff --git a/layers/src/main/java/com/iluwatar/layers/Runner.java b/layers/src/main/java/com/iluwatar/layers/Runner.java index 5e2ce2d8d2fe..5e9d28758a56 100644 --- a/layers/src/main/java/com/iluwatar/layers/Runner.java +++ b/layers/src/main/java/com/iluwatar/layers/Runner.java @@ -32,6 +32,10 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.domain.EntityScan; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.stereotype.Component; import service.CakeBakingService; import view.CakeViewImpl; @@ -44,6 +48,8 @@ * It uses the CakeBakingService to save new layers and toppings and to bake new cakes. * It also handles exceptions that might occur during the cake baking process.

*/ +@EntityScan(basePackages = "entity") +@ComponentScan(basePackages = {"com.iluwatar.layers", "service", "dto", "exception", "view", "dao"}) @Component @Slf4j public class Runner implements CommandLineRunner { @@ -64,6 +70,10 @@ public void run(String... args) { cakeView.render(); } + public static void main(String[] args) { + SpringApplication.run(Runner.class, args); + } + /** * Initializes the example data. */ diff --git a/layers/src/main/java/dto/CakeInfo.java b/layers/src/main/java/dto/CakeInfo.java index a93b60c6364c..0bda58239dd8 100644 --- a/layers/src/main/java/dto/CakeInfo.java +++ b/layers/src/main/java/dto/CakeInfo.java @@ -26,14 +26,13 @@ package dto; import java.util.List; -import java.util.Optional; /** * DTO for cakes. */ public class CakeInfo { - public final Optional id; + public final Long id; public final CakeToppingInfo cakeToppingInfo; public final List cakeLayerInfos; @@ -41,7 +40,7 @@ public class CakeInfo { * Constructor. */ public CakeInfo(Long id, CakeToppingInfo cakeToppingInfo, List cakeLayerInfos) { - this.id = Optional.of(id); + this.id = id; this.cakeToppingInfo = cakeToppingInfo; this.cakeLayerInfos = cakeLayerInfos; } @@ -50,7 +49,7 @@ public CakeInfo(Long id, CakeToppingInfo cakeToppingInfo, List ca * Constructor. */ public CakeInfo(CakeToppingInfo cakeToppingInfo, List cakeLayerInfos) { - this.id = Optional.empty(); + this.id = null; this.cakeToppingInfo = cakeToppingInfo; this.cakeLayerInfos = cakeLayerInfos; } @@ -66,7 +65,7 @@ public int calculateTotalCalories() { @Override public String toString() { - return String.format("CakeInfo id=%d topping=%s layers=%s totalCalories=%d", id.orElse(-1L), + return String.format("CakeInfo id=%d topping=%s layers=%s totalCalories=%d", id, cakeToppingInfo, cakeLayerInfos, calculateTotalCalories()); } } diff --git a/layers/src/main/java/dto/CakeLayerInfo.java b/layers/src/main/java/dto/CakeLayerInfo.java index 244ae2cf6316..6689b87461c2 100644 --- a/layers/src/main/java/dto/CakeLayerInfo.java +++ b/layers/src/main/java/dto/CakeLayerInfo.java @@ -25,14 +25,12 @@ package dto; -import java.util.Optional; - /** * DTO for cake layers. */ public class CakeLayerInfo { - public final Optional id; + public final Long id; public final String name; public final int calories; @@ -40,7 +38,7 @@ public class CakeLayerInfo { * Constructor. */ public CakeLayerInfo(Long id, String name, int calories) { - this.id = Optional.of(id); + this.id = id; this.name = name; this.calories = calories; } @@ -49,13 +47,13 @@ public CakeLayerInfo(Long id, String name, int calories) { * Constructor. */ public CakeLayerInfo(String name, int calories) { - this.id = Optional.empty(); + this.id = null; this.name = name; this.calories = calories; } @Override public String toString() { - return String.format("CakeLayerInfo id=%d name=%s calories=%d", id.orElse(-1L), name, calories); + return String.format("CakeLayerInfo id=%d name=%s calories=%d", id, name, calories); } } diff --git a/layers/src/main/java/dto/CakeToppingInfo.java b/layers/src/main/java/dto/CakeToppingInfo.java index a1e2e9ba4116..a3657208668e 100644 --- a/layers/src/main/java/dto/CakeToppingInfo.java +++ b/layers/src/main/java/dto/CakeToppingInfo.java @@ -25,15 +25,12 @@ package dto; - -import java.util.Optional; - /** * DTO for cake toppings. */ public class CakeToppingInfo { - public final Optional id; + public final Long id; public final String name; public final int calories; @@ -41,7 +38,7 @@ public class CakeToppingInfo { * Constructor. */ public CakeToppingInfo(Long id, String name, int calories) { - this.id = Optional.of(id); + this.id = id; this.name = name; this.calories = calories; } @@ -50,14 +47,14 @@ public CakeToppingInfo(Long id, String name, int calories) { * Constructor. */ public CakeToppingInfo(String name, int calories) { - this.id = Optional.empty(); + this.id = null; this.name = name; this.calories = calories; } @Override public String toString() { - return String.format("CakeToppingInfo id=%d name=%s calories=%d", id.orElse(-1L), name, + return String.format("CakeToppingInfo id=%d name=%s calories=%d", id, name, calories); } }