feat:实现全异步改造

This commit is contained in:
wangyu 2021-12-07 22:46:21 +08:00
parent a5ffeae42f
commit 6d088e20a9
2 changed files with 0 additions and 225 deletions

View File

@ -1,125 +0,0 @@
package com.flyfish.framework.controller.authorized;
import com.flyfish.framework.annotations.Operation;
import com.flyfish.framework.bean.Result;
import com.flyfish.framework.bean.SyncVo;
import com.flyfish.framework.configuration.annotations.CurrentUser;
import com.flyfish.framework.configuration.annotations.PagedQuery;
import com.flyfish.framework.configuration.annotations.ValidRequestBody;
import com.flyfish.framework.constant.ReactiveConstants;
import com.flyfish.framework.context.UserContext;
import com.flyfish.framework.controller.SafeController;
import com.flyfish.framework.domain.authorized.AuthorizedDomain;
import com.flyfish.framework.domain.authorized.AuthorizedQo;
import com.flyfish.framework.domain.po.User;
import com.flyfish.framework.service.BaseReactiveService;
import com.flyfish.framework.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;
import java.util.List;
/**
* 通用RESTful的控制器用于动态提供基础的RESTful接口
*
* @author wangyu
* @create 2017-06-15 8:48
*/
public abstract class AuthorizedController<T extends AuthorizedDomain, Q extends AuthorizedQo<T>> implements SafeController {
@Autowired
protected BaseService<T> service;
@Autowired(required = false)
protected BaseReactiveService<T> reactiveService;
@Autowired
protected UserContext userContext;
@SuppressWarnings("unchecked")
public <S extends BaseService<T>> S getService() {
return (S) service;
}
@GetMapping("/exists")
public Result<Boolean> exists(@PagedQuery Q qo) {
return Result.accept(service.count(qo) != 0);
}
@GetMapping("/count")
public Result<Long> count(@PagedQuery Q qo) {
return Result.accept(service.count(qo));
}
@PostMapping("")
@Operation.Create
public Result<T> create(@ValidRequestBody T entity, @CurrentUser User user) {
userContext.setUser(user);
return Result.accept(service.create(entity));
}
@GetMapping("{id}")
public Result<T> get(@PathVariable String id) {
return service.getById(id).map(Result::accept).orElse(Result.notFound());
}
@PutMapping("{id}")
@Operation.Update
public Result<T> update(@ValidRequestBody T entity, @CurrentUser User user) {
userContext.setUser(user);
return Result.accept(service.updateSelectiveById(entity));
}
@PatchMapping("{id}")
@Operation.Update
public Result<T> patch(@RequestBody T entity, @CurrentUser User user) {
userContext.setUser(user);
return Result.accept(service.updateSelectiveById(entity));
}
@PutMapping("")
@Operation.UpdateAll
public Result<List<T>> updateList(@RequestBody List<T> list, @CurrentUser User user) {
userContext.setUser(user);
return Result.accept(service.updateBatch(list));
}
@PutMapping("/sync")
@Operation.Sync
public Result<SyncVo<T>> syncList(@RequestBody List<T> list, @CurrentUser User user) {
userContext.setUser(user);
return Result.accept(service.sync(list));
}
@DeleteMapping("{id}")
@Operation.Delete
public Result<T> remove(@PathVariable List<String> id, @CurrentUser User user) {
userContext.setUser(user);
service.deleteBatchByIds(id);
return Result.ok();
}
@GetMapping("/all")
public Result<List<T>> all(@PagedQuery Q qo) {
if (qo.isEmpty()) {
return Result.accept(service.getAll());
}
return Result.accept(service.getList(qo));
}
@GetMapping("")
public Result<List<T>> list(@PagedQuery Q qo) {
if (null != qo.getPageable()) {
return Result.accept(service.getPageList(qo));
}
return Result.accept(service.getList(qo));
}
@PutMapping(value = "{id}", headers = ReactiveConstants.USE_REACTIVE)
public Mono<Result<T>> reactiveUpdate(@ValidRequestBody T entity) {
return reactiveService.updateById(entity)
.map(Result::accept)
.defaultIfEmpty(Result.notFound());
}
}

