package ink.wgink.gateway.router.route;

import ink.wgink.gateway.consts.ISystemConst;
import ink.wgink.gateway.handler.route.RouteHandler;
import ink.wgink.gateway.router.BaseRouter;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
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: RouteRouter
 * @Description: 路由管理
 * @Author: wanggeng
 * @Date: 2021/4/20 10:19 上午
 * @Version: 1.0
 */
@Component
public class RouteRouter extends BaseRouter {
    /**
     * 路由管理
     *
     * @param routeHandler
     * @return
     */
    @Bean
    public RouterFunction<ServerResponse> routeRouterFunction(RouteHandler routeHandler) {
        // 嵌套
        return RouterFunctions.nest(RequestPredicates.path(ISystemConst.ADMIN_ROUTER_PREFIX + "/api/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("get/{id}"), routeHandler::get)
                        .andRoute(RequestPredicates.GET("refresh"), routeHandler::refresh)
        );
    }

}
