Delete Operation is not working. How can i fetch data and then delete it in a functional way? The code successfully run but it is not deleting data in database.
Controller Class
@DeleteMapping("/deleteUserById/{userId}")
public Mono<ResponseEntity<String>> deleteUserById(@PathVariable Long userId) {
return userService.deleteUserById(userId);
}
UserService Class
@Override
public Mono<ResponseEntity<String>> deleteUserById(Long userId) {
return userRepository.findById(userId)
.map(user -> {
UserDto deletedUser = UserMapper.USER_MAPPER_INSTANCE.toUserDto(user);
return "User: " + deletedUser.getUsername() + " deleted!";
})
.doOnNext(user -> {
userRepository.deleteUserById(userId);
})
.map(ResponseEntity::ok)
.defaultIfEmpty(ResponseEntity
.status(HttpStatus.NOT_FOUND)
.body("UserId: " + userId + " Not found"));
}
Repostiory Interface
@EnableR2dbcRepositories
public interface UserRepository extends ReactiveCrudRepository<User, Long> {
@Query("SELECT * FROM tb_user WHERE username LIKE $1")
Mono<User> findByUsername(String username);
@Query("DELETE FROM tb_user WHERE userid = $1")
Mono<User> deleteUserById(Long userId);
}
Postgres Table
alertsystem=# select * from tb_user;
userid | username | name | email | password | ph_number | date_of_birth | role
——–+———-+—————+—————————-+———-+————+—————+——-
5 | am0007 | Ajay Maharjan | [email protected] | asdfgh1 | 558955697 | 1999-05-21 | ADMIN
Output
case: not available userId
UserId: 3 Not found
case: available userId
User: am0007 deleted!
I’m trying to delete data in a database in a reactive way. I’m getting expected output but delete operation is not working.
2
Answers
deleteUserById()
was returningmono<Void>
despite declaringMono<User>
. So what I did was, I changed it intoMono<Void>
and then added.thenReturn(user)
which returnedMono<User>
from the flatMap.This is because
deleteUserById()
is a reactive operation. It returnsMono
, but you call it withindoOnNext()
method, so nobody never subsribes to it. Remember the rule: nothing happens until you subscribeThe thing is that
doOnNext()
is meant just to put some side effect, for example, to log something.You should call your delete method inside
flatMap()
operatorSo downstream subsribes to
Mono
returned from that flatMap