124 lines
4.0 KiB
Java
124 lines
4.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.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.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
|
|
* 当你想要放弃的时候,想想当初你为何开始
|
|
*
|
|
* @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 routeDao.findAllById(idList).flatMap(
|
|
route -> routeDao.delete(route)
|
|
).then(ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).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).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());
|
|
}
|
|
|
|
/**
|
|
* 列表
|
|
*
|
|
* @param serverRequest
|
|
* @return
|
|
*/
|
|
public Mono<ServerResponse> list(ServerRequest serverRequest) {
|
|
Optional<String> keywords = serverRequest.queryParam("keywords");
|
|
|
|
Route route = new Route();
|
|
Example example = Example.of(route);
|
|
if (keywords.isPresent()) {
|
|
route.setSystem(keywords.get());
|
|
route.setSummary(keywords.get());
|
|
|
|
ExampleMatcher exampleMatcher = ExampleMatcher.matchingAny()
|
|
.withMatcher("system", ExampleMatcher.GenericPropertyMatcher::contains)
|
|
.withMatcher("summary", ExampleMatcher.GenericPropertyMatcher::contains)
|
|
.withIgnoreCase("id");
|
|
example = Example.of(route, exampleMatcher);
|
|
}
|
|
return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(routeDao.findAll(example), Route.class);
|
|
}
|
|
|
|
/**
|
|
* 列表
|
|
*
|
|
* @param serverRequest
|
|
* @return
|
|
*/
|
|
public Mono<ServerResponse> get(ServerRequest serverRequest) {
|
|
String id = serverRequest.pathVariable("id");
|
|
return routeDao.findById(id).flatMap(
|
|
route -> ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(Mono.just(route), Route.class)
|
|
).switchIfEmpty(ServerResponse.notFound().build());
|
|
}
|
|
|
|
}
|