package com.zsc.edu.gateway.modules.system.controller;

import com.zsc.edu.gateway.framework.security.UserDetailsImpl;
import com.zsc.edu.gateway.modules.system.dto.MenuDto;
import com.zsc.edu.gateway.modules.system.entity.Menu;
import com.zsc.edu.gateway.modules.system.service.MenuService;
import com.zsc.edu.gateway.modules.system.vo.MenuVo;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhuang
 */
@AllArgsConstructor
@RestController
@RequestMapping("/api/rest/menu")
public class MenuController {

    private final MenuService service;

    /**
     * 新建菜单
     */
    @PostMapping
    @PreAuthorize("hasAuthority('system:menu:create')")
    public Menu create(@RequestBody MenuDto dto) {
        return service.create(dto);
    }

    /**
     * 更新菜单
     */
    @PatchMapping("/{id}")
    @PreAuthorize("hasAuthority('system:menu:update')")
    public Menu update(@RequestBody MenuDto dto, @PathVariable("id") Long id) {
        return service.update(dto, id);
    }

    /**
     * 删除菜单
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('system:menu:delete')")
    public Boolean delete(@PathVariable("id") Long id) {
        return service.delete(id);
    }

    /**
     * 根据名字返回树
     */
    @GetMapping("/tree")
    @PreAuthorize("hasAuthority('system:menu:query')")
    public List<MenuVo> tree(@AuthenticationPrincipal UserDetailsImpl userDetails, @RequestParam String name) {
        return service.getTree(userDetails, name);
    }

    /**
     * 根据ID查询菜单详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('system:menu:query')")
    public Menu detail(@PathVariable("id") Long id) {
        return service.detail(id);
    }
}
