From 66db64267c765ba065ffe1efaadf5264a6240f51 Mon Sep 17 00:00:00 2001 From: yangzhaohan Date: Fri, 17 Jul 2026 09:17:15 +0800 Subject: [PATCH] =?UTF-8?q?feat(repository):=20=E6=B7=BB=E5=8A=A0=E9=80=9A?= =?UTF-8?q?=E7=94=A8=E5=9F=BA=E7=A1=80=E4=BB=93=E5=BA=93=E5=8F=8A=E5=85=B7?= =?UTF-8?q?=E4=BD=93=E5=AE=9E=E4=BD=93=E4=BB=93=E5=BA=93=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 core.BaseRepo 泛型仓库,提供增删改查基本操作方法 - 实现 Insert、SelectById、List、Update 和 Delete 方法,支持上下文传递 - 新增 ChannelRepo、ModelRepo 和 APIKeyRepo,均基于 BaseRepo 实现 - 规范化仓库结构,提升代码复用性和维护性 - 支持对不同模型的统一数据访问接口设计 --- internal/core/base_repository.go | 19 +++++++++++++++++++ internal/repository/api_key_repo.go | 10 ++++++++++ internal/repository/channel_repo.go | 10 ++++++++++ internal/repository/model_repo.go | 10 ++++++++++ 4 files changed, 49 insertions(+) create mode 100644 internal/repository/api_key_repo.go create mode 100644 internal/repository/channel_repo.go create mode 100644 internal/repository/model_repo.go diff --git a/internal/core/base_repository.go b/internal/core/base_repository.go index 395a3bf..d549953 100644 --- a/internal/core/base_repository.go +++ b/internal/core/base_repository.go @@ -27,3 +27,22 @@ type BaseRepo[T any] struct { func (repo *BaseRepo[T]) Insert(ctx context.Context, record *T) error { return repo.db.WithContext(ctx).Create(record).Error } + +func (repo *BaseRepo[T]) SelectById(ctx context.Context, id int64) (*T, error) { + var result T + return &result, repo.db.WithContext(ctx).First(&result, id).Error +} + +func (repo *BaseRepo[T]) List(ctx context.Context) ([]*T, error) { + var results []*T + return results, repo.db.WithContext(ctx).Find(&results).Error +} + +func (repo *BaseRepo[T]) Update(ctx context.Context, record *T) error { + return repo.db.WithContext(ctx).Save(record).Error +} + +func (repo *BaseRepo[T]) Delete(ctx context.Context, id int64) error { + var model T + return repo.db.WithContext(ctx).Delete(&model, id).Error +} diff --git a/internal/repository/api_key_repo.go b/internal/repository/api_key_repo.go new file mode 100644 index 0000000..0af53dd --- /dev/null +++ b/internal/repository/api_key_repo.go @@ -0,0 +1,10 @@ +package repository + +import ( + "synoth.com/janus/internal/core" + "synoth.com/janus/internal/model" +) + +type APIKeyRepo struct { + core.BaseRepo[model.APIKey] +} diff --git a/internal/repository/channel_repo.go b/internal/repository/channel_repo.go new file mode 100644 index 0000000..891a7cf --- /dev/null +++ b/internal/repository/channel_repo.go @@ -0,0 +1,10 @@ +package repository + +import ( + "synoth.com/janus/internal/core" + "synoth.com/janus/internal/model" +) + +type ChannelRepo struct { + core.BaseRepo[model.Channel] +} diff --git a/internal/repository/model_repo.go b/internal/repository/model_repo.go new file mode 100644 index 0000000..6208ce9 --- /dev/null +++ b/internal/repository/model_repo.go @@ -0,0 +1,10 @@ +package repository + +import ( + "synoth.com/janus/internal/core" + "synoth.com/janus/internal/model" +) + +type ModelRepo struct { + core.BaseRepo[model.Model] +}