sys_api.go 4.0 KB
Newer Older
1
package system
2 3 4 5

import (
	"errors"
	"gin-vue-admin/global"
6
	"gin-vue-admin/model/common/request"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
7
	"gin-vue-admin/model/system"
8

Mr.奇淼('s avatar
Mr.奇淼( 已提交
9
	"gorm.io/gorm"
10 11
)

m0_50812349's avatar
m0_50812349 已提交
12 13 14 15 16
//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateApi
//@description: 新增基础api
//@param: api model.SysApi
//@return: err error
Mr.奇淼('s avatar
Mr.奇淼( 已提交
17

18 19 20 21 22 23
type ApiService struct {
}

var ApiServiceApp = new(ApiService)

func (apiService *ApiService) CreateApi(api system.SysApi) (err error) {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
24
	if !errors.Is(global.GVA_DB.Where("path = ? AND method = ?", api.Path, api.Method).First(&system.SysApi{}).Error, gorm.ErrRecordNotFound) {
25 26
		return errors.New("存在相同api")
	}
S
songzhibin97 已提交
27
	return global.GVA_DB.Create(&api).Error
28 29
}

m0_50812349's avatar
m0_50812349 已提交
30 31 32 33 34
//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteApi
//@description: 删除基础api
//@param: api model.SysApi
//@return: err error
Mr.奇淼('s avatar
Mr.奇淼( 已提交
35

36
func (apiService *ApiService) DeleteApi(api system.SysApi) (err error) {
37
	err = global.GVA_DB.Delete(&api).Error
38
	CasbinServiceApp.ClearCasbin(1, api.Path, api.Method)
39 40 41
	return err
}

m0_50812349's avatar
m0_50812349 已提交
42 43 44 45 46
//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetAPIInfoList
//@description: 分页获取数据,
//@param: api model.SysApi, info request.PageInfo, order string, desc bool
//@return: err error
Mr.奇淼('s avatar
Mr.奇淼( 已提交
47

48
func (apiService *ApiService) GetAPIInfoList(api system.SysApi, info request.PageInfo, order string, desc bool) (err error, list interface{}, total int64) {
49 50
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
51 52
	db := global.GVA_DB.Model(&system.SysApi{})
	var apiList []system.SysApi
53

54 55
	if api.Path != "" {
		db = db.Where("path LIKE ?", "%"+api.Path+"%")
56
	}
57

58 59
	if api.Description != "" {
		db = db.Where("description LIKE ?", "%"+api.Description+"%")
60
	}
61

62 63
	if api.Method != "" {
		db = db.Where("method = ?", api.Method)
64
	}
65

66 67 68 69
	if api.ApiGroup != "" {
		db = db.Where("api_group = ?", api.ApiGroup)
	}

Mr.奇淼('s avatar
Mr.奇淼( 已提交
70
	err = db.Count(&total).Error
71

72 73 74 75
	if err != nil {
		return err, apiList, total
	} else {
		db = db.Limit(limit).Offset(offset)
76
		if order != "" {
77
			var OrderStr string
78 79
			if desc {
				OrderStr = order + " desc"
80
			} else {
81
				OrderStr = order
82
			}
Mr.奇淼('s avatar
Mr.奇淼( 已提交
83
			err = db.Order(OrderStr).Find(&apiList).Error
84
		} else {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
85
			err = db.Order("api_group").Find(&apiList).Error
86 87
		}
	}
88
	return err, apiList, total
89 90
}

m0_50812349's avatar
m0_50812349 已提交
91 92 93 94
//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetAllApis
//@description: 获取所有的api
//@return: err error, apis []model.SysApi
Mr.奇淼('s avatar
Mr.奇淼( 已提交
95

96
func (apiService *ApiService) GetAllApis() (err error, apis []system.SysApi) {
97 98 99 100
	err = global.GVA_DB.Find(&apis).Error
	return
}

m0_50812349's avatar
m0_50812349 已提交
101 102 103 104 105
//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetApiById
//@description: 根据id获取api
//@param: id float64
//@return: err error, api model.SysApi
Mr.奇淼('s avatar
Mr.奇淼( 已提交
106

107
func (apiService *ApiService) GetApiById(id float64) (err error, api system.SysApi) {
108 109 110 111
	err = global.GVA_DB.Where("id = ?", id).First(&api).Error
	return
}

m0_50812349's avatar
m0_50812349 已提交
112 113 114 115 116
//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateApi
//@description: 根据id更新api
//@param: api model.SysApi
//@return: err error
Mr.奇淼('s avatar
Mr.奇淼( 已提交
117

118
func (apiService *ApiService) UpdateApi(api system.SysApi) (err error) {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
119
	var oldA system.SysApi
120 121
	err = global.GVA_DB.Where("id = ?", api.ID).First(&oldA).Error
	if oldA.Path != api.Path || oldA.Method != api.Method {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
122
		if !errors.Is(global.GVA_DB.Where("path = ? AND method = ?", api.Path, api.Method).First(&system.SysApi{}).Error, gorm.ErrRecordNotFound) {
123 124 125
			return errors.New("存在相同api路径")
		}
	}
126 127 128
	if err != nil {
		return err
	} else {
129
		err = CasbinServiceApp.UpdateCasbinApi(oldA.Path, api.Path, oldA.Method, api.Method)
130 131 132
		if err != nil {
			return err
		} else {
S
songzhibin97 已提交
133
			err = global.GVA_DB.Save(&api).Error
134 135 136 137
		}
	}
	return err
}
138 139 140 141 142 143 144

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteApis
//@description: 删除选中API
//@param: apis []model.SysApi
//@return: err error

145
func (apiService *ApiService) DeleteApisByIds(ids request.IdsReq) (err error) {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
146
	err = global.GVA_DB.Delete(&[]system.SysApi{}, "id in ?", ids.Ids).Error
147
	return err
148
}
S
songzhibin97 已提交
149

150
func (apiService *ApiService) DeleteApiByIds(ids []string) (err error) {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
151
	return global.GVA_DB.Delete(system.SysApi{}, ids).Error
S
songzhibin97 已提交
152
}