217 lines
5.0 KiB
Go
217 lines
5.0 KiB
Go
package system
|
|
|
|
import (
|
|
"kra/internal/biz/system"
|
|
"kra/pkg/response"
|
|
"kra/pkg/utils"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type ApiApi struct{}
|
|
|
|
// CreateApiRequest 创建API请求
|
|
type CreateApiRequest struct {
|
|
Path string `json:"path" binding:"required"`
|
|
Description string `json:"description" binding:"required"`
|
|
ApiGroup string `json:"apiGroup" binding:"required"`
|
|
Method string `json:"method" binding:"required"`
|
|
}
|
|
|
|
// UpdateApiRequest 更新API请求
|
|
type UpdateApiRequest struct {
|
|
ID uint `json:"ID" binding:"required"`
|
|
Path string `json:"path" binding:"required"`
|
|
Description string `json:"description" binding:"required"`
|
|
ApiGroup string `json:"apiGroup" binding:"required"`
|
|
Method string `json:"method" binding:"required"`
|
|
}
|
|
|
|
// DeleteApiRequest 删除API请求
|
|
type DeleteApiRequest struct {
|
|
ID uint `json:"ID" binding:"required"`
|
|
}
|
|
|
|
// GetApiListRequest 获取API列表请求
|
|
type GetApiListRequest struct {
|
|
Page int `json:"page"`
|
|
PageSize int `json:"pageSize"`
|
|
Path string `json:"path"`
|
|
Description string `json:"description"`
|
|
ApiGroup string `json:"apiGroup"`
|
|
Method string `json:"method"`
|
|
OrderKey string `json:"orderKey"`
|
|
Desc bool `json:"desc"`
|
|
}
|
|
|
|
// GetApiByIdRequest 根据ID获取API请求
|
|
type GetApiByIdRequest struct {
|
|
ID uint `json:"ID" binding:"required"`
|
|
}
|
|
|
|
// DeleteApisByIdsRequest 批量删除API请求
|
|
type DeleteApisByIdsRequest struct {
|
|
Ids []uint `json:"ids" binding:"required"`
|
|
}
|
|
|
|
// CreateApi 创建API
|
|
func (a *ApiApi) CreateApi(c *gin.Context) {
|
|
var req CreateApiRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
api := &system.Api{
|
|
Path: req.Path,
|
|
Description: req.Description,
|
|
ApiGroup: req.ApiGroup,
|
|
Method: req.Method,
|
|
}
|
|
|
|
if err := apiUsecase.CreateApi(c, api); err != nil {
|
|
response.FailWithMessage("创建失败", c)
|
|
return
|
|
}
|
|
|
|
response.OkWithMessage("创建成功", c)
|
|
}
|
|
|
|
// DeleteApi 删除API
|
|
func (a *ApiApi) DeleteApi(c *gin.Context) {
|
|
var req DeleteApiRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
if err := apiUsecase.DeleteApi(c, req.ID); err != nil {
|
|
response.FailWithMessage("删除失败", c)
|
|
return
|
|
}
|
|
|
|
response.OkWithMessage("删除成功", c)
|
|
}
|
|
|
|
// UpdateApi 更新API
|
|
func (a *ApiApi) UpdateApi(c *gin.Context) {
|
|
var req UpdateApiRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
api := &system.Api{
|
|
ID: req.ID,
|
|
Path: req.Path,
|
|
Description: req.Description,
|
|
ApiGroup: req.ApiGroup,
|
|
Method: req.Method,
|
|
}
|
|
|
|
if err := apiUsecase.UpdateApi(c, api); err != nil {
|
|
response.FailWithMessage("修改失败", c)
|
|
return
|
|
}
|
|
|
|
response.OkWithMessage("修改成功", c)
|
|
}
|
|
|
|
// GetApiList 获取API列表
|
|
func (a *ApiApi) GetApiList(c *gin.Context) {
|
|
var req GetApiListRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
if req.Page == 0 {
|
|
req.Page = 1
|
|
}
|
|
if req.PageSize == 0 {
|
|
req.PageSize = 10
|
|
}
|
|
|
|
filters := make(map[string]interface{})
|
|
if req.Path != "" {
|
|
filters["path"] = req.Path
|
|
}
|
|
if req.Description != "" {
|
|
filters["description"] = req.Description
|
|
}
|
|
if req.ApiGroup != "" {
|
|
filters["api_group"] = req.ApiGroup
|
|
}
|
|
if req.Method != "" {
|
|
filters["method"] = req.Method
|
|
}
|
|
|
|
list, total, err := apiUsecase.GetApiList(c, req.Page, req.PageSize, filters)
|
|
if err != nil {
|
|
response.FailWithMessage("获取失败", c)
|
|
return
|
|
}
|
|
|
|
response.OkWithDetailed(response.PageResult{
|
|
List: list,
|
|
Total: total,
|
|
Page: req.Page,
|
|
PageSize: req.PageSize,
|
|
}, "获取成功", c)
|
|
}
|
|
|
|
// GetApiById 根据ID获取API
|
|
func (a *ApiApi) GetApiById(c *gin.Context) {
|
|
var req GetApiByIdRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
api, err := apiUsecase.GetApiById(c, req.ID)
|
|
if err != nil {
|
|
response.FailWithMessage("获取失败", c)
|
|
return
|
|
}
|
|
|
|
response.OkWithDetailed(gin.H{"api": api}, "获取成功", c)
|
|
}
|
|
|
|
// GetAllApis 获取所有API
|
|
func (a *ApiApi) GetAllApis(c *gin.Context) {
|
|
authorityID := utils.GetUserAuthorityId(c)
|
|
apis, err := apiUsecase.GetAllApis(c, authorityID)
|
|
if err != nil {
|
|
response.FailWithMessage("获取失败", c)
|
|
return
|
|
}
|
|
|
|
response.OkWithDetailed(gin.H{"apis": apis}, "获取成功", c)
|
|
}
|
|
|
|
// DeleteApisByIds 批量删除API
|
|
func (a *ApiApi) DeleteApisByIds(c *gin.Context) {
|
|
var req DeleteApisByIdsRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
if err := apiUsecase.DeleteApisByIds(c, req.Ids); err != nil {
|
|
response.FailWithMessage("删除失败", c)
|
|
return
|
|
}
|
|
|
|
response.OkWithMessage("删除成功", c)
|
|
}
|
|
|
|
// FreshCasbin 刷新Casbin缓存
|
|
func (a *ApiApi) FreshCasbin(c *gin.Context) {
|
|
if err := casbinUsecase.FreshCasbin(); err != nil {
|
|
response.FailWithMessage("刷新失败", c)
|
|
return
|
|
}
|
|
|
|
response.OkWithMessage("刷新成功", c)
|
|
}
|