|
@@ -1,6 +1,7 @@
|
|
|
package org.hswebframework.web.crud.web.reactive;
|
|
|
|
|
|
import org.hswebframework.ezorm.rdb.mapping.ReactiveRepository;
|
|
|
+import org.hswebframework.ezorm.rdb.mapping.defaults.SaveResult;
|
|
|
import org.hswebframework.web.api.crud.entity.RecordCreationEntity;
|
|
|
import org.hswebframework.web.api.crud.entity.RecordModifierEntity;
|
|
|
import org.hswebframework.web.authorization.Authentication;
|
|
@@ -8,6 +9,7 @@ import org.hswebframework.web.authorization.Permission;
|
|
|
import org.hswebframework.web.authorization.annotation.Authorize;
|
|
|
import org.hswebframework.web.authorization.annotation.SaveAction;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
+import reactor.core.publisher.Flux;
|
|
|
import reactor.core.publisher.Mono;
|
|
|
|
|
|
import javax.validation.Valid;
|
|
@@ -36,7 +38,7 @@ public interface ReactiveSaveController<E, K> {
|
|
|
}
|
|
|
|
|
|
@Authorize(ignore = true)
|
|
|
- default E applyAuthentication(Authentication authentication, E entity) {
|
|
|
+ default E applyAuthentication(E entity, Authentication authentication) {
|
|
|
if (entity instanceof RecordCreationEntity) {
|
|
|
entity = applyCreationEntity(authentication, entity);
|
|
|
}
|
|
@@ -48,28 +50,43 @@ public interface ReactiveSaveController<E, K> {
|
|
|
|
|
|
@PatchMapping
|
|
|
@SaveAction
|
|
|
- default Mono<E> save(@RequestBody Mono<E> payload) {
|
|
|
+ default Mono<SaveResult> save(@RequestBody Flux<E> payload) {
|
|
|
return Authentication.currentReactive()
|
|
|
- .zipWith(payload, this::applyAuthentication)
|
|
|
+ .flatMapMany(auth -> payload.map(entity -> applyAuthentication(entity, auth)))
|
|
|
.switchIfEmpty(payload)
|
|
|
- .flatMap(entity -> getRepository().save(Mono.just(entity)).thenReturn(entity));
|
|
|
+ .as(getRepository()::save);
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("/batch")
|
|
|
+ @SaveAction
|
|
|
+ default Mono<Integer> add(@RequestBody Flux<E> payload) {
|
|
|
+
|
|
|
+ return Authentication.currentReactive()
|
|
|
+ .flatMapMany(auth -> payload.map(entity -> applyAuthentication(entity, auth)))
|
|
|
+ .switchIfEmpty(payload)
|
|
|
+ .collectList()
|
|
|
+ .as(getRepository()::insertBatch);
|
|
|
}
|
|
|
|
|
|
@PostMapping
|
|
|
@SaveAction
|
|
|
default Mono<E> add(@RequestBody Mono<E> payload) {
|
|
|
- return Authentication.currentReactive()
|
|
|
- .zipWith(payload, this::applyAuthentication)
|
|
|
+ return Authentication.currentReactive()
|
|
|
+ .flatMap(auth -> payload.map(entity -> applyAuthentication(entity, auth)))
|
|
|
.switchIfEmpty(payload)
|
|
|
.flatMap(entity -> getRepository().insert(Mono.just(entity)).thenReturn(entity));
|
|
|
}
|
|
|
|
|
|
+
|
|
|
@PutMapping("/{id}")
|
|
|
@SaveAction
|
|
|
- default Mono<E> update(@PathVariable K id, @RequestBody Mono<E> payload) {
|
|
|
- return Authentication.currentReactive()
|
|
|
- .zipWith(payload, this::applyAuthentication)
|
|
|
+ default Mono<Boolean> update(@PathVariable K id, @RequestBody Mono<E> payload) {
|
|
|
+
|
|
|
+ return Authentication.currentReactive()
|
|
|
+ .flatMap(auth -> payload.map(entity -> applyAuthentication(entity, auth)))
|
|
|
.switchIfEmpty(payload)
|
|
|
- .flatMap(entity -> getRepository().updateById(id,Mono.just(entity)).thenReturn(entity));
|
|
|
+ .flatMap(entity -> getRepository().updateById(id, Mono.just(entity)))
|
|
|
+ .thenReturn(true);
|
|
|
+
|
|
|
}
|
|
|
}
|