You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
109 lines
2.8 KiB
109 lines
2.8 KiB
package repository |
|
|
|
import ( |
|
"gofaster/internal/auth/model" |
|
|
|
"gorm.io/gorm" |
|
) |
|
|
|
// FrontendRouteRepository 前台路由仓库 |
|
type FrontendRouteRepository struct { |
|
db *gorm.DB |
|
} |
|
|
|
// NewFrontendRouteRepository 创建前台路由仓库实例 |
|
func NewFrontendRouteRepository(db *gorm.DB) *FrontendRouteRepository { |
|
return &FrontendRouteRepository{db: db} |
|
} |
|
|
|
// Create 创建前台路由 |
|
func (r *FrontendRouteRepository) Create(route *model.FrontendRoute) error { |
|
return r.db.Create(route).Error |
|
} |
|
|
|
// FindByID 根据ID查找前台路由 |
|
func (r *FrontendRouteRepository) FindByID(id uint) (*model.FrontendRoute, error) { |
|
var route model.FrontendRoute |
|
err := r.db.Where("id = ?", id).First(&route).Error |
|
if err != nil { |
|
return nil, err |
|
} |
|
return &route, nil |
|
} |
|
|
|
// FindByPath 根据路径查找前台路由 |
|
func (r *FrontendRouteRepository) FindByPath(path string) (*model.FrontendRoute, error) { |
|
var route model.FrontendRoute |
|
err := r.db.Where("path = ?", path).First(&route).Error |
|
if err != nil { |
|
return nil, err |
|
} |
|
return &route, nil |
|
} |
|
|
|
// FindByModule 根据模块查找前台路由 |
|
func (r *FrontendRouteRepository) FindByModule(module string) ([]*model.FrontendRoute, error) { |
|
var routes []*model.FrontendRoute |
|
err := r.db.Where("module = ?", module).Order("sort ASC").Find(&routes).Error |
|
return routes, err |
|
} |
|
|
|
// List 获取前台路由列表 |
|
func (r *FrontendRouteRepository) List() ([]*model.FrontendRoute, error) { |
|
var routes []*model.FrontendRoute |
|
err := r.db.Order("sort ASC").Find(&routes).Error |
|
return routes, err |
|
} |
|
|
|
// Update 更新前台路由 |
|
func (r *FrontendRouteRepository) Update(route *model.FrontendRoute) error { |
|
return r.db.Save(route).Error |
|
} |
|
|
|
// Delete 删除前台路由 |
|
func (r *FrontendRouteRepository) Delete(id uint) error { |
|
return r.db.Delete(&model.FrontendRoute{}, id).Error |
|
} |
|
|
|
// UpsertByPath 根据路径更新或插入前台路由 |
|
func (r *FrontendRouteRepository) UpsertByPath(route *model.FrontendRoute) error { |
|
var existingRoute model.FrontendRoute |
|
err := r.db.Where("path = ?", route.Path).First(&existingRoute).Error |
|
|
|
if err != nil { |
|
if err == gorm.ErrRecordNotFound { |
|
// 不存在则创建 |
|
return r.Create(route) |
|
} |
|
return err |
|
} |
|
|
|
// 存在则更新 |
|
route.ID = existingRoute.ID |
|
return r.Update(route) |
|
} |
|
|
|
// GetStats 获取前台路由统计信息 |
|
func (r *FrontendRouteRepository) GetStats() (map[string]interface{}, error) { |
|
var total int64 |
|
var moduleStats []struct { |
|
Module string `json:"module"` |
|
Count int64 `json:"count"` |
|
} |
|
|
|
if err := r.db.Model(&model.FrontendRoute{}).Count(&total).Error; err != nil { |
|
return nil, err |
|
} |
|
|
|
if err := r.db.Model(&model.FrontendRoute{}). |
|
Select("module, count(*) as count"). |
|
Group("module"). |
|
Scan(&moduleStats).Error; err != nil { |
|
return nil, err |
|
} |
|
|
|
return map[string]interface{}{ |
|
"total": total, |
|
"module_stats": moduleStats, |
|
}, nil |
|
}
|
|
|