diff --git a/src/main/java/edu/ntnu/idatt2106/sparesti/dto/challenge/ChallengeDto.java b/src/main/java/edu/ntnu/idatt2106/sparesti/dto/challenge/ChallengeDto.java index 4c5de70e91b458848dd260aaadfb06cb4e761f01..f5910efa34b44ee11a72cb10255168586325f067 100644 --- a/src/main/java/edu/ntnu/idatt2106/sparesti/dto/challenge/ChallengeDto.java +++ b/src/main/java/edu/ntnu/idatt2106/sparesti/dto/challenge/ChallengeDto.java @@ -24,9 +24,12 @@ import lombok.experimental.SuperBuilder; @AllArgsConstructor @JsonTypeInfo( use = JsonTypeInfo.Id.NAME, - property = "challengeType") + property = "challengeType", + defaultImpl = ChallengeDto.class) @JsonSubTypes({ - @JsonSubTypes.Type(value = SavingChallengeDto.class, name = "SavingChallengeDto")} + @JsonSubTypes.Type(value = SavingChallengeDto.class, name = "SavingChallengeDto") + +} ) public class ChallengeDto { diff --git a/src/main/java/edu/ntnu/idatt2106/sparesti/model/challenge/Challenge.java b/src/main/java/edu/ntnu/idatt2106/sparesti/model/challenge/Challenge.java index e5ddb2c19e2d071fd11005d10be9f879bcca2371..1a85aa34542f4061f19717505b26b93b69756a02 100644 --- a/src/main/java/edu/ntnu/idatt2106/sparesti/model/challenge/Challenge.java +++ b/src/main/java/edu/ntnu/idatt2106/sparesti/model/challenge/Challenge.java @@ -73,7 +73,6 @@ public class Challenge { @ManyToOne @JoinColumn(name = "user_id", nullable = false) - @NonNull private User user; } diff --git a/src/main/java/edu/ntnu/idatt2106/sparesti/service/challenge/ChallengeService.java b/src/main/java/edu/ntnu/idatt2106/sparesti/service/challenge/ChallengeService.java index 48248b599e0439c99b3adbd481d3bd569a0313fa..b54a1753ba760e6fdb530cbbd62632c34da943d7 100644 --- a/src/main/java/edu/ntnu/idatt2106/sparesti/service/challenge/ChallengeService.java +++ b/src/main/java/edu/ntnu/idatt2106/sparesti/service/challenge/ChallengeService.java @@ -83,8 +83,13 @@ public class ChallengeService { savingChallengeMapperImpl.savingChallengeDtoToSavingChallenge( (SavingChallengeDto) challenge, user, challengeMapperImpl); challengesRepository.save(savingChallenge); + } else { + Challenge newChallenge = challengeMapperImpl.challengeDtoToChallenge(challenge); + newChallenge.setUser(user); + challengesRepository.save(newChallenge); } + } @@ -161,9 +166,9 @@ public class ChallengeService { if (challenge instanceof SavingChallenge) { return savingChallengeMapperImpl.savingChallengeDto( (SavingChallenge) challenge, challengeMapperImpl); - } else { - throw new IllegalArgumentException("Not an appropriate challenge type."); } + + return challengeMapperImpl.challengeIntoChallengeDto(challenge); } diff --git a/src/test/java/edu/ntnu/idatt2106/sparesti/model/challenge/util/ChallengeUtility.java b/src/test/java/edu/ntnu/idatt2106/sparesti/model/challenge/util/ChallengeUtility.java index 104dfa7237a441803b21f771b9c19d40f87b0045..991f8eb2ef89dd95ed96c9c25a0e9faa820ff98f 100644 --- a/src/test/java/edu/ntnu/idatt2106/sparesti/model/challenge/util/ChallengeUtility.java +++ b/src/test/java/edu/ntnu/idatt2106/sparesti/model/challenge/util/ChallengeUtility.java @@ -1,5 +1,6 @@ package edu.ntnu.idatt2106.sparesti.model.challenge.util; +import edu.ntnu.idatt2106.sparesti.dto.challenge.ChallengeDto; import edu.ntnu.idatt2106.sparesti.dto.challenge.ChallengeUpdateRequestDto; import edu.ntnu.idatt2106.sparesti.dto.challenge.SavingChallengeDto; import edu.ntnu.idatt2106.sparesti.dto.user.LoginRequestDto; @@ -75,6 +76,18 @@ public class ChallengeUtility { .build(); } + public static ChallengeDto createChallengeDto() { + return ChallengeDto.builder() + .id(1L) + .title("Challenge") + .startDate(LocalDate.parse("2021-10-10")) + .endDate(LocalDate.parse("2021-10-20")) + .difficulty("EASY") + .lives(3) + .currentTile(0) + .build(); + } + public static ChallengeUpdateRequestDto createChallengeUpdateRequestDto() { return ChallengeUpdateRequestDto.builder() .currentTiles(1) diff --git a/src/test/java/edu/ntnu/idatt2106/sparesti/service/challenge/ChallengeServiceTest.java b/src/test/java/edu/ntnu/idatt2106/sparesti/service/challenge/ChallengeServiceTest.java index ef1d95206be4c5dafe05bc1b62a699336fa18917..ec407666e99597bca5dc6dc2674611cdd8c3a6a6 100644 --- a/src/test/java/edu/ntnu/idatt2106/sparesti/service/challenge/ChallengeServiceTest.java +++ b/src/test/java/edu/ntnu/idatt2106/sparesti/service/challenge/ChallengeServiceTest.java @@ -66,6 +66,7 @@ class ChallengeServiceTest { //Act assertDoesNotThrow(() -> challengeService.addChallenge(principal, ChallengeUtility.createSavingChallengeDto())); + assertDoesNotThrow(() -> challengeService.addChallenge(principal, ChallengeUtility.createChallengeDto())); } @Test