54 lines
1.3 KiB
Go
54 lines
1.3 KiB
Go
|
|
package repository
|
||
|
|
|
||
|
|
import (
|
||
|
|
"x-agents/server/internal/model"
|
||
|
|
|
||
|
|
"gorm.io/gorm"
|
||
|
|
)
|
||
|
|
|
||
|
|
// ModelRepository 模型仓储
|
||
|
|
type ModelRepository struct {
|
||
|
|
db *gorm.DB
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewModelRepository(db *gorm.DB) *ModelRepository {
|
||
|
|
return &ModelRepository{db: db}
|
||
|
|
}
|
||
|
|
|
||
|
|
// FindAll 获取所有模型
|
||
|
|
func (r *ModelRepository) FindAll() ([]model.ModelInfo, error) {
|
||
|
|
var models []model.ModelInfo
|
||
|
|
err := r.db.Order("created_at desc").Find(&models).Error
|
||
|
|
return models, err
|
||
|
|
}
|
||
|
|
|
||
|
|
// FindByID 根据 ID 获取模型
|
||
|
|
func (r *ModelRepository) FindByID(id string) (*model.ModelInfo, error) {
|
||
|
|
var model model.ModelInfo
|
||
|
|
err := r.db.Where("id = ?", id).First(&model).Error
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return &model, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// Create 创建模型
|
||
|
|
func (r *ModelRepository) Create(info *model.ModelInfo) error {
|
||
|
|
return r.db.Create(info).Error
|
||
|
|
}
|
||
|
|
|
||
|
|
// Update 更新模型
|
||
|
|
func (r *ModelRepository) Update(id string, info *model.ModelInfo) error {
|
||
|
|
return r.db.Where("id = ?", id).Updates(info).Error
|
||
|
|
}
|
||
|
|
|
||
|
|
// Delete 删除模型
|
||
|
|
func (r *ModelRepository) Delete(id string) error {
|
||
|
|
return r.db.Where("id = ?", id).Delete(&model.ModelInfo{}).Error
|
||
|
|
}
|
||
|
|
|
||
|
|
// UpdateFields 更新指定字段
|
||
|
|
func (r *ModelRepository) UpdateFields(id string, fields map[string]interface{}) error {
|
||
|
|
return r.db.Model(&model.ModelInfo{}).Where("id = ?", id).Updates(fields).Error
|
||
|
|
}
|