Joël
Joël

Reputation: 13

Use Value of Result Mono from DB Request in Another Request

I want to get a user by his username, assign this user as the author of a note and then save that note to the database. The problem is that I get a Mono with a user in it and I can't assign that to the author field of type user.

What I'm trying to do:

noteRepository
    .save(noteMapper
        .fromDTO(noteDTO)
        .apply { owner = userReository
            .findByUsername(userPrincipalService.getCurrentUserPrincipal()
                .map { it.username }) 
        })

Upvotes: 1

Views: 313

Answers (1)

Phil Clay
Phil Clay

Reputation: 4554

userRepository
        // Find the User
        .findByUsername(userPrincipalService.getCurrentUserPrincipal()
                .map { it.username })
        // Map the User to a Note, while setting the note's owner 
        .map { user ->
            noteMapper.fromDTO(noteDTO).apply {
                owner = user
            }
        }
        // Save the Note 
        .flatMap { note -> noteRepository.save(note) }

Upvotes: 1

Related Questions