apiGroup.go 6.2 KB
Newer Older
Y
Your Name 已提交
1
package console_sqlite3
E
eoLinker API Management 已提交
2 3 4 5 6 7

import (
	SQL "database/sql"
	"strconv"
	"time"

Y
Your Name 已提交
8 9
	"github.com/eolinker/goku-api-gateway/common/pdao"
	"github.com/eolinker/goku-api-gateway/server/dao"
E
eoLinker API Management 已提交
10 11
)

Y
Your Name 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
var (
	apiDao dao.APIDao
)

func init() {
	pdao.Need(&apiDao)
}

//APIGroupDao APIGroupDao
type APIGroupDao struct {
	db *SQL.DB
}

//NewAPIGroupDao new APIGroupDao
func NewAPIGroupDao() *APIGroupDao {
	return &APIGroupDao{}
}

//Create create
func (d *APIGroupDao) Create(db *SQL.DB) (interface{}, error) {
	d.db = db
	var i dao.APIGroupDao = d
	return &i, nil
}

Y
Your Name 已提交
37
//AddAPIGroup 新建接口分组
Y
Your Name 已提交
38 39
func (d *APIGroupDao) AddAPIGroup(groupName string, projectID, parentGroupID int) (bool, interface{}, error) {
	db := d.db
E
eoLinker API Management 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
	now := time.Now().Format("2006-01-02 15:04:05")
	Tx, _ := db.Begin()
	groupPath := ""
	groupDepth := 1
	sql := ""

	// 查询父分组信息
	if parentGroupID > 0 {
		const sql = "SELECT groupDepth,groupPath FROM goku_gateway_api_group WHERE groupID = ?;"
		err := Tx.QueryRow(sql, parentGroupID).Scan(&groupDepth, &groupPath)
		if err != nil {
			if err != SQL.ErrNoRows {
				Tx.Rollback()
				return false, "[ERROR]Illegal SQL statement!", err
			}
		}
		if groupDepth > 4 {
			Tx.Rollback()
			return false, "[ERROR]Exceeding the grouping level!", err
		}
	}

	const sql2 = "INSERT INTO goku_gateway_api_group (projectID,groupName,parentGroupID) VALUES (?,?,?);"
	result, err := Tx.Exec(sql2, projectID, groupName, parentGroupID)
	if err != nil {
		Tx.Rollback()
		return false, "[ERROR]Illegal SQL statement!", err
	}
	groupID, err := result.LastInsertId()
	if err != nil {
		Tx.Rollback()
		return false, "[ERROR]Fail to insert data!", err
	}
	if groupPath == "" {
		groupPath = strconv.Itoa(int(groupID))
	} else {
Y
Your Name 已提交
76
		groupDepth = groupDepth + 1
Y
Your Name 已提交
77
		groupPath += "," + strconv.Itoa(int(groupID))
E
eoLinker API Management 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
	}

	// 更新groupDepth和groupPath
	sql = "UPDATE goku_gateway_api_group SET groupPath = ?,groupDepth = ? WHERE groupID = ?;"
	_, err = Tx.Exec(sql, groupPath, groupDepth, groupID)
	if err != nil {
		Tx.Rollback()
		return false, "[ERROR]Fail to update data!", err
	}
	// 更新项目更新时间
	_, err = Tx.Exec("UPDATE goku_gateway_project SET updateTime = ? WHERE projectID = ?;", now, projectID)
	if err != nil {
		Tx.Rollback()
		return false, "[ERROR]Fail to update data!", err
	}
	Tx.Commit()
	return true, groupID, nil
}

Y
Your Name 已提交
97
//EditAPIGroup 修改接口分组
Y
Your Name 已提交
98 99
func (d *APIGroupDao) EditAPIGroup(groupName string, groupID, projectID int) (bool, string, error) {
	db := d.db
E
eoLinker API Management 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
	now := time.Now().Format("2006-01-02 15:04:05")
	Tx, _ := db.Begin()
	sql := "UPDATE goku_gateway_api_group SET groupName = ? WHERE groupID = ? AND projectID = ?;"
	_, err := Tx.Exec(sql, groupName, groupID, projectID)
	if err != nil {
		Tx.Rollback()
		return false, "[ERROR]Fail to update data!", err
	}
	// 更新项目更新时间
	_, err = Tx.Exec("UPDATE goku_gateway_project SET updateTime = ? WHERE projectID = ?;", now, projectID)
	if err != nil {
		Tx.Rollback()
		return false, "[ERROR]Fail to update data!", err
	}
	Tx.Commit()
	return true, "", nil
}

