I am migrating to the reactive stack and I am having difficulties working with the reactive approach.
The task is to get data from a remote service in the Flux<GroupDTO>
format, convert each GroupDTO
and collect them into a List<Community>
, then create a Communities
object based on a previously collected list and save this data to a Postgresql reactive database. I ask for help.
GroupController
:
@GetMapping("/groups")
public Flux<ResponseEntity<GroupDTO>> getGroups(@RequestParam("q") @Max(value = 128) String q) {
Flux<GroupDTO> flux = groupService.getGroups(q); // getting data from remote service
// how to convert and collect all groups to reactive database entity and save to reactive database(postgresql) here?
return flux.map(ResponseEntity::ok)
.defaultIfEmpty(ResponseEntity.notFound().build());;
}
GroupDTO
:
@Jacksonized
@Builder
@Value
@JsonIgnoreProperties(ignoreUnknown = true)
public class GroupDTO {
int id;
@JsonProperty("is_member")
boolean member;
}
Class Communities
with all converted and collected from Flux<GroupDTO>
:
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED, force = true)
public class Communities {
@Id
private Long id;
private List<Community> elements;
private String q;
//@CreationTimestamp
@JsonSerialize(using = CustomTimestampSerializer.class)
private Timestamp timestamp;
public Communities(String q, List<Community> elements) {
this.q = q;
this.elements = elements;
}
}
Each GroupDTO
may convert to Community
:
@Getter
@NoArgsConstructor
public class Community {
@Id
private Long id;
private Long groupId;
public Community(Long groupId) {
this.groupId = groupId;
}
}
Reactive repositories
:
@Repository
public interface CommunitiesRepository extends ReactiveSortingRepository<Communities, Long> {}
@Repository
public interface CommunityRepository extends ReactiveSortingRepository<Community, Long> {}
The names of the Community and Communities database models were chosen as a result of the fact that earlier when working with JPA Hibernate (Postgresql) errors occurred due to the keywords "Group" / "Groups". If there is a way, then I would be happy to fix it in the current migration.
If there are errors in the design, I will be glad to hear tips on how to fix them.