From b15e672eb391f5ab537d61c81ebad8504d0342af Mon Sep 17 00:00:00 2001 From: WenG <450292408@qq.com> Date: Tue, 11 May 2021 23:05:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=B7=AF=E7=94=B1=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../handler/routetype/RouteTypeHandler.java | 62 +++++++++++++++---- .../router/routetype/RouteTypeRouter.java | 5 +- .../static/wg/route/route-type/update.html | 2 +- 3 files changed, 55 insertions(+), 14 deletions(-) diff --git a/src/main/java/ink/wgink/gateway/handler/routetype/RouteTypeHandler.java b/src/main/java/ink/wgink/gateway/handler/routetype/RouteTypeHandler.java index 9b0e33a..079f420 100644 --- a/src/main/java/ink/wgink/gateway/handler/routetype/RouteTypeHandler.java +++ b/src/main/java/ink/wgink/gateway/handler/routetype/RouteTypeHandler.java @@ -6,6 +6,7 @@ import ink.wgink.gateway.handler.BaseHandler; import ink.wgink.gateway.pojo.result.SuccessResult; import ink.wgink.gateway.pojo.routetype.RouteType; import ink.wgink.gateway.util.RequestFieldCheckUtil; +import org.apache.commons.lang3.StringUtils; import org.springframework.data.domain.Example; import org.springframework.data.domain.ExampleMatcher; import org.springframework.http.MediaType; @@ -54,10 +55,10 @@ public class RouteTypeHandler extends BaseHandler { routeTypeExample.setTitle(routeType.getTitle().trim()); ExampleMatcher exampleMatcher = ExampleMatcher.matchingAny().withMatcher("title", ExampleMatcher.GenericPropertyMatcher::exact).withIgnoreCase("id"); Example example = Example.of(routeTypeExample, exampleMatcher); - return routeTypeDao.findOne(example).flatMap(r -> Mono.error(new SearchException("类型已经存在"))) - .switchIfEmpty(routeTypeDao.save(routeType)) - .then(ServerResponse.ok().contentType(MediaType.APPLICATION_JSON_UTF8).body(Flux.just(new SuccessResult()), SuccessResult.class)); - }); + return routeTypeDao.findOne(example).flatMap( + r -> Mono.error(new SearchException("类型已经存在")) + ).switchIfEmpty(routeTypeDao.save(routeType)); + }).then(ServerResponse.ok().contentType(MediaType.APPLICATION_JSON_UTF8).body(Flux.just(new SuccessResult()), SuccessResult.class)); } /** @@ -69,19 +70,43 @@ public class RouteTypeHandler extends BaseHandler { public Mono delete(ServerRequest serverRequest) { String ids = serverRequest.pathVariable("ids"); List idList = Arrays.asList(ids.split(",")); - return routeTypeDao.findAllById(idList).flatMap(route -> routeTypeDao.delete(route)) - .then(ServerResponse.ok().contentType(MediaType.APPLICATION_JSON_UTF8).body(Mono.just(new SuccessResult()), SuccessResult.class)); + return routeTypeDao.findAllById(idList).flatMap(route -> routeTypeDao.delete(route)).then(ServerResponse.ok().contentType(MediaType.APPLICATION_JSON_UTF8).body(Mono.just(new SuccessResult()), SuccessResult.class)); } + /** + * 修改 + * + * @param serverRequest + * @return + */ public Mono update(ServerRequest serverRequest) { String id = serverRequest.pathVariable("id"); Mono routeTypeMono = serverRequest.bodyToMono(RouteType.class); - return routeTypeDao.findById(id).flatMap(routeType -> routeTypeMono.flatMap(rt -> { - RouteType routeTypeExample = new RouteType(); - routeTypeExample.setTitle(rt.getTitle()); - ExampleMatcher exampleMatcher = ExampleMatcher.matchingAny().withMatcher("title", ExampleMatcher.GenericPropertyMatcher::exact).withIgnoreCase("id"); - Example example = Example.of(routeTypeExample, exampleMatcher); - })).switchIfEmpty(ServerResponse.notFound().build()); + + // 查询ID是否存在 + return routeTypeDao.findById(id).flatMap(routeType -> { + setUpdate(routeType); + return routeTypeMono.flatMap(rt -> { + routeType.setTitle(rt.getTitle()); + routeType.setSummary(rt.getSummary()); + + RequestFieldCheckUtil.check(rt); + rt.setSummary(null ); + ExampleMatcher exampleMatcher = ExampleMatcher.matchingAny() + .withMatcher("title", ExampleMatcher.GenericPropertyMatcher::exact) + .withIgnoreCase("id"); + Example example = Example.of(rt, exampleMatcher); + // 查询title是否存在 + return routeTypeDao.findOne(example).flatMap(ert -> { + RouteType existRouteType = (RouteType) ert; + // 如果已经存在 + if (!StringUtils.equals(existRouteType.getUuid(), id)) { + return Mono.error(new SearchException("类型已经存在")); + } + return routeTypeDao.save(routeType); + }).switchIfEmpty(routeTypeDao.save(routeType)); + }); + }).switchIfEmpty(ServerResponse.notFound().build()).then(ServerResponse.ok().contentType(MediaType.APPLICATION_JSON_UTF8).body(Mono.just(new SuccessResult()), SuccessResult.class)); } /** @@ -108,4 +133,17 @@ public class RouteTypeHandler extends BaseHandler { } return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON_UTF8).body(routeTypeDao.findAll(example), RouteType.class); } + + /** + * 详情 + * + * @param serverRequest + * @return + */ + public Mono get(ServerRequest serverRequest) { + String id = serverRequest.pathVariable("id"); + return routeTypeDao.findById(id).flatMap( + routeType -> ServerResponse.ok().body(Mono.just(routeType), RouteType.class) + ).switchIfEmpty(ServerResponse.notFound().build()); + } } diff --git a/src/main/java/ink/wgink/gateway/router/routetype/RouteTypeRouter.java b/src/main/java/ink/wgink/gateway/router/routetype/RouteTypeRouter.java index 927a199..b81602c 100644 --- a/src/main/java/ink/wgink/gateway/router/routetype/RouteTypeRouter.java +++ b/src/main/java/ink/wgink/gateway/router/routetype/RouteTypeRouter.java @@ -28,7 +28,10 @@ public class RouteTypeRouter extends BaseRouter { return RouterFunctions.nest(RequestPredicates.path(ISystemConst.ADMIN_ROUTER_PREFIX + "/api/route-type"), RouterFunctions .route(RequestPredicates.POST("/save"), routeTypeHandler::save) - .andRoute(RequestPredicates.GET("/list"), routeTypeHandler::list)); + .andRoute(RequestPredicates.DELETE("/delete/{ids}"), routeTypeHandler::delete) + .andRoute(RequestPredicates.PUT("/update/{id}"), routeTypeHandler::update) + .andRoute(RequestPredicates.GET("/list"), routeTypeHandler::list) + .andRoute(RequestPredicates.GET("/get/{id}"), routeTypeHandler::get)); } } diff --git a/src/main/resources/static/wg/route/route-type/update.html b/src/main/resources/static/wg/route/route-type/update.html index 2280655..5f868e6 100644 --- a/src/main/resources/static/wg/route/route-type/update.html +++ b/src/main/resources/static/wg/route/route-type/update.html @@ -84,7 +84,7 @@ top.dialog.confirm(top.dataMessage.commit, function(index) { top.dialog.close(index); var loadLayerIndex; - top.restAjax.put(top.restAjax.path('api/route/update-type/{uuid}', [uuid]), formData.field, null, function(code, data) { + top.restAjax.put(top.restAjax.path('api/route-type/update/{uuid}', [uuid]), formData.field, null, function(code, data) { var layerIndex = top.dialog.msg(top.dataMessage.updateSuccess, { time: 0, btn: [top.dataMessage.button.yes, top.dataMessage.button.no],