调整路由入口,新增分页

This commit is contained in:
wanggeng888 2021-04-20 12:32:15 +08:00
parent 4376a53d8e
commit ba311be507
5 changed files with 87 additions and 20 deletions

View File

@ -21,7 +21,4 @@ public interface IRouteDao extends ReactiveMongoRepository<Route, String> {
String COLLECTION_NAME = "sys_route";
@Query("db."+ COLLECTION_NAME+ ".find")
Mono<Route> list(String uuid);
}

View File

@ -2,15 +2,20 @@ package ink.wgink.gateway.handler.route;
import ink.wgink.gateway.dao.route.IRouteDao;
import ink.wgink.gateway.handler.BaseHandler;
import ink.wgink.gateway.pojo.ListSearch;
import ink.wgink.gateway.pojo.route.Route;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.RouterFunction;
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;
import java.util.Optional;
/**
* When you feel like quitting. Think about why you started
@ -54,7 +59,9 @@ public class RouteHandler extends BaseHandler {
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());
return routeDao.findAllById(idList).flatMap(
route -> routeDao.delete(route)
).then(ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).build());
}
/**
@ -66,11 +73,16 @@ public class RouteHandler extends BaseHandler {
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());
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());
}
/**
@ -83,6 +95,37 @@ public class RouteHandler extends BaseHandler {
return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(routeDao.findAll(), Route.class);
}
/**
* 列表分页
*
* @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());
}
Optional<String> keyword = serverRequest.queryParam("keyword");
Route route = new Route();
ExampleMatcher exampleMatcher = ExampleMatcher.matchingAny();
Example example = Example.of(route, exampleMatcher);
if (keyword.isPresent()) {
route.setSystem(keyword.get());
route.setSummary(keyword.get());
exampleMatcher
.withMatcher("system", ExampleMatcher.GenericPropertyMatcher::contains)
.withMatcher("summary", ExampleMatcher.GenericPropertyMatcher::contains);
}
return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(routeDao.findAll().skip((currentPage - 1) * totalSize).take(totalSize), Route.class);
}
/**
* 列表
*
@ -91,7 +134,9 @@ public class RouteHandler extends BaseHandler {
*/
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());
return routeDao.findById(id).flatMap(
route -> ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(Mono.just(route), Route.class)
).switchIfEmpty(ServerResponse.notFound().build());
}
}

View File

@ -1,6 +1,7 @@
package ink.wgink.gateway.pojo;
import lombok.Data;
import lombok.ToString;
import org.springframework.data.annotation.Id;
/**

View File

@ -0,0 +1,21 @@
package ink.wgink.gateway.pojo;
import lombok.Data;
import lombok.ToString;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: Search
* @Description: 搜索
* @Author: wanggeng
* @Date: 2021/4/20 12:06 下午
* @Version: 1.0
*/
@Data
public class ListSearch {
private String keywords;
}

View File

@ -3,21 +3,23 @@ package ink.wgink.gateway.router;
import ink.wgink.gateway.handler.route.RouteHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.*;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: AllRoutes
* @Description: 路由
* @Author: WangGeng
* @Date: 2021/4/19 22:57
* @ClassName: RouteRouter
* @Description: 路由管理
* @Author: wanggeng
* @Date: 2021/4/20 10:19 上午
* @Version: 1.0
**/
*/
@Component
public class AllRoutes {
public class RouteRouter {
/**
* 路由管理
*
@ -25,14 +27,15 @@ public class AllRoutes {
* @return
*/
@Bean
public RouterFunction<ServerResponse> routeRouter(RouteHandler routeHandler) {
public RouterFunction<ServerResponse> routeRouterFunction(RouteHandler routeHandler) {
// 嵌套
return RouterFunctions.nest(RequestPredicates.path("/route"),
return RouterFunctions.nest(RequestPredicates.path("/wg/route"),
RouterFunctions
.route(RequestPredicates.POST("/save"), routeHandler::save)
.andRoute(RequestPredicates.DELETE("/delete/{ids}"), routeHandler::delete)
.andRoute(RequestPredicates.PUT("/update/{id}"), routeHandler::update)
.andRoute(RequestPredicates.GET("/list"), routeHandler::list)
.andRoute(RequestPredicates.GET("/listpage"), routeHandler::listPage)
.andRoute(RequestPredicates.GET("get/{id}"), routeHandler::get)
);
}