2021-04-19 23:37:33 +08:00
|
|
|
|
package ink.wgink.gateway.handler.route;
|
|
|
|
|
|
|
|
|
|
import ink.wgink.gateway.dao.route.IRouteDao;
|
|
|
|
|
import ink.wgink.gateway.handler.BaseHandler;
|
2021-04-20 12:32:15 +08:00
|
|
|
|
import ink.wgink.gateway.pojo.ListSearch;
|
2021-04-20 21:26:40 +08:00
|
|
|
|
import ink.wgink.gateway.pojo.result.SuccessResultList;
|
2021-04-19 23:37:33 +08:00
|
|
|
|
import ink.wgink.gateway.pojo.route.Route;
|
2021-04-20 12:32:15 +08:00
|
|
|
|
import org.springframework.data.domain.Example;
|
|
|
|
|
import org.springframework.data.domain.ExampleMatcher;
|
2021-04-19 23:37:33 +08:00
|
|
|
|
import org.springframework.http.MediaType;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
2021-04-20 12:32:15 +08:00
|
|
|
|
import org.springframework.web.reactive.function.server.RouterFunction;
|
2021-04-19 23:37:33 +08:00
|
|
|
|
import org.springframework.web.reactive.function.server.ServerRequest;
|
|
|
|
|
import org.springframework.web.reactive.function.server.ServerResponse;
|
2021-04-20 21:26:40 +08:00
|
|
|
|
import reactor.core.publisher.Flux;
|
2021-04-19 23:37:33 +08:00
|
|
|
|
import reactor.core.publisher.Mono;
|
|
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.List;
|
2021-04-20 12:32:15 +08:00
|
|
|
|
import java.util.Optional;
|
2021-04-19 23:37:33 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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(","));
|
2021-04-20 12:32:15 +08:00
|
|
|
|
return routeDao.findAllById(idList).flatMap(
|
|
|
|
|
route -> routeDao.delete(route)
|
|
|
|
|
).then(ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).build());
|
2021-04-19 23:37:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 修改
|
|
|
|
|
*
|
|
|
|
|
* @param serverRequest
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public Mono<ServerResponse> update(ServerRequest serverRequest) {
|
|
|
|
|
String id = serverRequest.pathVariable("id");
|
|
|
|
|
Mono<Route> routeMono = serverRequest.bodyToMono(Route.class);
|
2021-04-20 12:32:15 +08:00
|
|
|
|
return routeDao.findById(id).flatMap(
|
|
|
|
|
route -> routeMono.flatMap(r -> {
|
|
|
|
|
route.setSystem(r.getSystem());
|
|
|
|
|
route.setSummary(r.getSummary());
|
|
|
|
|
route.setPath(r.getPath());
|
|
|
|
|
route.setUri(r.getUri());
|
|
|
|
|
setUpdate(route);
|
|
|
|
|
return routeDao.save(route);
|
|
|
|
|
}).then(ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).build())
|
|
|
|
|
).switchIfEmpty(ServerResponse.notFound().build());
|
2021-04-19 23:37:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 列表
|
|
|
|
|
*
|
|
|
|
|
* @param serverRequest
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public Mono<ServerResponse> list(ServerRequest serverRequest) {
|
|
|
|
|
return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(routeDao.findAll(), Route.class);
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-20 12:32:15 +08:00
|
|
|
|
/**
|
|
|
|
|
* 列表分页
|
|
|
|
|
*
|
|
|
|
|
* @param serverRequest
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public Mono<ServerResponse> listPage(ServerRequest serverRequest) {
|
|
|
|
|
Optional<String> page = serverRequest.queryParam("page");
|
|
|
|
|
int currentPage = 1;
|
|
|
|
|
if (page.isPresent()) {
|
|
|
|
|
currentPage = Integer.parseInt(page.get());
|
|
|
|
|
}
|
|
|
|
|
int totalSize = 5;
|
|
|
|
|
Optional<String> size = serverRequest.queryParam("size");
|
|
|
|
|
if (size.isPresent()) {
|
|
|
|
|
totalSize = Integer.parseInt(size.get());
|
|
|
|
|
}
|
2021-04-20 21:26:40 +08:00
|
|
|
|
|
2021-04-20 12:32:15 +08:00
|
|
|
|
Optional<String> keyword = serverRequest.queryParam("keyword");
|
|
|
|
|
Route route = new Route();
|
2021-04-20 21:26:40 +08:00
|
|
|
|
ExampleMatcher exampleMatcher = ExampleMatcher.matchingAny()
|
|
|
|
|
.withMatcher("system", ExampleMatcher.GenericPropertyMatcher::contains)
|
|
|
|
|
.withMatcher("summary", ExampleMatcher.GenericPropertyMatcher::contains)
|
|
|
|
|
.withIgnoreCase("id");
|
2021-04-20 12:32:15 +08:00
|
|
|
|
Example example = Example.of(route, exampleMatcher);
|
|
|
|
|
if (keyword.isPresent()) {
|
|
|
|
|
route.setSystem(keyword.get());
|
|
|
|
|
route.setSummary(keyword.get());
|
|
|
|
|
}
|
2021-04-20 21:26:40 +08:00
|
|
|
|
return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(listPage(example, currentPage - 1, totalSize), Route.class);
|
|
|
|
|
//return routeDao.findAll(example).skip((currentPage - 1) * totalSize).take(totalSize);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Mono<SuccessResultList> listPage(Example example, int skip, long totalSize) {
|
|
|
|
|
Flux<Route> routeFlux = routeDao.findAll(example).skip(skip * totalSize).take(totalSize);
|
|
|
|
|
Mono<Long> mono = routeDao.count(example);
|
|
|
|
|
return Mono.just(new SuccessResultList());
|
2021-04-20 12:32:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-19 23:37:33 +08:00
|
|
|
|
/**
|
|
|
|
|
* 列表
|
|
|
|
|
*
|
|
|
|
|
* @param serverRequest
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public Mono<ServerResponse> get(ServerRequest serverRequest) {
|
|
|
|
|
String id = serverRequest.pathVariable("id");
|
2021-04-20 12:32:15 +08:00
|
|
|
|
return routeDao.findById(id).flatMap(
|
|
|
|
|
route -> ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(Mono.just(route), Route.class)
|
|
|
|
|
).switchIfEmpty(ServerResponse.notFound().build());
|
2021-04-19 23:37:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|