Skip to content
Open
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
Expand Up @@ -83,4 +83,16 @@ class FeatureFlagService(

return featureFlagRepository.save(flag)
}

@Transactional
fun update(
user: User,
key: String,
enabled: Boolean,
) {
val flag = getFlagOrThrow(key)
flag.enabled = enabled
flag.updatedBy = user
featureFlagRepository.save(flag)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class FeatureFlagController(
@PutMapping("/{key}")
fun updateFlag(
@PathVariable key: String,
@RequestBody request: UpdateFeatureFlagRequest,
@RequestBody @Valid request: UpdateFeatureFlagRequest,
): PayloadResponse<FeatureFlagResponse> {
// TODO: Improve the way finding the authenticated user.
val authentication = SecurityContextHolder.getContext().authentication
Expand All @@ -104,13 +104,15 @@ class FeatureFlagController(
@PatchMapping("/{key}")
fun updateFlagStatus(
@PathVariable key: String,
@RequestParam status: String,
): PayloadResponse<FeatureFlagResponse> {
return PayloadResponse<FeatureFlagResponse>(
status = "status",
message = "message",
data = null,
)
@RequestParam status: Boolean,
): StatusResponse {
// TODO: Improve the way finding the authenticated user.
val authentication = SecurityContextHolder.getContext().authentication
val userId = authentication.name
val user = userRepository.findByIdOrNull(userId.toLong()) ?: throw BusinessException("User not found")

featureFlagService.update(user, key, status)
return StatusResponse.success("Successfully updated the status")
}

@Operation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ class GlobalExceptionHandler {
@ExceptionHandler(MethodArgumentNotValidException::class)
fun handleMethodArgumentNotValidException(ex: MethodArgumentNotValidException): ResponseEntity<StatusResponse> {
log.error(ex.message, ex)
val errorMessages = ex.bindingResult.fieldErrors.joinToString(", ") { it.defaultMessage.toString() }
return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.body(
StatusResponse(
status = HttpStatus.BAD_REQUEST.name,
message = ex.message,
message = errorMessages,
),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,10 @@ package com.lightswitch.presentation.model
data class StatusResponse(
val status: String,
val message: String,
)
) {
companion object {
fun success(message: String): StatusResponse {
return StatusResponse("Success", message)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,40 @@ class FeatureFlagServiceTest : BaseRepositoryTest() {
.hasMessageContaining("Unsupported type: json")
}

@Test
fun `update should update feature flag status`() {
val user = saveTestUser()
val flag =
featureFlagRepository.save(
FeatureFlag(
name = "test-flag",
description = "description",
type = Type.BOOLEAN,
enabled = false,
createdBy = user,
updatedBy = user,
),
)

featureFlagService.update(user, "test-flag", true)

val updatedFlag = featureFlagRepository.findById(flag.id!!.toInt()).orElseThrow()

assertThat(updatedFlag.enabled).isTrue()
assertThat(updatedFlag.updatedBy).isEqualTo(user)
}

@Test
fun `update should throw EntityNotFoundException when key does not exist`() {
val user = saveTestUser()

assertThatThrownBy {
featureFlagService.update(user, "flag-one", true)
}
.isInstanceOf(EntityNotFoundException::class.java)
.hasMessageContaining("Feature flag flag-one does not exist")
}

private fun saveTestUser() =
userRepository.save(
User(
Expand Down
Loading