Quellcode durchsuchen

增加批量保存

zhou-hao vor 5 Jahren
Ursprung
Commit
3712ca4b20

+ 27 - 10
hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/reactive/ReactiveSaveController.java

@@ -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);
+
     }
 }

+ 27 - 10
hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/reactive/ReactiveServiceSaveController.java

@@ -1,5 +1,6 @@
 package org.hswebframework.web.crud.web.reactive;
 
+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;
@@ -7,6 +8,7 @@ import org.hswebframework.web.authorization.annotation.Authorize;
 import org.hswebframework.web.authorization.annotation.SaveAction;
 import org.hswebframework.web.crud.service.ReactiveCrudService;
 import org.springframework.web.bind.annotation.*;
+import reactor.core.publisher.Flux;
 import reactor.core.publisher.Mono;
 
 public interface ReactiveServiceSaveController<E,K>  {
@@ -33,7 +35,7 @@ public interface ReactiveServiceSaveController<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);
         }
@@ -45,28 +47,43 @@ public interface ReactiveServiceSaveController<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 -> getService().save(Mono.just(entity)).thenReturn(entity));
+                .as(getService()::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(getService()::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 -> getService().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 -> getService().updateById(id,Mono.just(entity)).thenReturn(entity));
+                .flatMap(entity -> getService().updateById(id, Mono.just(entity)))
+                .thenReturn(true);
+
     }
 }