View File

@ -1,100 +0,0 @@
package com.flyfish.framework.controller.authorized;
import com.flyfish.framework.annotations.Operation;
import com.flyfish.framework.bean.Result;
import com.flyfish.framework.bean.SyncVo;
import com.flyfish.framework.configuration.annotations.PagedQuery;
import com.flyfish.framework.configuration.annotations.ValidRequestBody;
import com.flyfish.framework.controller.SafeController;
import com.flyfish.framework.domain.authorized.AuthorizedDomain;
import com.flyfish.framework.domain.authorized.AuthorizedQo;
import com.flyfish.framework.service.BaseReactiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.util.CastUtils;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;
import java.util.List;
/**
* 多租户带授权的控制器
*
* @param <T> 实体泛型
* @param <Q> 查询实体泛型
*/
public abstract class ReactiveAuthorizedController<T extends AuthorizedDomain, Q extends AuthorizedQo<T>> implements SafeController {
@Autowired
protected BaseReactiveService<T> reactiveService;
public <S extends BaseReactiveService<T>> S getService() {
return CastUtils.cast(reactiveService);
}
@GetMapping("/exists")
public Mono<Result<Boolean>> exists(@PagedQuery Q qo) {
return reactiveService.exists(qo).map(Result::accept);
}
@GetMapping("/count")
public Mono<Result<Long>> count(@PagedQuery Q qo) {
return reactiveService.count(qo).map(Result::accept);
}
@PostMapping("")
@Operation.Create
public Mono<Result<T>> create(@ValidRequestBody T entity) {
return reactiveService.create(entity).map(Result::accept);
}
@GetMapping("{id}")
public Mono<Result<T>> get(@PathVariable String id) {
return reactiveService.getById(id).map(Result::accept).defaultIfEmpty(Result.notFound());
}
@PutMapping("{id}")
@Operation.Update
public Mono<Result<T>> update(@ValidRequestBody T entity) {
return reactiveService.updateSelectiveById(entity).map(Result::accept).defaultIfEmpty(Result.notFound());
}
@PatchMapping("{id}")
@Operation.Update
public Mono<Result<T>> patch(@RequestBody T entity) {
return reactiveService.updateSelectiveById(entity).map(Result::accept).defaultIfEmpty(Result.notFound());
}
@PutMapping("")
@Operation.UpdateAll
public Mono<Result<List<T>>> updateList(@RequestBody List<T> list) {
return reactiveService.updateBatch(list).collectList().map(Result::accept).defaultIfEmpty(Result.emptyList());
}
@PutMapping("/sync")
@Operation.Sync
public Mono<Result<SyncVo<T>>> syncList(@RequestBody List<T> list) {
return reactiveService.sync(list).map(Result::accept).defaultIfEmpty(Result.accept(SyncVo.<T>builder().build()));
}
@DeleteMapping("{id}")
@Operation.Delete
public Mono<Result<T>> remove(@PathVariable List<String> id) {
return reactiveService.deleteBatchByIds(id).thenReturn(Result.ok());
}
@GetMapping("/all")
public Mono<Result<List<T>>> all(@PagedQuery Q qo) {
if (qo.isEmpty()) {
return reactiveService.getAll().collectList().map(Result::accept).defaultIfEmpty(Result.emptyList());
}
return reactiveService.getList(qo).collectList().map(Result::accept).defaultIfEmpty(Result.emptyList());
}
@GetMapping("")
public Mono<Result<List<T>>> list(@PagedQuery Q qo) {
if (null != qo.getPageable() && qo.getPageable().isPaged()) {
return reactiveService.getPageList(qo).map(Result::accept).defaultIfEmpty(Result.emptyPage());
}
return reactiveService.getList(qo).collectList().map(Result::accept).defaultIfEmpty(Result.emptyList());
}
}