基础项目
Some checks failed
CI / init (pull_request) Has been cancelled
CI / Frontend node 18.16.0 (pull_request) Has been cancelled
CI / Backend go (1.22) (pull_request) Has been cancelled
CI / release-pr (pull_request) Has been cancelled
CI / devops-test (1.22, 18.16.0) (pull_request) Has been cancelled
CI / release-please (pull_request) Has been cancelled
CI / devops-prod (1.22, 18.x) (pull_request) Has been cancelled
CI / docker (pull_request) Has been cancelled

This commit is contained in:
2026-04-26 15:32:21 +08:00
parent cc40d743cb
commit 1e33640629
102 changed files with 4088 additions and 197 deletions

View File

@@ -95,8 +95,8 @@ func Test_autoCodePackage_templates(t *testing.T) {
}
for key, value := range gotCode {
t.Logf("\n")
t.Logf(key)
t.Logf(value)
t.Log(key)
t.Log(value)
t.Logf("\n")
}
t.Log(gotCreates)

View File

@@ -134,6 +134,9 @@ func (apiService *ApiService) IgnoreApi(ignoreApi system.SysIgnoreApi) (err erro
}
func (apiService *ApiService) EnterSyncApi(syncApis systemRes.SysSyncApis) (err error) {
if len(syncApis.NewApis) == 0 && len(syncApis.DeleteApis) == 0 {
return apiService.SyncApiToDB()
}
return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
var txErr error
if len(syncApis.NewApis) > 0 {
@@ -153,6 +156,87 @@ func (apiService *ApiService) EnterSyncApi(syncApis systemRes.SysSyncApis) (err
})
}
func (apiService *ApiService) SyncApiToDB() (err error) {
return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
var dbApis []system.SysApi
if err := tx.Find(&dbApis).Error; err != nil {
return err
}
var ignores []system.SysIgnoreApi
if err := tx.Find(&ignores).Error; err != nil {
return err
}
ignoreMap := make(map[string]bool, len(ignores))
for i := range ignores {
ignoreMap[apiRouteKey(ignores[i].Path, ignores[i].Method)] = true
}
dbMap := make(map[string]system.SysApi, len(dbApis))
for i := range dbApis {
dbMap[apiRouteKey(dbApis[i].Path, dbApis[i].Method)] = dbApis[i]
}
routeMap := make(map[string]system.SysApi, len(global.GVA_ROUTERS))
for i := range global.GVA_ROUTERS {
path := global.GVA_ROUTERS[i].Path
method := global.GVA_ROUTERS[i].Method
key := apiRouteKey(path, method)
if ignoreMap[key] {
continue
}
routeMap[key] = system.SysApi{
Path: path,
Method: method,
ApiGroup: defaultApiGroup(path),
Description: defaultApiDescription(method, path),
}
}
newApis := make([]system.SysApi, 0)
for key, routeApi := range routeMap {
if _, ok := dbMap[key]; !ok {
newApis = append(newApis, routeApi)
}
}
if len(newApis) > 0 {
if err := tx.Create(&newApis).Error; err != nil {
return err
}
}
for i := range dbApis {
key := apiRouteKey(dbApis[i].Path, dbApis[i].Method)
if _, ok := routeMap[key]; ok {
continue
}
CasbinServiceApp.ClearCasbin(1, dbApis[i].Path, dbApis[i].Method)
if err := tx.Delete(&system.SysApi{}, "path = ? AND method = ?", dbApis[i].Path, dbApis[i].Method).Error; err != nil {
return err
}
}
return nil
})
}
func apiRouteKey(path, method string) string {
return method + " " + path
}
func defaultApiGroup(path string) string {
pathArr := strings.Split(strings.Trim(path, "/"), "/")
if len(pathArr) == 0 || pathArr[0] == "" {
return "default"
}
return pathArr[0]
}
func defaultApiDescription(method, path string) string {
return method + " " + path
}
//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteApi
//@description: 删除基础api

View File

@@ -0,0 +1,84 @@
package system
import (
"testing"
"github.com/flipped-aurora/gin-vue-admin/server/global"
systemModel "github.com/flipped-aurora/gin-vue-admin/server/model/system"
systemRes "github.com/flipped-aurora/gin-vue-admin/server/model/system/response"
"github.com/gin-gonic/gin"
"github.com/glebarez/sqlite"
"github.com/stretchr/testify/require"
"gorm.io/gorm"
)
func setupApiServiceTestDB(t *testing.T) {
t.Helper()
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
require.NoError(t, err)
require.NoError(t, db.AutoMigrate(&systemModel.SysApi{}, &systemModel.SysIgnoreApi{}))
global.GVA_DB = db
global.GVA_ROUTERS = gin.RoutesInfo{
{
Method: "POST",
Path: "/book/createBook",
},
}
t.Cleanup(func() {
global.GVA_DB = nil
global.GVA_ROUTERS = nil
})
}
func TestApiService_EnterSyncApiSyncsRoutesWhenPayloadIsEmpty(t *testing.T) {
setupApiServiceTestDB(t)
err := ApiServiceApp.EnterSyncApi(systemRes.SysSyncApis{})
require.NoError(t, err)
var api systemModel.SysApi
err = global.GVA_DB.Where("path = ? AND method = ?", "/book/createBook", "POST").First(&api).Error
require.NoError(t, err)
require.Equal(t, "/book/createBook", api.Path)
require.Equal(t, "POST", api.Method)
}
func TestApiService_EnterSyncApiKeepsExistingApiMetadata(t *testing.T) {
setupApiServiceTestDB(t)
err := global.GVA_DB.Create(&systemModel.SysApi{
Path: "/book/createBook",
Method: "POST",
ApiGroup: "book-admin",
Description: "创建书籍",
}).Error
require.NoError(t, err)
err = ApiServiceApp.EnterSyncApi(systemRes.SysSyncApis{})
require.NoError(t, err)
var api systemModel.SysApi
err = global.GVA_DB.Where("path = ? AND method = ?", "/book/createBook", "POST").First(&api).Error
require.NoError(t, err)
require.Equal(t, "book-admin", api.ApiGroup)
require.Equal(t, "创建书籍", api.Description)
}
func TestApiService_EnterSyncApiSkipsIgnoredRoutes(t *testing.T) {
setupApiServiceTestDB(t)
err := global.GVA_DB.Create(&systemModel.SysIgnoreApi{
Path: "/book/createBook",
Method: "POST",
}).Error
require.NoError(t, err)
err = ApiServiceApp.EnterSyncApi(systemRes.SysSyncApis{})
require.NoError(t, err)
var count int64
err = global.GVA_DB.Model(&systemModel.SysApi{}).Where("path = ? AND method = ?", "/book/createBook", "POST").Count(&count).Error
require.NoError(t, err)
require.Zero(t, count)
}