diff --git a/server/src/main/kotlin/delta/codecharacter/server/exception/RestExceptionHandler.kt b/server/src/main/kotlin/delta/codecharacter/server/exception/RestExceptionHandler.kt index a115f8e2..4a2066ac 100644 --- a/server/src/main/kotlin/delta/codecharacter/server/exception/RestExceptionHandler.kt +++ b/server/src/main/kotlin/delta/codecharacter/server/exception/RestExceptionHandler.kt @@ -1,5 +1,6 @@ package delta.codecharacter.server.exception +import com.fasterxml.jackson.databind.exc.InvalidFormatException import com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException import jakarta.validation.ConstraintViolationException import org.springframework.core.Ordered @@ -25,12 +26,15 @@ class RestExceptionHandler : ResponseEntityExceptionHandler() { status: HttpStatusCode, request: WebRequest ): ResponseEntity? { - val cause = ex.cause - return if (cause is MissingKotlinParameterException) { - ResponseEntity.status(HttpStatus.BAD_REQUEST) - .body(mapOf("message" to "${cause.parameter.name} is missing")) - } else { - ResponseEntity.status(HttpStatus.BAD_REQUEST).body(mapOf("message" to "Unknown error")) + return when (val cause = ex.cause) { + is MissingKotlinParameterException -> + ResponseEntity.status(HttpStatus.BAD_REQUEST) + .body(mapOf("message" to "${cause.parameter.name} is missing")) + is InvalidFormatException -> + ResponseEntity.status(HttpStatus.BAD_REQUEST) + .body(mapOf("message" to "${cause.value} is of Invalid Format")) + else -> + ResponseEntity.status(HttpStatus.BAD_REQUEST).body(mapOf("message" to "Unknown Error")) } } diff --git a/server/src/main/kotlin/delta/codecharacter/server/seeders/DailyChallengeSeeder.kt b/server/src/main/kotlin/delta/codecharacter/server/seeders/DailyChallengeSeeder.kt index 54a7affc..3b4a3390 100644 --- a/server/src/main/kotlin/delta/codecharacter/server/seeders/DailyChallengeSeeder.kt +++ b/server/src/main/kotlin/delta/codecharacter/server/seeders/DailyChallengeSeeder.kt @@ -19,7 +19,7 @@ class DailyChallengeSeeder { private val logger: Logger = LoggerFactory.getLogger(DailyChallengeSeeder::class.java) @EventListener(ApplicationReadyEvent::class) - fun doSomethingAfterStartup() { + fun seedDailyChallenges() { if (dailyChallengeRepository.findAll().isEmpty()) { logger.info("Seeding daily_challenges") @@ -44,6 +44,7 @@ class DailyChallengeSeeder { ) } dailyChallengeRepository.saveAll(dcEntities) + logger.info("Seeding Daily-Challenges Completed") } else { logger.error("dcConstants.json is empty or doesn't exist") } diff --git a/server/src/main/kotlin/delta/codecharacter/server/user/UserService.kt b/server/src/main/kotlin/delta/codecharacter/server/user/UserService.kt index b93d8fd2..0228c3b8 100644 --- a/server/src/main/kotlin/delta/codecharacter/server/user/UserService.kt +++ b/server/src/main/kotlin/delta/codecharacter/server/user/UserService.kt @@ -129,6 +129,21 @@ class UserService( ) = registerUserRequestDto + if (username.trim().length < 5) { + throw CustomException(HttpStatus.BAD_REQUEST, "Username must be minimum 5 characters long") + } + if (name.trim().length < 5) { + throw CustomException(HttpStatus.BAD_REQUEST, "Name must be minimum 5 characters long") + } + if (avatarId !in 0..19) { + throw CustomException(HttpStatus.BAD_REQUEST, "Selected Avatar is invalid") + } + if (college.trim().isEmpty()) { + throw CustomException(HttpStatus.BAD_REQUEST, "College can not be empty") + } + if (country.trim().isEmpty()) { + throw CustomException(HttpStatus.BAD_REQUEST, "Country can not be empty") + } if (password != passwordConfirmation) { throw CustomException( HttpStatus.BAD_REQUEST, "Password and password confirmation don't match" diff --git a/server/src/main/kotlin/delta/codecharacter/server/user/public_user/PublicUserService.kt b/server/src/main/kotlin/delta/codecharacter/server/user/public_user/PublicUserService.kt index cecbf277..ccca2e10 100644 --- a/server/src/main/kotlin/delta/codecharacter/server/user/public_user/PublicUserService.kt +++ b/server/src/main/kotlin/delta/codecharacter/server/user/public_user/PublicUserService.kt @@ -101,11 +101,36 @@ class PublicUserService(@Autowired private val publicUserRepository: PublicUserR fun updateUserProfile(userId: UUID, updateCurrentUserProfileDto: UpdateCurrentUserProfileDto) { val user = publicUserRepository.findById(userId).get() + + if (updateCurrentUserProfileDto.name != null && + updateCurrentUserProfileDto.name!!.trim().length < 5 + ) { + throw CustomException(HttpStatus.BAD_REQUEST, "Name must be minimum 5 characters") + } + + if (updateCurrentUserProfileDto.country != null && + updateCurrentUserProfileDto.country!!.trim().isEmpty() + ) { + throw CustomException(HttpStatus.BAD_REQUEST, "Country can not be an empty") + } + + if (updateCurrentUserProfileDto.college != null && + updateCurrentUserProfileDto.college!!.trim().isEmpty() + ) { + throw CustomException(HttpStatus.BAD_REQUEST, "College can not be an empty") + } + if (updateCurrentUserProfileDto.avatarId != null && + updateCurrentUserProfileDto.avatarId!! !in 0..19 + ) { + throw CustomException(HttpStatus.BAD_REQUEST, "Selected Avatar is invalid") + } + val updatedUser = user.copy( name = updateCurrentUserProfileDto.name ?: user.name, country = updateCurrentUserProfileDto.country ?: user.country, college = updateCurrentUserProfileDto.college ?: user.college, + avatarId = updateCurrentUserProfileDto.avatarId ?: user.avatarId, tutorialLevel = updateTutorialLevel( updateCurrentUserProfileDto.updateTutorialLevel, user.tutorialLevel