refactor: Update tenant ID handling across the application
This commit is contained in:
@@ -29,7 +29,7 @@ func (r *chunkRepository) CreateChunks(ctx context.Context, chunks []*types.Chun
|
||||
}
|
||||
|
||||
// GetChunkByID retrieves a chunk by its ID and tenant ID
|
||||
func (r *chunkRepository) GetChunkByID(ctx context.Context, tenantID uint, id string) (*types.Chunk, error) {
|
||||
func (r *chunkRepository) GetChunkByID(ctx context.Context, tenantID uint64, id string) (*types.Chunk, error) {
|
||||
var chunk types.Chunk
|
||||
if err := r.db.WithContext(ctx).Where("tenant_id = ? AND id = ?", tenantID, id).First(&chunk).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@@ -42,7 +42,7 @@ func (r *chunkRepository) GetChunkByID(ctx context.Context, tenantID uint, id st
|
||||
|
||||
// ListChunksByID retrieves multiple chunks by their IDs
|
||||
func (r *chunkRepository) ListChunksByID(
|
||||
ctx context.Context, tenantID uint, ids []string,
|
||||
ctx context.Context, tenantID uint64, ids []string,
|
||||
) ([]*types.Chunk, error) {
|
||||
var chunks []*types.Chunk
|
||||
if err := r.db.Debug().WithContext(ctx).
|
||||
@@ -55,7 +55,7 @@ func (r *chunkRepository) ListChunksByID(
|
||||
|
||||
// ListChunksByKnowledgeID lists all chunks for a knowledge ID
|
||||
func (r *chunkRepository) ListChunksByKnowledgeID(
|
||||
ctx context.Context, tenantID uint, knowledgeID string,
|
||||
ctx context.Context, tenantID uint64, knowledgeID string,
|
||||
) ([]*types.Chunk, error) {
|
||||
var chunks []*types.Chunk
|
||||
if err := r.db.WithContext(ctx).
|
||||
@@ -71,7 +71,7 @@ func (r *chunkRepository) ListChunksByKnowledgeID(
|
||||
// ListPagedChunksByKnowledgeID lists chunks for a knowledge ID with pagination
|
||||
func (r *chunkRepository) ListPagedChunksByKnowledgeID(
|
||||
ctx context.Context,
|
||||
tenantID uint,
|
||||
tenantID uint64,
|
||||
knowledgeID string,
|
||||
page *types.Pagination,
|
||||
chunkType []types.ChunkType,
|
||||
@@ -112,7 +112,7 @@ func (r *chunkRepository) ListPagedChunksByKnowledgeID(
|
||||
return chunks, total, nil
|
||||
}
|
||||
|
||||
func (r *chunkRepository) ListChunkByParentID(ctx context.Context, tenantID uint, parentID string) ([]*types.Chunk, error) {
|
||||
func (r *chunkRepository) ListChunkByParentID(ctx context.Context, tenantID uint64, parentID string) ([]*types.Chunk, error) {
|
||||
var chunks []*types.Chunk
|
||||
if err := r.db.WithContext(ctx).
|
||||
Where("tenant_id = ? AND parent_chunk_id = ?", tenantID, parentID).
|
||||
@@ -139,12 +139,12 @@ func (r *chunkRepository) UpdateChunks(ctx context.Context, chunks []*types.Chun
|
||||
}
|
||||
|
||||
// DeleteChunk deletes a chunk by its ID
|
||||
func (r *chunkRepository) DeleteChunk(ctx context.Context, tenantID uint, id string) error {
|
||||
func (r *chunkRepository) DeleteChunk(ctx context.Context, tenantID uint64, id string) error {
|
||||
return r.db.WithContext(ctx).Where("tenant_id = ? AND id = ?", tenantID, id).Delete(&types.Chunk{}).Error
|
||||
}
|
||||
|
||||
// DeleteChunks deletes chunks by IDs in batch
|
||||
func (r *chunkRepository) DeleteChunks(ctx context.Context, tenantID uint, ids []string) error {
|
||||
func (r *chunkRepository) DeleteChunks(ctx context.Context, tenantID uint64, ids []string) error {
|
||||
if len(ids) == 0 {
|
||||
return nil
|
||||
}
|
||||
@@ -152,21 +152,21 @@ func (r *chunkRepository) DeleteChunks(ctx context.Context, tenantID uint, ids [
|
||||
}
|
||||
|
||||
// DeleteChunksByKnowledgeID deletes all chunks for a knowledge ID
|
||||
func (r *chunkRepository) DeleteChunksByKnowledgeID(ctx context.Context, tenantID uint, knowledgeID string) error {
|
||||
func (r *chunkRepository) DeleteChunksByKnowledgeID(ctx context.Context, tenantID uint64, knowledgeID string) error {
|
||||
return r.db.WithContext(ctx).Where(
|
||||
"tenant_id = ? AND knowledge_id = ?", tenantID, knowledgeID,
|
||||
).Delete(&types.Chunk{}).Error
|
||||
}
|
||||
|
||||
// DeleteByKnowledgeList deletes all chunks for a knowledge list
|
||||
func (r *chunkRepository) DeleteByKnowledgeList(ctx context.Context, tenantID uint, knowledgeIDs []string) error {
|
||||
func (r *chunkRepository) DeleteByKnowledgeList(ctx context.Context, tenantID uint64, knowledgeIDs []string) error {
|
||||
return r.db.WithContext(ctx).Where(
|
||||
"tenant_id = ? AND knowledge_id in ?", tenantID, knowledgeIDs,
|
||||
).Delete(&types.Chunk{}).Error
|
||||
}
|
||||
|
||||
// CountChunksByKnowledgeBaseID counts the number of chunks in a knowledge base
|
||||
func (r *chunkRepository) CountChunksByKnowledgeBaseID(ctx context.Context, tenantID uint, kbID string) (int64, error) {
|
||||
func (r *chunkRepository) CountChunksByKnowledgeBaseID(ctx context.Context, tenantID uint64, kbID string) (int64, error) {
|
||||
var count int64
|
||||
err := r.db.WithContext(ctx).Model(&types.Chunk{}).
|
||||
Where("tenant_id = ? AND knowledge_base_id = ?", tenantID, kbID).
|
||||
@@ -175,7 +175,7 @@ func (r *chunkRepository) CountChunksByKnowledgeBaseID(ctx context.Context, tena
|
||||
}
|
||||
|
||||
// DeleteUnindexedChunks by knowledge id and chunk index range
|
||||
func (r *chunkRepository) DeleteUnindexedChunks(ctx context.Context, tenantID uint, knowledgeID string) ([]*types.Chunk, error) {
|
||||
func (r *chunkRepository) DeleteUnindexedChunks(ctx context.Context, tenantID uint64, knowledgeID string) ([]*types.Chunk, error) {
|
||||
var chunks []*types.Chunk
|
||||
if err := r.db.WithContext(ctx).
|
||||
Where("tenant_id = ? AND knowledge_id = ? AND status = ?", tenantID, knowledgeID, types.ChunkStatusStored).
|
||||
@@ -194,7 +194,7 @@ func (r *chunkRepository) DeleteUnindexedChunks(ctx context.Context, tenantID ui
|
||||
|
||||
// ListAllFAQChunksByKnowledgeID lists all FAQ chunks for a knowledge ID (only essential fields for efficiency)
|
||||
// Uses batch query to handle large datasets
|
||||
func (r *chunkRepository) ListAllFAQChunksByKnowledgeID(ctx context.Context, tenantID uint, knowledgeID string) ([]*types.Chunk, error) {
|
||||
func (r *chunkRepository) ListAllFAQChunksByKnowledgeID(ctx context.Context, tenantID uint64, knowledgeID string) ([]*types.Chunk, error) {
|
||||
const batchSize = 1000 // 每批查询1000条
|
||||
var allChunks []*types.Chunk
|
||||
offset := 0
|
||||
|
||||
@@ -28,7 +28,7 @@ func (r *knowledgeRepository) CreateKnowledge(ctx context.Context, knowledge *ty
|
||||
}
|
||||
|
||||
// GetKnowledgeByID gets knowledge
|
||||
func (r *knowledgeRepository) GetKnowledgeByID(ctx context.Context, tenantID uint, id string) (*types.Knowledge, error) {
|
||||
func (r *knowledgeRepository) GetKnowledgeByID(ctx context.Context, tenantID uint64, id string) (*types.Knowledge, error) {
|
||||
var knowledge types.Knowledge
|
||||
if err := r.db.WithContext(ctx).Where("tenant_id = ? AND id = ?", tenantID, id).First(&knowledge).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@@ -41,7 +41,7 @@ func (r *knowledgeRepository) GetKnowledgeByID(ctx context.Context, tenantID uin
|
||||
|
||||
// ListKnowledgeByKnowledgeBaseID lists all knowledge in a knowledge base
|
||||
func (r *knowledgeRepository) ListKnowledgeByKnowledgeBaseID(
|
||||
ctx context.Context, tenantID uint, kbID string,
|
||||
ctx context.Context, tenantID uint64, kbID string,
|
||||
) ([]*types.Knowledge, error) {
|
||||
var knowledges []*types.Knowledge
|
||||
if err := r.db.WithContext(ctx).Where("tenant_id = ? AND knowledge_base_id = ?", tenantID, kbID).
|
||||
@@ -54,7 +54,7 @@ func (r *knowledgeRepository) ListKnowledgeByKnowledgeBaseID(
|
||||
// ListPagedKnowledgeByKnowledgeBaseID lists all knowledge in a knowledge base with pagination
|
||||
func (r *knowledgeRepository) ListPagedKnowledgeByKnowledgeBaseID(
|
||||
ctx context.Context,
|
||||
tenantID uint,
|
||||
tenantID uint64,
|
||||
kbID string,
|
||||
page *types.Pagination,
|
||||
tagID string,
|
||||
@@ -106,18 +106,18 @@ func (r *knowledgeRepository) UpdateKnowledgeBatch(ctx context.Context, knowledg
|
||||
}
|
||||
|
||||
// DeleteKnowledge deletes knowledge
|
||||
func (r *knowledgeRepository) DeleteKnowledge(ctx context.Context, tenantID uint, id string) error {
|
||||
func (r *knowledgeRepository) DeleteKnowledge(ctx context.Context, tenantID uint64, id string) error {
|
||||
return r.db.WithContext(ctx).Where("tenant_id = ? AND id = ?", tenantID, id).Delete(&types.Knowledge{}).Error
|
||||
}
|
||||
|
||||
// DeleteKnowledge deletes knowledge
|
||||
func (r *knowledgeRepository) DeleteKnowledgeList(ctx context.Context, tenantID uint, ids []string) error {
|
||||
func (r *knowledgeRepository) DeleteKnowledgeList(ctx context.Context, tenantID uint64, ids []string) error {
|
||||
return r.db.WithContext(ctx).Where("tenant_id = ? AND id in ?", tenantID, ids).Delete(&types.Knowledge{}).Error
|
||||
}
|
||||
|
||||
// GetKnowledgeBatch gets knowledge in batch
|
||||
func (r *knowledgeRepository) GetKnowledgeBatch(
|
||||
ctx context.Context, tenantID uint, ids []string,
|
||||
ctx context.Context, tenantID uint64, ids []string,
|
||||
) ([]*types.Knowledge, error) {
|
||||
var knowledge []*types.Knowledge
|
||||
if err := r.db.WithContext(ctx).Debug().
|
||||
@@ -131,7 +131,7 @@ func (r *knowledgeRepository) GetKnowledgeBatch(
|
||||
// CheckKnowledgeExists checks if knowledge already exists
|
||||
func (r *knowledgeRepository) CheckKnowledgeExists(
|
||||
ctx context.Context,
|
||||
tenantID uint,
|
||||
tenantID uint64,
|
||||
kbID string,
|
||||
params *types.KnowledgeCheckParams,
|
||||
) (bool, *types.Knowledge, error) {
|
||||
@@ -199,8 +199,8 @@ func (r *knowledgeRepository) CheckKnowledgeExists(
|
||||
|
||||
func (r *knowledgeRepository) AminusB(
|
||||
ctx context.Context,
|
||||
Atenant uint, A string,
|
||||
Btenant uint, B string,
|
||||
Atenant uint64, A string,
|
||||
Btenant uint64, B string,
|
||||
) ([]string, error) {
|
||||
knowledgeIDs := []string{}
|
||||
subQuery := r.db.Model(&types.Knowledge{}).
|
||||
@@ -222,7 +222,7 @@ func (r *knowledgeRepository) UpdateKnowledgeColumn(ctx context.Context, id stri
|
||||
}
|
||||
|
||||
// CountKnowledgeByKnowledgeBaseID counts the number of knowledge items in a knowledge base
|
||||
func (r *knowledgeRepository) CountKnowledgeByKnowledgeBaseID(ctx context.Context, tenantID uint, kbID string) (int64, error) {
|
||||
func (r *knowledgeRepository) CountKnowledgeByKnowledgeBaseID(ctx context.Context, tenantID uint64, kbID string) (int64, error) {
|
||||
var count int64
|
||||
err := r.db.WithContext(ctx).Model(&types.Knowledge{}).
|
||||
Where("tenant_id = ? AND knowledge_base_id = ?", tenantID, kbID).
|
||||
@@ -233,7 +233,7 @@ func (r *knowledgeRepository) CountKnowledgeByKnowledgeBaseID(ctx context.Contex
|
||||
// CountKnowledgeByStatus counts the number of knowledge items with the specified parse status
|
||||
func (r *knowledgeRepository) CountKnowledgeByStatus(
|
||||
ctx context.Context,
|
||||
tenantID uint,
|
||||
tenantID uint64,
|
||||
kbID string,
|
||||
parseStatuses []string,
|
||||
) (int64, error) {
|
||||
|
||||
@@ -49,7 +49,7 @@ func (r *knowledgeBaseRepository) ListKnowledgeBases(ctx context.Context) ([]*ty
|
||||
|
||||
// ListKnowledgeBasesByTenantID lists all knowledge bases by tenant id
|
||||
func (r *knowledgeBaseRepository) ListKnowledgeBasesByTenantID(
|
||||
ctx context.Context, tenantID uint,
|
||||
ctx context.Context, tenantID uint64,
|
||||
) ([]*types.KnowledgeBase, error) {
|
||||
var kbs []*types.KnowledgeBase
|
||||
if err := r.db.WithContext(ctx).Where("tenant_id = ? AND is_temporary = ?", tenantID, false).
|
||||
|
||||
@@ -25,7 +25,7 @@ func (r *mcpServiceRepository) Create(ctx context.Context, service *types.MCPSer
|
||||
}
|
||||
|
||||
// GetByID retrieves an MCP service by ID and tenant ID
|
||||
func (r *mcpServiceRepository) GetByID(ctx context.Context, tenantID uint, id string) (*types.MCPService, error) {
|
||||
func (r *mcpServiceRepository) GetByID(ctx context.Context, tenantID uint64, id string) (*types.MCPService, error) {
|
||||
var service types.MCPService
|
||||
err := r.db.WithContext(ctx).
|
||||
Where("id = ? AND tenant_id = ?", id, tenantID).
|
||||
@@ -42,7 +42,7 @@ func (r *mcpServiceRepository) GetByID(ctx context.Context, tenantID uint, id st
|
||||
}
|
||||
|
||||
// List retrieves all MCP services for a tenant
|
||||
func (r *mcpServiceRepository) List(ctx context.Context, tenantID uint) ([]*types.MCPService, error) {
|
||||
func (r *mcpServiceRepository) List(ctx context.Context, tenantID uint64) ([]*types.MCPService, error) {
|
||||
var services []*types.MCPService
|
||||
err := r.db.WithContext(ctx).
|
||||
Where("tenant_id = ?", tenantID).
|
||||
@@ -57,7 +57,7 @@ func (r *mcpServiceRepository) List(ctx context.Context, tenantID uint) ([]*type
|
||||
}
|
||||
|
||||
// ListEnabled retrieves all enabled MCP services for a tenant
|
||||
func (r *mcpServiceRepository) ListEnabled(ctx context.Context, tenantID uint) ([]*types.MCPService, error) {
|
||||
func (r *mcpServiceRepository) ListEnabled(ctx context.Context, tenantID uint64) ([]*types.MCPService, error) {
|
||||
var services []*types.MCPService
|
||||
err := r.db.WithContext(ctx).
|
||||
Where("tenant_id = ? AND enabled = ?", tenantID, true).
|
||||
@@ -72,7 +72,7 @@ func (r *mcpServiceRepository) ListEnabled(ctx context.Context, tenantID uint) (
|
||||
}
|
||||
|
||||
// ListByIDs retrieves MCP services by multiple IDs for a tenant
|
||||
func (r *mcpServiceRepository) ListByIDs(ctx context.Context, tenantID uint, ids []string) ([]*types.MCPService, error) {
|
||||
func (r *mcpServiceRepository) ListByIDs(ctx context.Context, tenantID uint64, ids []string) ([]*types.MCPService, error) {
|
||||
if len(ids) == 0 {
|
||||
return []*types.MCPService{}, nil
|
||||
}
|
||||
@@ -134,7 +134,7 @@ func (r *mcpServiceRepository) Update(ctx context.Context, service *types.MCPSer
|
||||
}
|
||||
|
||||
// Delete deletes an MCP service (soft delete)
|
||||
func (r *mcpServiceRepository) Delete(ctx context.Context, tenantID uint, id string) error {
|
||||
func (r *mcpServiceRepository) Delete(ctx context.Context, tenantID uint64, id string) error {
|
||||
return r.db.WithContext(ctx).
|
||||
Where("id = ? AND tenant_id = ?", id, tenantID).
|
||||
Delete(&types.MCPService{}).Error
|
||||
|
||||
@@ -25,7 +25,7 @@ func (r *modelRepository) Create(ctx context.Context, m *types.Model) error {
|
||||
}
|
||||
|
||||
// GetByID retrieves a model by ID
|
||||
func (r *modelRepository) GetByID(ctx context.Context, tenantID uint, id string) (*types.Model, error) {
|
||||
func (r *modelRepository) GetByID(ctx context.Context, tenantID uint64, id string) (*types.Model, error) {
|
||||
var m types.Model
|
||||
if err := r.db.WithContext(ctx).Where("id = ?", id).Where(
|
||||
"tenant_id = ? OR is_builtin = true", tenantID,
|
||||
@@ -40,7 +40,7 @@ func (r *modelRepository) GetByID(ctx context.Context, tenantID uint, id string)
|
||||
|
||||
// List lists models with optional filtering
|
||||
func (r *modelRepository) List(
|
||||
ctx context.Context, tenantID uint, modelType types.ModelType, source types.ModelSource,
|
||||
ctx context.Context, tenantID uint64, modelType types.ModelType, source types.ModelSource,
|
||||
) ([]*types.Model, error) {
|
||||
var models []*types.Model
|
||||
query := r.db.WithContext(ctx).Where(
|
||||
@@ -71,7 +71,7 @@ func (r *modelRepository) Update(ctx context.Context, m *types.Model) error {
|
||||
}
|
||||
|
||||
// Delete deletes a model
|
||||
func (r *modelRepository) Delete(ctx context.Context, tenantID uint, id string) error {
|
||||
func (r *modelRepository) Delete(ctx context.Context, tenantID uint64, id string) error {
|
||||
return r.db.WithContext(ctx).Where(
|
||||
"id = ? AND tenant_id = ?", id, tenantID,
|
||||
).Delete(&types.Model{}).Error
|
||||
|
||||
@@ -31,7 +31,7 @@ func (r *sessionRepository) Create(ctx context.Context, session *types.Session)
|
||||
}
|
||||
|
||||
// Get retrieves a session by ID
|
||||
func (r *sessionRepository) Get(ctx context.Context, tenantID uint, id string) (*types.Session, error) {
|
||||
func (r *sessionRepository) Get(ctx context.Context, tenantID uint64, id string) (*types.Session, error) {
|
||||
var session types.Session
|
||||
err := r.db.WithContext(ctx).Where("tenant_id = ?", tenantID).First(&session, "id = ?", id).Error
|
||||
if err != nil {
|
||||
@@ -41,7 +41,7 @@ func (r *sessionRepository) Get(ctx context.Context, tenantID uint, id string) (
|
||||
}
|
||||
|
||||
// GetByTenantID retrieves all sessions for a tenant
|
||||
func (r *sessionRepository) GetByTenantID(ctx context.Context, tenantID uint) ([]*types.Session, error) {
|
||||
func (r *sessionRepository) GetByTenantID(ctx context.Context, tenantID uint64) ([]*types.Session, error) {
|
||||
var sessions []*types.Session
|
||||
err := r.db.WithContext(ctx).Where("tenant_id = ?", tenantID).Order("created_at DESC").Find(&sessions).Error
|
||||
if err != nil {
|
||||
@@ -52,7 +52,7 @@ func (r *sessionRepository) GetByTenantID(ctx context.Context, tenantID uint) ([
|
||||
|
||||
// GetPagedByTenantID retrieves sessions for a tenant with pagination
|
||||
func (r *sessionRepository) GetPagedByTenantID(
|
||||
ctx context.Context, tenantID uint, page *types.Pagination,
|
||||
ctx context.Context, tenantID uint64, page *types.Pagination,
|
||||
) ([]*types.Session, int64, error) {
|
||||
var sessions []*types.Session
|
||||
var total int64
|
||||
@@ -84,6 +84,6 @@ func (r *sessionRepository) Update(ctx context.Context, session *types.Session)
|
||||
}
|
||||
|
||||
// Delete deletes a session
|
||||
func (r *sessionRepository) Delete(ctx context.Context, tenantID uint, id string) error {
|
||||
func (r *sessionRepository) Delete(ctx context.Context, tenantID uint64, id string) error {
|
||||
return r.db.WithContext(ctx).Where("tenant_id = ?", tenantID).Delete(&types.Session{}, "id = ?", id).Error
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ func (r *knowledgeTagRepository) Update(ctx context.Context, tag *types.Knowledg
|
||||
return r.db.WithContext(ctx).Save(tag).Error
|
||||
}
|
||||
|
||||
func (r *knowledgeTagRepository) GetByID(ctx context.Context, tenantID uint, id string) (*types.KnowledgeTag, error) {
|
||||
func (r *knowledgeTagRepository) GetByID(ctx context.Context, tenantID uint64, id string) (*types.KnowledgeTag, error) {
|
||||
var tag types.KnowledgeTag
|
||||
if err := r.db.WithContext(ctx).
|
||||
Where("tenant_id = ? AND id = ?", tenantID, id).
|
||||
@@ -35,7 +35,7 @@ func (r *knowledgeTagRepository) GetByID(ctx context.Context, tenantID uint, id
|
||||
return &tag, nil
|
||||
}
|
||||
|
||||
func (r *knowledgeTagRepository) ListByKB(ctx context.Context, tenantID uint, kbID string) ([]*types.KnowledgeTag, error) {
|
||||
func (r *knowledgeTagRepository) ListByKB(ctx context.Context, tenantID uint64, kbID string) ([]*types.KnowledgeTag, error) {
|
||||
var tags []*types.KnowledgeTag
|
||||
if err := r.db.WithContext(ctx).
|
||||
Where("tenant_id = ? AND knowledge_base_id = ?", tenantID, kbID).
|
||||
@@ -46,7 +46,7 @@ func (r *knowledgeTagRepository) ListByKB(ctx context.Context, tenantID uint, kb
|
||||
return tags, nil
|
||||
}
|
||||
|
||||
func (r *knowledgeTagRepository) Delete(ctx context.Context, tenantID uint, id string) error {
|
||||
func (r *knowledgeTagRepository) Delete(ctx context.Context, tenantID uint64, id string) error {
|
||||
return r.db.WithContext(ctx).
|
||||
Where("tenant_id = ? AND id = ?", tenantID, id).
|
||||
Delete(&types.KnowledgeTag{}).Error
|
||||
@@ -55,7 +55,7 @@ func (r *knowledgeTagRepository) Delete(ctx context.Context, tenantID uint, id s
|
||||
// CountReferences returns how many knowledges and chunks reference this tag.
|
||||
func (r *knowledgeTagRepository) CountReferences(
|
||||
ctx context.Context,
|
||||
tenantID uint,
|
||||
tenantID uint64,
|
||||
kbID string,
|
||||
tagID string,
|
||||
) (knowledgeCount int64, chunkCount int64, err error) {
|
||||
|
||||
@@ -32,7 +32,7 @@ func (r *tenantRepository) CreateTenant(ctx context.Context, tenant *types.Tenan
|
||||
}
|
||||
|
||||
// GetTenantByID gets tenant by ID
|
||||
func (r *tenantRepository) GetTenantByID(ctx context.Context, id uint) (*types.Tenant, error) {
|
||||
func (r *tenantRepository) GetTenantByID(ctx context.Context, id uint64) (*types.Tenant, error) {
|
||||
var tenant types.Tenant
|
||||
if err := r.db.WithContext(ctx).Where("id = ?", id).First(&tenant).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@@ -58,11 +58,11 @@ func (r *tenantRepository) UpdateTenant(ctx context.Context, tenant *types.Tenan
|
||||
}
|
||||
|
||||
// DeleteTenant deletes tenant
|
||||
func (r *tenantRepository) DeleteTenant(ctx context.Context, id uint) error {
|
||||
func (r *tenantRepository) DeleteTenant(ctx context.Context, id uint64) error {
|
||||
return r.db.WithContext(ctx).Where("id = ?", id).Delete(&types.Tenant{}).Error
|
||||
}
|
||||
|
||||
func (r *tenantRepository) AdjustStorageUsed(ctx context.Context, tenantID uint, delta int64) error {
|
||||
func (r *tenantRepository) AdjustStorageUsed(ctx context.Context, tenantID uint64, delta int64) error {
|
||||
return r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
var tenant types.Tenant
|
||||
// 使用悲观锁确保并发安全
|
||||
|
||||
@@ -4,16 +4,15 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/logger"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrUserNotFound = errors.New("user not found")
|
||||
ErrUserNotFound = errors.New("user not found")
|
||||
ErrUserAlreadyExists = errors.New("user already exists")
|
||||
ErrTokenNotFound = errors.New("token not found")
|
||||
ErrTokenNotFound = errors.New("token not found")
|
||||
)
|
||||
|
||||
// userRepository implements user repository interface
|
||||
@@ -28,7 +27,6 @@ func NewUserRepository(db *gorm.DB) interfaces.UserRepository {
|
||||
|
||||
// CreateUser creates a user
|
||||
func (r *userRepository) CreateUser(ctx context.Context, user *types.User) error {
|
||||
logger.Infof(ctx, "Creating user in database: %s", user.Email)
|
||||
return r.db.WithContext(ctx).Create(user).Error
|
||||
}
|
||||
|
||||
@@ -82,15 +80,15 @@ func (r *userRepository) DeleteUser(ctx context.Context, id string) error {
|
||||
func (r *userRepository) ListUsers(ctx context.Context, offset, limit int) ([]*types.User, error) {
|
||||
var users []*types.User
|
||||
query := r.db.WithContext(ctx).Order("created_at DESC")
|
||||
|
||||
|
||||
if limit > 0 {
|
||||
query = query.Limit(limit)
|
||||
}
|
||||
|
||||
|
||||
if offset > 0 {
|
||||
query = query.Offset(offset)
|
||||
}
|
||||
|
||||
|
||||
if err := query.Find(&users).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -151,4 +149,4 @@ func (r *authTokenRepository) DeleteExpiredTokens(ctx context.Context) error {
|
||||
// RevokeTokensByUserID revokes all tokens for a user
|
||||
func (r *authTokenRepository) RevokeTokensByUserID(ctx context.Context, userID string) error {
|
||||
return r.db.WithContext(ctx).Model(&types.AuthToken{}).Where("user_id = ?", userID).Update("is_revoked", true).Error
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user