🎉 初始化项目
This commit is contained in:
155
server/service/system/sys_api.go
Normal file
155
server/service/system/sys_api.go
Normal file
@@ -0,0 +1,155 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"git.echol.cn/loser/ai_proxy/server/global"
|
||||
"git.echol.cn/loser/ai_proxy/server/model/system"
|
||||
"git.echol.cn/loser/ai_proxy/server/model/system/request"
|
||||
"git.echol.cn/loser/ai_proxy/server/model/system/response"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ApiService struct{}
|
||||
|
||||
// CreateApi 创建API
|
||||
func (s *ApiService) CreateApi(req *request.CreateApiRequest) error {
|
||||
// 检查是否已存在相同的 API
|
||||
var count int64
|
||||
err := global.GVA_DB.Model(&system.SysApi{}).
|
||||
Where("path = ? AND method = ?", req.Path, req.Method).
|
||||
Count(&count).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if count > 0 {
|
||||
return errors.New("API已存在")
|
||||
}
|
||||
|
||||
api := system.SysApi{
|
||||
Path: req.Path,
|
||||
Description: req.Description,
|
||||
ApiGroup: req.ApiGroup,
|
||||
Method: req.Method,
|
||||
}
|
||||
|
||||
return global.GVA_DB.Create(&api).Error
|
||||
}
|
||||
|
||||
// UpdateApi 更新API
|
||||
func (s *ApiService) UpdateApi(req *request.UpdateApiRequest) error {
|
||||
var api system.SysApi
|
||||
if err := global.GVA_DB.First(&api, req.ID).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return errors.New("API不存在")
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// 检查是否有其他相同的 API
|
||||
var count int64
|
||||
err := global.GVA_DB.Model(&system.SysApi{}).
|
||||
Where("path = ? AND method = ? AND id != ?", req.Path, req.Method, req.ID).
|
||||
Count(&count).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if count > 0 {
|
||||
return errors.New("API已存在")
|
||||
}
|
||||
|
||||
updates := map[string]interface{}{
|
||||
"path": req.Path,
|
||||
"description": req.Description,
|
||||
"api_group": req.ApiGroup,
|
||||
"method": req.Method,
|
||||
}
|
||||
|
||||
return global.GVA_DB.Model(&api).Updates(updates).Error
|
||||
}
|
||||
|
||||
// DeleteApi 删除API
|
||||
func (s *ApiService) DeleteApi(id uint) error {
|
||||
var api system.SysApi
|
||||
if err := global.GVA_DB.First(&api, id).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return errors.New("API不存在")
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
return global.GVA_DB.Delete(&api).Error
|
||||
}
|
||||
|
||||
// GetApiList 获取API列表
|
||||
func (s *ApiService) GetApiList(req *request.GetApiListRequest) (list []response.ApiInfo, total int64, err error) {
|
||||
db := global.GVA_DB.Model(&system.SysApi{})
|
||||
|
||||
// 条件查询
|
||||
if req.Path != "" {
|
||||
db = db.Where("path LIKE ?", "%"+req.Path+"%")
|
||||
}
|
||||
if req.ApiGroup != "" {
|
||||
db = db.Where("api_group = ?", req.ApiGroup)
|
||||
}
|
||||
if req.Method != "" {
|
||||
db = db.Where("method = ?", req.Method)
|
||||
}
|
||||
|
||||
// 获取总数
|
||||
err = db.Count(&total).Error
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
// 分页查询
|
||||
if req.Page > 0 && req.PageSize > 0 {
|
||||
offset := (req.Page - 1) * req.PageSize
|
||||
db = db.Offset(offset).Limit(req.PageSize)
|
||||
}
|
||||
|
||||
var apis []system.SysApi
|
||||
err = db.Order("created_at DESC").Find(&apis).Error
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
// 转换为响应格式
|
||||
list = make([]response.ApiInfo, len(apis))
|
||||
for i, api := range apis {
|
||||
list[i] = response.ApiInfo{
|
||||
ID: api.ID,
|
||||
Path: api.Path,
|
||||
Description: api.Description,
|
||||
ApiGroup: api.ApiGroup,
|
||||
Method: api.Method,
|
||||
CreatedAt: api.CreatedAt,
|
||||
UpdatedAt: api.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
return list, total, nil
|
||||
}
|
||||
|
||||
// GetApiById 根据ID获取API
|
||||
func (s *ApiService) GetApiById(id uint) (info response.ApiInfo, err error) {
|
||||
var api system.SysApi
|
||||
if err = global.GVA_DB.First(&api, id).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return info, errors.New("API不存在")
|
||||
}
|
||||
return info, err
|
||||
}
|
||||
|
||||
info = response.ApiInfo{
|
||||
ID: api.ID,
|
||||
Path: api.Path,
|
||||
Description: api.Description,
|
||||
ApiGroup: api.ApiGroup,
|
||||
Method: api.Method,
|
||||
CreatedAt: api.CreatedAt,
|
||||
UpdatedAt: api.UpdatedAt,
|
||||
}
|
||||
|
||||
return info, nil
|
||||
}
|
||||
Reference in New Issue
Block a user