From 64e0181ecb605c7230a4f293122f434d960da858 Mon Sep 17 00:00:00 2001 From: jarno Date: Sat, 19 Oct 2024 16:07:06 +0200 Subject: [PATCH 1/3] UPDATE: now use value object for level instead of string --- .../wiemanapi/application/ProfileService.java | 4 ++-- .../com/wiemanboy/wiemanapi/domain/Skill.java | 5 +++-- .../wiemanapi/domain/SkillLevel.java | 20 +++++++++++++++++++ .../InvalidSkillLevelException.java | 18 +++++++++++++++++ .../presentation/dto/response/SkillDto.java | 4 ++-- .../wiemanapi/builders/SkillBuilder.java | 5 +++-- 6 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/wiemanboy/wiemanapi/domain/SkillLevel.java create mode 100644 src/main/java/com/wiemanboy/wiemanapi/domain/exceptions/InvalidSkillLevelException.java diff --git a/src/main/java/com/wiemanboy/wiemanapi/application/ProfileService.java b/src/main/java/com/wiemanboy/wiemanapi/application/ProfileService.java index 847c621..127c794 100644 --- a/src/main/java/com/wiemanboy/wiemanapi/application/ProfileService.java +++ b/src/main/java/com/wiemanboy/wiemanapi/application/ProfileService.java @@ -55,7 +55,7 @@ public Profile createProfile(String firstName, String lastName, String username, .map( skillDto -> Skill.builder() .name(skillDto.name()) - .level(skillDto.level()) + .level(SkillLevel.of(skillDto.level())) .build() ).toList() ).build( @@ -107,7 +107,7 @@ public Profile updateProfile(String id, String firstName, String lastName, Strin .map( skillDto -> Skill.builder() .name(skillDto.name()) - .level(skillDto.level()) + .level(SkillLevel.of(skillDto.level())) .build() ).toList() ).build( diff --git a/src/main/java/com/wiemanboy/wiemanapi/domain/Skill.java b/src/main/java/com/wiemanboy/wiemanapi/domain/Skill.java index eb66c73..92dac88 100644 --- a/src/main/java/com/wiemanboy/wiemanapi/domain/Skill.java +++ b/src/main/java/com/wiemanboy/wiemanapi/domain/Skill.java @@ -9,10 +9,11 @@ @Builder public class Skill { private String name; - private String level; + private SkillLevel level; - public Skill(String name, String level) { + public Skill(String name, SkillLevel level) { this.name = name; this.level = level; } + } diff --git a/src/main/java/com/wiemanboy/wiemanapi/domain/SkillLevel.java b/src/main/java/com/wiemanboy/wiemanapi/domain/SkillLevel.java new file mode 100644 index 0000000..670c4c8 --- /dev/null +++ b/src/main/java/com/wiemanboy/wiemanapi/domain/SkillLevel.java @@ -0,0 +1,20 @@ +package com.wiemanboy.wiemanapi.domain; + +import com.wiemanboy.wiemanapi.domain.exceptions.InvalidSkillLevelException; +import lombok.Value; + +@Value +public class SkillLevel { + int value; + + private SkillLevel(int value) { + this.value = value; + } + + public static SkillLevel of(int value) { + if (value < 0 || value > 5) { + throw new InvalidSkillLevelException(0, 5); + } + return new SkillLevel(value); + } +} diff --git a/src/main/java/com/wiemanboy/wiemanapi/domain/exceptions/InvalidSkillLevelException.java b/src/main/java/com/wiemanboy/wiemanapi/domain/exceptions/InvalidSkillLevelException.java new file mode 100644 index 0000000..99d155a --- /dev/null +++ b/src/main/java/com/wiemanboy/wiemanapi/domain/exceptions/InvalidSkillLevelException.java @@ -0,0 +1,18 @@ +package com.wiemanboy.wiemanapi.domain.exceptions; + +import lombok.Getter; + +import static java.lang.String.format; + +@Getter +public class InvalidSkillLevelException extends RuntimeException { + + private final int min; + private final int max; + + public InvalidSkillLevelException(int min, int max) { + super(format("Level must be between %d and %d", min, max)); + this.min = min; + this.max = max; + } +} diff --git a/src/main/java/com/wiemanboy/wiemanapi/presentation/dto/response/SkillDto.java b/src/main/java/com/wiemanboy/wiemanapi/presentation/dto/response/SkillDto.java index 85aa789..46161c1 100644 --- a/src/main/java/com/wiemanboy/wiemanapi/presentation/dto/response/SkillDto.java +++ b/src/main/java/com/wiemanboy/wiemanapi/presentation/dto/response/SkillDto.java @@ -6,10 +6,10 @@ public record SkillDto( String name, - String level + int level ) { public static SkillDto from(Skill skill) { - return new SkillDto(skill.getName(), skill.getName()); + return new SkillDto(skill.getName(), skill.getLevel().getValue()); } public static List from(List skills) { diff --git a/src/test/java/com/wiemanboy/wiemanapi/builders/SkillBuilder.java b/src/test/java/com/wiemanboy/wiemanapi/builders/SkillBuilder.java index 00b5141..57d6e5a 100644 --- a/src/test/java/com/wiemanboy/wiemanapi/builders/SkillBuilder.java +++ b/src/test/java/com/wiemanboy/wiemanapi/builders/SkillBuilder.java @@ -1,6 +1,7 @@ package com.wiemanboy.wiemanapi.builders; import com.wiemanboy.wiemanapi.domain.Skill; +import com.wiemanboy.wiemanapi.domain.SkillLevel; import lombok.Setter; import lombok.experimental.Accessors; @@ -8,9 +9,9 @@ @Accessors(chain = true) public class SkillBuilder { private String name = "Skill"; - private String description = "Description"; + private int level = 5; public Skill build() { - return new Skill(name, description); + return new Skill(name, SkillLevel.of(level)); } } From bd7d41dbcf6a23b19e20b0b024f2f83e349ade79 Mon Sep 17 00:00:00 2001 From: jarno Date: Sat, 19 Oct 2024 16:07:26 +0200 Subject: [PATCH 2/3] FIX: data test functional test configuration --- src/test/resources/application.properties | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties index e16eb8e..f1d1279 100644 --- a/src/test/resources/application.properties +++ b/src/test/resources/application.properties @@ -1,2 +1,9 @@ debug=true -spring.main.allow-bean-definition-overriding=true \ No newline at end of file +spring.main.allow-bean-definition-overriding=true +spring.data.mongodb.host=${MONGO_HOST:localhost} +spring.data.mongodb.port=${MONGO_PORT:27017} +spring.data.mongodb.database=${MONGO_DB:dev_db} +spring.data.mongodb.username=${MONGO_USER:admin} +spring.data.mongodb.password=${MONGO_PASS:admin} +spring.data.mongodb.authentication-database=${MONGO_AUTH_DB:admin} +spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration \ No newline at end of file From 972b4d4ef21f46ef8d6710466008b33907bbdb7a Mon Sep 17 00:00:00 2001 From: jarno Date: Sat, 19 Oct 2024 16:07:53 +0200 Subject: [PATCH 3/3] UPDATE: bump version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index c99e20d..966e0dd 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ com.wiemanboy WiemanApi - 1.0.1 + 1.1.0 WiemanApi WiemanApi