top.zenyoung.controller.BaseAuthController Maven / Gradle / Ivy
package top.zenyoung.controller;
import reactor.core.publisher.Mono;
import top.zenyoung.common.model.*;
import top.zenyoung.common.paging.PagingQuery;
import top.zenyoung.common.paging.PagingResult;
import top.zenyoung.controller.listener.AuthProccessListener;
import top.zenyoung.controller.listener.ExceptHandlerListener;
import top.zenyoung.controller.listener.PreHandlerListener;
import top.zenyoung.controller.listener.ProccessListener;
import top.zenyoung.controller.model.ExceptHandler;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.Serializable;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
/**
* 认证用户-控制器-基类
*
* @author yangyong
* @version 1.0
* date 2020/7/8 2:32 下午
**/
public abstract class BaseAuthController extends BaseController {
/**
* 认证用户数据转换
*
* @param principal 认证用户
* @return 转换数据
*/
@Nonnull
protected abstract A convert(@Nonnull final UserPrincipal principal);
/**
* 检查认证用户
*
* @param principal 认证用户
*/
protected abstract void checkAuth(@Nonnull final A principal);
/**
* 查询数据
*
* @param principal 认证用户
* @param queryHandler 查询处理器
* @param convertHandler 查询结果转换处理器
* @param - 查询数据类型
* @param
结果数据类型
* @return 查询结果
*/
protected - Mono
> buildQuery(
@Nonnull final UserPrincipal principal,
@Nonnull final Function> queryHandler,
@Nonnull final Function- convertHandler
) {
return buildQuery(() -> {
final A auth = convert(principal);
checkAuth(auth);
return queryHandler.apply(auth);
}, convertHandler);
}
/**
* 分页查询数据
*
* @param principal 认证用户
* @param reqQuery 分页查询条件
* @param queryConvertHandler 查询条件转换处理
* @param pagingQueryHandler 分页查询处理
* @param resultConvertHandler 查询结果转换处理
* @param
请求查询条件类型
* @param 转换后查询条件类型
* @param - 查询数据类型
* @param
结果数据类型
* @return 查询结果
*/
protected Mono> buildQuery(
@Nonnull final UserPrincipal principal,
@Nonnull final PagingQuery reqQuery,
@Nonnull final BiFunction queryConvertHandler,
@Nonnull final Function, PagingResult- > pagingQueryHandler,
@Nonnull final Function
- resultConvertHandler
) {
return buildQuery(reqQuery, reqQry -> {
final A auth = convert(principal);
checkAuth(auth);
return queryConvertHandler.apply(auth, reqQry);
}, pagingQueryHandler, resultConvertHandler);
}
/**
* 业务处理-无入参验证
*
* @param principal 认证用户
* @param process 处理器
* @param
返回数据类型
* @return 处理结果
*/
protected Mono> action(
@Nonnull final UserPrincipal principal,
@Nonnull final AuthProccessListener process
) {
return action(new ProccessListener() {
@Override
public void getExceptHandlers(@Nonnull final List handlers) {
process.getExceptHandlers(handlers);
}
@Override
public void preHandler(@Nullable final Void reqData) {
process.preHandler(reqData);
}
@Override
public R apply(final Void aVoid) {
final A auth = convert(principal);
checkAuth(auth);
return process.apply(auth, aVoid);
}
});
}
/**
* 业务处理
*
* @param principal 认证用户
* @param req 请求数据
* @param process 处理器
* @param 请求数据类型
* @param 响应数据类型
* @return 处理结果
*/
protected Mono> action(
@Nonnull final UserPrincipal principal,
@Nonnull final Mono req,
@Nonnull final AuthProccessListener process
) {
return action(req, new ProccessListener() {
@Override
public void getExceptHandlers(@Nonnull final List handlers) {
process.getExceptHandlers(handlers);
}
@Override
public void preHandler(@Nullable final T reqData) {
process.preHandler(reqData);
}
@Override
public R apply(final T data) {
final A auth = convert(principal);
checkAuth(auth);
return process.apply(auth, data);
}
});
}
/**
* 业务处理-新增
*
* @param principal 认证用户
* @param req 请求数据
* @param process 处理器
* @param 请求数据类型
* @return 处理结果
*/
protected Mono actionAdd(
@Nonnull final UserPrincipal principal,
@Nonnull final Mono req,
@Nonnull final AuthProccessListener process
) {
return actionAdd(req, new ProccessListener() {
@Override
public void getExceptHandlers(@Nonnull final List handlers) {
process.getExceptHandlers(handlers);
}
@Override
public void preHandler(@Nullable final T reqData) {
process.preHandler(reqData);
}
@Override
public String apply(final T data) {
final A auth = convert(principal);
checkAuth(auth);
return process.apply(auth, data);
}
});
}
/**
* 业务处理-修改
*
* @param principal 认证用户
* @param req 请求数据
* @param process 处理器
* @param 请求数据类型
* @return 处理结果
*/
protected Mono actionModify(
@Nonnull final UserPrincipal principal,
@Nonnull final Mono req,
@Nonnull final AuthProccessModifyListener process
) {
return actionModify(req, new ProccessModifyListener() {
@Override
public void getExceptHandlers(@Nonnull final List handlers) {
process.getExceptHandlers(handlers);
}
@Override
public void preHandler(@Nullable final T reqData) {
process.preHandler(reqData);
}
@Override
public void accept(final T data) {
final A auth = convert(principal);
checkAuth(auth);
process.accept(auth, data);
}
});
}
/**
* 业务处理-删除
*
* @param principal 认证用户
* @param req 请求数据
* @param process 处理器
* @param 请求数据类型
* @return 处理结果
*/
protected Mono actionDelete(
@Nonnull final UserPrincipal principal,
@Nonnull final Mono req,
@Nonnull final AuthProccessModifyListener process
) {
return actionDelete(req, new ProccessModifyListener() {
@Override
public void getExceptHandlers(@Nonnull final List handlers) {
process.getExceptHandlers(handlers);
}
@Override
public void preHandler(@Nullable final T reqData) {
process.preHandler(reqData);
}
@Override
public void accept(final T data) {
final A auth = convert(principal);
checkAuth(auth);
process.accept(auth, data);
}
});
}
/**
* 业务处理-删除
*
* @param principal 认证用户
* @param process 删除处理器
* @return 处理结果
*/
protected Mono actionDelete(
@Nonnull final UserPrincipal principal,
@Nonnull final AuthProccessDeleteListener process
) {
return actionDelete(new ProccessDeleteListener() {
@Override
public void getExceptHandlers(@Nonnull final List handlers) {
process.getExceptHandlers(handlers);
}
@Override
public void preHandler(@Nullable final Void reqData) {
process.preHandler(reqData);
}
@Override
public void accept(final Void aVoid) {
final A auth = convert(principal);
checkAuth(auth);
process.accept(auth, aVoid);
}
});
}
protected interface AuthProccessModifyListener extends BiConsumer, PreHandlerListener, ExceptHandlerListener {
}
protected interface AuthProccessDeleteListener extends AuthProccessModifyListener {
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy