6 Ocak 2021 Çarşamba

SpringData MongoDB ReactiveMongoOperations

Giriş
Şu satırı dahil ederiz
import org.springframework.data.mongodb.core.ReactiveMongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
findById metodu
Şöyle yaparız
@Service
class ReservationService {

  private final ReactiveMongoOperations reactiveMongoOperations ;

  @Autowired
  public ReservationServiceImpl(ReactiveMongoOperations reactiveMongoOperations ) {
    this.reactiveMongoOperations = reactiveMongoOperations ;
  }

  public Mono<Reservation> getReservation(String id) {
    return reactiveMongoOperations
      .findById(id, Reservation.class);
  }

  public Mono<Reservation> createReservation(Mono<Reservation> reservation) {
    return reactiveMongoOperations
      .save(reservation);
  }

  public Mono<Reservation> updateReservation(String id, Mono<Reservation> reservation) {
    return reservation
      .flatMap(res -> reactiveMongoOperations .
        findAndModify(Query.query(Criteria.where("id").is(id)),
                      Update.update("price", res.getPrice()), Reservation.class)
                      .flatMap(result -> {
                        result.setPrice(res.getPrice());
                        return Mono.just(result);
    }));
  }

  public Mono<Boolean> deleteReservation(String id) {
    return reactiveMongoOperations .remove(Query
      .query(Criteria.where("id").is(id)), Reservation.class)
      .flatMap(deleteResult -> Mono.just(deleteResult.wasAcknowledged()));
  }

  public Flux<Reservation> listAllReservations() {
    return reactiveMongoOperations .findAll(Reservation.class);
  }
}

Hiç yorum yok:

Yorum Gönder