feat(core): 新增基础仓库接口及相关模型仓库定义
- 新增 BaseRepository 泛型接口,定义通用增删改查方法 - 实现 BaseRepo 结构体及其 Insert 方法 - 定义 ProviderRepository、ModelRepository、ChannelRepository、APIKeyRepository 接口,继承基础接口 - 将模型结构 JanusModel 和 JanusProvider 重命名为 Model 和 Provider - 创建 ProviderRepo 结构体,组合基础仓库实现 Provider 仓库功能
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// BaseRepository 是一个泛型接口,定义了基本的增删改查操作
|
||||
type BaseRepository[T any] interface {
|
||||
// Insert 插入一条记录
|
||||
Insert(ctx context.Context, record *T) error
|
||||
// SelectById 根据id查询一条记录
|
||||
SelectById(ctx context.Context, id int64) (*T, error)
|
||||
// List 查询所有记录
|
||||
List(ctx context.Context) ([]*T, error)
|
||||
// Update 更新一条记录
|
||||
Update(ctx context.Context, record *T) error
|
||||
// Delete 根据id删除一条记录
|
||||
Delete(ctx context.Context, id int64) error
|
||||
}
|
||||
|
||||
type BaseRepo[T any] struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func (repo *BaseRepo[T]) Insert(ctx context.Context, record *T) error {
|
||||
return repo.db.WithContext(ctx).Create(record).Error
|
||||
}
|
||||
@@ -8,8 +8,8 @@ import (
|
||||
// Type 模型类型
|
||||
type Type string
|
||||
|
||||
// JanusModel 供应商下可用的AI模型
|
||||
type JanusModel struct {
|
||||
// Model 供应商下可用的AI模型
|
||||
type Model struct {
|
||||
core.BaseModel
|
||||
// ProviderID 供应商ID
|
||||
ProviderID int64 `gorm:"not null" json:"provider_id"`
|
||||
|
||||
@@ -5,8 +5,8 @@ import (
|
||||
"synoth.com/janus/internal/enum"
|
||||
)
|
||||
|
||||
// JanusProvider 服务提供商
|
||||
type JanusProvider struct {
|
||||
// Provider 服务提供商
|
||||
type Provider struct {
|
||||
core.BaseModel
|
||||
// Name 服务供应商名称
|
||||
Name string `gorm:"uniqueIndex;size:100;not null" json:"name"`
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"synoth.com/janus/internal/core"
|
||||
"synoth.com/janus/internal/model"
|
||||
)
|
||||
|
||||
// ProviderRepository 模型服务商的接口定义
|
||||
type ProviderRepository interface {
|
||||
core.BaseRepository[model.Provider]
|
||||
}
|
||||
|
||||
type ModelRepository interface {
|
||||
core.BaseRepository[model.Model]
|
||||
}
|
||||
|
||||
type ChannelRepository interface {
|
||||
core.BaseRepository[model.Channel]
|
||||
}
|
||||
|
||||
type APIKeyRepository interface {
|
||||
core.BaseRepository[model.APIKey]
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"synoth.com/janus/internal/core"
|
||||
"synoth.com/janus/internal/model"
|
||||
)
|
||||
|
||||
type ProviderRepo struct {
|
||||
core.BaseRepo[model.Provider]
|
||||
}
|
||||
Reference in New Issue
Block a user