package com.ruoyi.gateway.enhance.controller;

import com.ruoyi.common.core.domain.QueryRequest;
import com.ruoyi.gateway.enhance.entity.RateLimitRule;
import com.ruoyi.gateway.enhance.service.RateLimitRuleService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * @author MrBird
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("route/auth/rateLimitRule")
public class RateLimitRuleController {

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        //noinspection RedundantArrayCreation
        binder.setDisallowedFields(new String[]{"admin"});
    }

    private final RateLimitRuleService rateLimitRuleService;

    @GetMapping("data")
    public Flux<RateLimitRule> findUserPages(QueryRequest request, RateLimitRule rateLimitRule) {
        return rateLimitRuleService.findPages(request, rateLimitRule);
    }

    @GetMapping("count")
    public Mono<Long> findUserCount(RateLimitRule rateLimitRule) {
        return rateLimitRuleService.findCount(rateLimitRule);
    }

    @GetMapping(value = "/{id}")
    public Mono<RateLimitRule> findById(@PathVariable("id") String id) {
        return rateLimitRuleService.findById(id);
    }

    @GetMapping("exist")
    public Flux<RateLimitRule> findByRequestUriAndRequestMethod(String requestUri, String requestMethod) {
        return rateLimitRuleService.findByRequestUriAndRequestMethod(requestUri, requestMethod);
    }

    @PostMapping
    @PreAuthorize("hasAuthority('admin')")
    public Mono<RateLimitRule> createRateLimitRule(@RequestBody RateLimitRule rateLimitRule) {
        return rateLimitRuleService.create(rateLimitRule);
    }

    @PutMapping
    @PreAuthorize("hasAuthority('admin')")
    public Mono<RateLimitRule> updateRateLimitRule(@RequestBody RateLimitRule rateLimitRule) {
        return rateLimitRuleService.update(rateLimitRule);
    }

    @DeleteMapping
    @PreAuthorize("hasAuthority('admin')")
    public Flux<RateLimitRule> deleteRateLimitRule(String ids) {
        return rateLimitRuleService.delete(ids);
    }
}
