98 lines
3.0 KiB
Java
98 lines
3.0 KiB
Java
|
package ink.wgink.gateway.handler.route;
|
|||
|
|
|||
|
import ink.wgink.gateway.dao.route.IRouteDao;
|
|||
|
import ink.wgink.gateway.handler.BaseHandler;
|
|||
|
import ink.wgink.gateway.pojo.route.Route;
|
|||
|
import org.springframework.http.MediaType;
|
|||
|
import org.springframework.stereotype.Component;
|
|||
|
import org.springframework.web.reactive.function.server.ServerRequest;
|
|||
|
import org.springframework.web.reactive.function.server.ServerResponse;
|
|||
|
import reactor.core.publisher.Mono;
|
|||
|
|
|||
|
import java.util.Arrays;
|
|||
|
import java.util.List;
|
|||
|
|
|||
|
/**
|
|||
|
* When you feel like quitting. Think about why you started
|
|||
|
* 当你想要放弃的时候,想想当初你为何开始
|
|||
|
*
|
|||
|
* @ClassName: RouteHandler
|
|||
|
* @Description: 路由
|
|||
|
* @Author: WangGeng
|
|||
|
* @Date: 2021/4/19 21:40
|
|||
|
* @Version: 1.0
|
|||
|
**/
|
|||
|
@Component
|
|||
|
public class RouteHandler extends BaseHandler {
|
|||
|
|
|||
|
private IRouteDao routeDao;
|
|||
|
|
|||
|
public RouteHandler(IRouteDao routeDao) {
|
|||
|
this.routeDao = routeDao;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 新增
|
|||
|
*
|
|||
|
* @param serverRequest
|
|||
|
* @return
|
|||
|
*/
|
|||
|
public Mono<ServerResponse> save(ServerRequest serverRequest) {
|
|||
|
Mono<Route> routeMono = serverRequest.bodyToMono(Route.class);
|
|||
|
return routeMono.flatMap(route -> {
|
|||
|
setSave(route);
|
|||
|
return routeDao.save(route);
|
|||
|
}).then(ServerResponse.ok().build());
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 删除
|
|||
|
*
|
|||
|
* @param serverRequest
|
|||
|
* @return
|
|||
|
*/
|
|||
|
public Mono<ServerResponse> delete(ServerRequest serverRequest) {
|
|||
|
String ids = serverRequest.pathVariable("ids");
|
|||
|
List<String> idList = Arrays.asList(ids.split(","));
|
|||
|
return Mono.from(routeDao.findAllById(idList).flatMap(route -> routeDao.delete(route))).then(ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).build()).switchIfEmpty(ServerResponse.notFound().build());
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 修改
|
|||
|
*
|
|||
|
* @param serverRequest
|
|||
|
* @return
|
|||
|
*/
|
|||
|
public Mono<ServerResponse> update(ServerRequest serverRequest) {
|
|||
|
String id = serverRequest.pathVariable("id");
|
|||
|
Mono<Route> routeMono = serverRequest.bodyToMono(Route.class);
|
|||
|
return routeDao.findById(id).then(routeMono.flatMap(route -> {
|
|||
|
route.setUuid(id);
|
|||
|
setUpdate(route);
|
|||
|
return routeDao.save(route);
|
|||
|
})).then(ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).build()).switchIfEmpty(ServerResponse.notFound().build());
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 列表
|
|||
|
*
|
|||
|
* @param serverRequest
|
|||
|
* @return
|
|||
|
*/
|
|||
|
public Mono<ServerResponse> list(ServerRequest serverRequest) {
|
|||
|
return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(routeDao.findAll(), Route.class);
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 列表
|
|||
|
*
|
|||
|
* @param serverRequest
|
|||
|
* @return
|
|||
|
*/
|
|||
|
public Mono<ServerResponse> get(ServerRequest serverRequest) {
|
|||
|
String id = serverRequest.pathVariable("id");
|
|||
|
return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(routeDao.findById(id), Route.class).switchIfEmpty(ServerResponse.notFound().build());
|
|||
|
}
|
|||
|
|
|||
|
}
|