Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -25,12 +26,15 @@ class RestExceptionHandler : ResponseEntityExceptionHandler() {
status: HttpStatusCode,
request: WebRequest
): ResponseEntity<Any>? {
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"))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down