Skip to content

Commit

Permalink
BE: Chore: Use dto builders in controller package (#504)
Browse files Browse the repository at this point in the history
Co-authored-by: Roman Zabaluev <[email protected]>
  • Loading branch information
wernerdv and Haarolean authored Aug 7, 2024
1 parent 0531186 commit 2bb9d5c
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 33 deletions.
39 changes: 20 additions & 19 deletions api/src/main/java/io/kafbat/ui/controller/AccessController.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,33 +45,34 @@ public Mono<ResponseEntity<AuthenticationInfoDTO>> getUserAuthInfo(ServerWebExch
.map(SecurityContext::getAuthentication)
.map(Principal::getName);

var builder = AuthenticationInfoDTO.builder()
.rbacEnabled(accessControlService.isRbacEnabled());

return userName
.zipWith(permissions)
.map(data -> {
var dto = new AuthenticationInfoDTO(accessControlService.isRbacEnabled());
dto.setUserInfo(new UserInfoDTO(data.getT1(), data.getT2()));
return dto;
})
.switchIfEmpty(Mono.just(new AuthenticationInfoDTO(accessControlService.isRbacEnabled())))
.map(data -> (AuthenticationInfoDTO) builder
.userInfo(new UserInfoDTO(data.getT1(), data.getT2()))
.build()
)
.switchIfEmpty(Mono.just(builder.build()))
.map(ResponseEntity::ok);
}

private List<UserPermissionDTO> mapPermissions(List<Permission> permissions, List<String> clusters) {
return permissions
.stream()
.map(permission -> {
UserPermissionDTO dto = new UserPermissionDTO();
dto.setClusters(clusters);
dto.setResource(ResourceTypeDTO.fromValue(permission.getResource().toString().toUpperCase()));
dto.setValue(permission.getValue());
dto.setActions(permission.getParsedActions()
.stream()
.map(p -> p.name().toUpperCase())
.map(this::mapAction)
.filter(Objects::nonNull)
.toList());
return dto;
})
.map(permission -> (UserPermissionDTO) UserPermissionDTO.builder()
.clusters(clusters)
.resource(ResourceTypeDTO.fromValue(permission.getResource().toString().toUpperCase()))
.value(permission.getValue())
.actions(permission.getParsedActions()
.stream()
.map(p -> p.name().toUpperCase())
.map(this::mapAction)
.filter(Objects::nonNull)
.toList())
.build()
)
.toList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public Mono<ResponseEntity<KsqlCommandV2ResponseDTO>> executeKsql(String cluster
}

@Override
@SuppressWarnings("unchecked")
public Mono<ResponseEntity<Flux<KsqlResponseDTO>>> openKsqlResponsePipe(String clusterName,
String pipeId,
ServerWebExchange exchange) {
Expand Down
20 changes: 7 additions & 13 deletions api/src/main/java/io/kafbat/ui/controller/TopicsController.java
Original file line number Diff line number Diff line change
Expand Up @@ -350,18 +350,12 @@ private Comparator<InternalTopic> getComparatorForTopic(
if (orderBy == null) {
return defaultComparator;
}
switch (orderBy) {
case TOTAL_PARTITIONS:
return Comparator.comparing(InternalTopic::getPartitionCount);
case OUT_OF_SYNC_REPLICAS:
return Comparator.comparing(t -> t.getReplicas() - t.getInSyncReplicas());
case REPLICATION_FACTOR:
return Comparator.comparing(InternalTopic::getReplicationFactor);
case SIZE:
return Comparator.comparing(InternalTopic::getSegmentSize);
case NAME:
default:
return defaultComparator;
}
return switch (orderBy) {
case TOTAL_PARTITIONS -> Comparator.comparing(InternalTopic::getPartitionCount);
case OUT_OF_SYNC_REPLICAS -> Comparator.comparing(t -> t.getReplicas() - t.getInSyncReplicas());
case REPLICATION_FACTOR -> Comparator.comparing(InternalTopic::getReplicationFactor);
case SIZE -> Comparator.comparing(InternalTopic::getSegmentSize);
default -> defaultComparator;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ void testBase64DecodingWorks() {
}

private TopicMessageDTO msg() {
return new TopicMessageDTO(1, -1L, OffsetDateTime.now());
return TopicMessageDTO.builder()
.partition(1)
.offset(-1L)
.timestamp(OffsetDateTime.now())
.build();
}
}
11 changes: 11 additions & 0 deletions contract/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -100,6 +105,12 @@
<useTags>true</useTags>
<useSpringBoot3>true</useSpringBoot3>
<dateLibrary>java8</dateLibrary>
<generatedConstructorWithRequiredArgs>false</generatedConstructorWithRequiredArgs>
<additionalModelTypeAnnotations>
@lombok.experimental.SuperBuilder
@lombok.NoArgsConstructor
@lombok.AllArgsConstructor
</additionalModelTypeAnnotations>
</configOptions>
</configuration>
</execution>
Expand Down

0 comments on commit 2bb9d5c

Please sign in to comment.