sys_api.go 3.7 KB
Newer Older
1 2 3 4 5 6 7
package service

import (
	"errors"
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
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

S
songzhibin97 已提交
18
func CreateApi(api model.SysApi) (err error) {
19
	if !errors.Is(global.GVA_DB.Where("path = ? AND method = ?", api.Path, api.Method).First(&model.SysApi{}).Error, gorm.ErrRecordNotFound) {
20 21
		return errors.New("存在相同api")
	}
S
songzhibin97 已提交
22
	return global.GVA_DB.Create(&api).Error
23 24
}

m0_50812349's avatar
m0_50812349 已提交
25 26 27 28 29
//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteApi
//@description: 删除基础api
//@param: api model.SysApi
//@return: err error
Mr.奇淼('s avatar
Mr.奇淼( 已提交
30

S
songzhibin97 已提交
31
func DeleteApi(api model.SysApi) (err error) {
32
	err = global.GVA_DB.Delete(&api).Error
33
	ClearCasbin(1, api.Path, api.Method)
34 35 36
	return err
}

m0_50812349's avatar
m0_50812349 已提交
37 38 39 40 41
//@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.奇淼( 已提交
42

S
songzhibin97 已提交
43
func GetAPIInfoList(api model.SysApi, info request.PageInfo, order string, desc bool) (err error, list interface{}, total int64) {
44 45
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
46
	db := global.GVA_DB.Model(&model.SysApi{})
47
	var apiList []model.SysApi
48

49 50
	if api.Path != "" {
		db = db.Where("path LIKE ?", "%"+api.Path+"%")
51
	}
52

53 54
	if api.Description != "" {
		db = db.Where("description LIKE ?", "%"+api.Description+"%")
55
	}
56

57 58
	if api.Method != "" {
		db = db.Where("method = ?", api.Method)
59
	}
60

61 62 63 64
	if api.ApiGroup != "" {
		db = db.Where("api_group = ?", api.ApiGroup)
	}

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

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

m0_50812349's avatar
m0_50812349 已提交
86 87 88 89
//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetAllApis
//@description: 获取所有的api
//@return: err error, apis []model.SysApi
Mr.奇淼('s avatar
Mr.奇淼( 已提交
90

91 92 93 94 95
func GetAllApis() (err error, apis []model.SysApi) {
	err = global.GVA_DB.Find(&apis).Error
	return
}

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

102 103 104 105 106
func GetApiById(id float64) (err error, api model.SysApi) {
	err = global.GVA_DB.Where("id = ?", id).First(&api).Error
	return
}

m0_50812349's avatar
m0_50812349 已提交
107 108 109 110 111
//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateApi
//@description: 根据id更新api
//@param: api model.SysApi
//@return: err error
Mr.奇淼('s avatar
Mr.奇淼( 已提交
112

S
songzhibin97 已提交
113
func UpdateApi(api model.SysApi) (err error) {
114
	var oldA model.SysApi
115 116
	err = global.GVA_DB.Where("id = ?", api.ID).First(&oldA).Error
	if oldA.Path != api.Path || oldA.Method != api.Method {
117
		if !errors.Is(global.GVA_DB.Where("path = ? AND method = ?", api.Path, api.Method).First(&model.SysApi{}).Error, gorm.ErrRecordNotFound) {
118 119 120
			return errors.New("存在相同api路径")
		}
	}
121 122 123
	if err != nil {
		return err
	} else {
124
		err = UpdateCasbinApi(oldA.Path, api.Path, oldA.Method, api.Method)
125 126 127
		if err != nil {
			return err
		} else {
S
songzhibin97 已提交
128
			err = global.GVA_DB.Save(&api).Error
129 130 131 132
		}
	}
	return err
}
133 134 135 136 137 138 139 140

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

func DeleteApisByIds(ids request.IdsReq) (err error) {
141
	err = global.GVA_DB.Delete(&[]model.SysApi{}, "id in ?", ids.Ids).Error
142
	return err
143
}
S
songzhibin97 已提交
144 145 146 147

func DeleteApiByIds(ids []string) (err error) {
	return global.GVA_DB.Delete(model.SysApi{}, ids).Error
}