package cn.reghao.tnb.data.app.route;

import cn.reghao.tnb.data.app.handler.TimeHandler;
import cn.reghao.tnb.data.app.handler.UserProfileHandler;
import cn.reghao.tnb.data.app.handler.VideoHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.*;

/**
 * @author reghao
 * @date 2022-05-14 20:45:04
 */
@Configuration
public class RouterConfig {
    private final TimeHandler timeHandler;
    private final VideoHandler videoHandler;

    public RouterConfig(TimeHandler timeHandler, VideoHandler videoHandler) {
        this.timeHandler = timeHandler;
        this.videoHandler = videoHandler;
    }

    @Bean
    public RouterFunction<ServerResponse> timeRouter() {
        return RouterFunctions
                .route(RequestPredicates.GET("/time"), timeHandler::getTime)
                .andRoute(RequestPredicates.GET("/date"), timeHandler::getDate)
                .andRoute(RequestPredicates.GET("/times"), timeHandler::timePerSec)
                .andRoute(RequestPredicates.GET("/api/data/video/banner"), videoHandler::hotVideo1)
                .andRoute(RequestPredicates.GET("/api/data/video/hot1"), videoHandler::hotVideo);
    }

    @Bean
    RouterFunction<ServerResponse> userRoute(UserProfileHandler userHandler) {
        return RouterFunctions.nest(
                RequestPredicates.path("/user"),
                RouterFunctions.route(RequestPredicates.POST(""), userHandler::addUser)
                        .andRoute(RequestPredicates.DELETE("/{id}"), userHandler::delUser)
                        .andRoute(RequestPredicates.PUT(""), userHandler::updateUser)
                        .andRoute(RequestPredicates.GET(""), userHandler::getAllUser)
                        .andRoute(RequestPredicates.GET("/stream"), userHandler::getAllUserStream)
        );
    }
}
