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

import (
	"errors"
S
songzhibin97 已提交
5 6 7 8

	"github.com/flipped-aurora/gin-vue-admin/global"
	"github.com/flipped-aurora/gin-vue-admin/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/model/system"
9

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

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

19 20 21 22 23 24
type ApiService struct {
}

var ApiServiceApp = new(ApiService)

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

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

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

m0_50812349's avatar
m0_50812349 已提交
43 44 45 46 47
//@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.奇淼( 已提交
48

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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