Y
Your Name 已提交
118
//DeleteAPIGroup 删除接口分组
Y
Your Name 已提交
119 120
func (d *APIGroupDao) DeleteAPIGroup(projectID, groupID int) (bool, string, error) {
	db := d.db
E
eoLinker API Management 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
	Tx, _ := db.Begin()
	var groupPath string
	// 获取分组信息
	sql := "SELECT groupPath FROM goku_gateway_api_group WHERE groupID = ?"
	err := Tx.QueryRow(sql, groupID).Scan(&groupPath)
	if err != nil {
		Tx.Rollback()
		return false, "[ERROR]Fail to delete data!", err
	}
	var concatGroupID string
	sql = "SELECT GROUP_CONCAT(DISTINCT groupID) AS groupID FROM goku_gateway_api_group WHERE projectID = ? AND groupPath LIKE ?;"
	err = Tx.QueryRow(sql, projectID, groupPath+"%").Scan(&concatGroupID)
	if err != nil {
		Tx.Rollback()
		return false, "[ERROR]Fail to delete data!", err
	}
	sql = "DELETE FROM goku_gateway_api_group WHERE groupID IN (" + concatGroupID + ");"
	_, err = Tx.Exec(sql)
	if err != nil {
		Tx.Rollback()
		return false, "[ERROR]Fail to delete data!", err
	}
Y
Your Name 已提交
143
	flag, apiList, _ := apiDao.GetAPIListByGroupList(projectID, concatGroupID)
E
eoLinker API Management 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
	if flag {
		listLen := len(apiList)
		if listLen > 0 {
			apiIDList := ""
			for i, api := range apiList {
				apiIDList += strconv.Itoa(api["apiID"].(int))
				if i < listLen-1 {
					apiIDList += ","
				}
			}
			sql = "DELETE FROM goku_gateway_api WHERE apiID IN (" + apiIDList + ");"
			_, err = Tx.Exec(sql)
			if err != nil {
				Tx.Rollback()
				return false, "[ERROR]Fail to delete data!", err
			}
			//sql = "DELETE FROM goku_gateway_api_cache WHERE apiID IN (" + apiIDList + ");"
			//_, err = Tx.Exec(sql)
			//if err != nil {
			//	Tx.Rollback()
			//	return false, "[ERROR]Fail to delete data!", err
			//}

			sql = "DELETE FROM goku_conn_strategy_api WHERE apiID IN (" + apiIDList + ");"
			_, err = Tx.Exec(sql)
			if err != nil {
				Tx.Rollback()
				return false, "[ERROR]Fail to delete data!", err
			}

			sql = "DELETE FROM goku_conn_plugin_api WHERE apiID IN (" + apiIDList + ");"
			_, err = Tx.Exec(sql)
			if err != nil {
				Tx.Rollback()
				return false, "[ERROR]Fail to delete data!", err
			}
Y
Your Name 已提交
180

E
eoLinker API Management 已提交
181 182 183 184 185 186 187 188 189 190 191 192 193 194
		}
	}
	now := time.Now().Format("2006-01-02 15:04:05")
	// 更新项目更新时间
	_, err = Tx.Exec("UPDATE goku_gateway_project SET updateTime = ? WHERE projectID = ?;", now, projectID)
	if err != nil {
		Tx.Rollback()
		return false, "[ERROR]Fail to update data!", err
	}
	// 获取接口分组下面的接口ID
	Tx.Commit()
	return true, "", nil
}

Y
Your Name 已提交
195
//GetAPIGroupList 获取接口分组列表
Y
Your Name 已提交
196 197
func (d *APIGroupDao) GetAPIGroupList(projectID int) (bool, []map[string]interface{}, error) {
	db := d.db
E
eoLinker API Management 已提交
198 199 200 201 202 203 204
	sql := "SELECT groupID,groupName,parentGroupID,groupDepth FROM goku_gateway_api_group WHERE projectID = ?;"
	rows, err := db.Query(sql, projectID)
	if err != nil {
		return false, make([]map[string]interface{}, 0), err
	}
	defer rows.Close()
	//获取记录列
Y
Your Name 已提交
205

Y
Your Name 已提交
206 207 208 209 210 211 212 213 214 215 216 217 218
	groupList := make([]map[string]interface{}, 0)
	for rows.Next() {
		var groupID, parentGroupID, groupDepth int
		var groupName string
		err = rows.Scan(&groupID, &groupName, &parentGroupID, &groupDepth)
		if err != nil {
			return false, make([]map[string]interface{}, 0), err
		}
		groupInfo := map[string]interface{}{
			"groupID":       groupID,
			"groupName":     groupName,
			"groupDepth":    groupDepth,
			"parentGroupID": parentGroupID,
E
eoLinker API Management 已提交
219
		}
Y
Your Name 已提交
220
		groupList = append(groupList, groupInfo)
E
eoLinker API Management 已提交
221
	}
Y
Your Name 已提交
222
	return true, groupList, nil
E
eoLinker API Management 已提交
223
}