goweb/repository/apis_repository.go

36 lines
759 B
Go

package repository
import (
"github.com/gin-gonic/gin"
. "go_api_tmpl/global"
"go_api_tmpl/model"
)
type apisRepository struct{}
type ApisRepository interface {
SaveAllApi(info gin.RoutesInfo)
Migrate() error
}
// NewApisRepository 新建实例
func NewApisRepository() ApisRepository {
return apisRepository{}
}
// SaveAllApi 保存所有gin的路由
func (ar apisRepository) SaveAllApi(routes gin.RoutesInfo) {
for _, route := range routes {
m := model.Apis{Path: route.Path, Method: route.Method}
err := MySQLConn.FirstOrCreate(&m, &m).Error
if err != nil {
Log.Infof("保存菜单错误: %v", err)
}
}
}
// Migrate 初始化数据库结构
func (ar apisRepository) Migrate() error {
return MySQLConn.AutoMigrate(&model.Apis{})
}