Skip to content

Commit

Permalink
Merge pull request #10 from wiemanboy/feature/skill-level
Browse files Browse the repository at this point in the history
UPDATE: now use value object for level instead of string
  • Loading branch information
wiemanboy authored Oct 19, 2024
2 parents 117397d + 972b4d4 commit af8726d
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>
<groupId>com.wiemanboy</groupId>
<artifactId>WiemanApi</artifactId>
<version>1.0.1</version>
<version>1.1.0</version>
<name>WiemanApi</name>
<description>WiemanApi</description>
<url/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/wiemanboy/wiemanapi/domain/Skill.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
20 changes: 20 additions & 0 deletions src/main/java/com/wiemanboy/wiemanapi/domain/SkillLevel.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<SkillDto> from(List<Skill> skills) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package com.wiemanboy.wiemanapi.builders;

import com.wiemanboy.wiemanapi.domain.Skill;
import com.wiemanboy.wiemanapi.domain.SkillLevel;
import lombok.Setter;
import lombok.experimental.Accessors;

@Setter
@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));
}
}
9 changes: 8 additions & 1 deletion src/test/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
debug=true
spring.main.allow-bean-definition-overriding=true
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

0 comments on commit af8726d

Please sign in to comment.