Files
xinmi/internal/application/repository/resource.go
T
yawan f7fc992f57
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-weknora) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (latest, advisory) (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Build & Publish (dsh-weknora) / Publish to npm (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
WeKnora deployment source with the reviewed wiki identity, summary and recovery fixes
2026-09-22 16:19:06 +08:00

118 lines
3.9 KiB
Go

package repository
import (
"context"
"errors"
"time"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
type resourceRepository struct{ db *gorm.DB }
// NewResourceRepository creates the persistence adapter for resource metadata.
func NewResourceRepository(db *gorm.DB) interfaces.ResourceRepository {
return &resourceRepository{db: db}
}
func (r *resourceRepository) Create(ctx context.Context, resource *types.StoredResource) error {
return r.db.WithContext(ctx).Create(resource).Error
}
func (r *resourceRepository) GetByID(ctx context.Context, id string) (*types.StoredResource, error) {
var resource types.StoredResource
err := r.db.WithContext(ctx).Where("id = ? AND state = ?", id, types.ResourceStateActive).First(&resource).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
return &resource, err
}
func (r *resourceRepository) GetByHandle(ctx context.Context, handle string) (*types.StoredResource, error) {
var resource types.StoredResource
err := r.db.WithContext(ctx).
Where("handle = ? AND state = ?", handle, types.ResourceStateActive).
First(&resource).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
return &resource, err
}
func (r *resourceRepository) GetByTenantLocation(
ctx context.Context,
tenantID uint64,
locationHash string,
) (*types.StoredResource, error) {
var resource types.StoredResource
err := r.db.WithContext(ctx).
Where(
"tenant_id = ? AND location_hash = ? AND state = ?",
tenantID,
locationHash,
types.ResourceStateActive,
).
First(&resource).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
return &resource, err
}
func (r *resourceRepository) MarkDeleted(ctx context.Context, id string) error {
return r.db.WithContext(ctx).Model(&types.StoredResource{}).Where("id = ?", id).
Updates(map[string]interface{}{"state": types.ResourceStateDeleted, "deleted_at": time.Now()}).Error
}
func (r *resourceRepository) CreateBinding(ctx context.Context, binding *types.ResourceBinding) error {
return r.db.WithContext(ctx).Clauses(clause.OnConflict{DoNothing: true}).Create(binding).Error
}
// DeleteBinding removes one owner's claim on a resource. Deleting a claim that
// was never recorded is not an error: callers release optimistically, from a
// content scan that cannot know which references were bound.
func (r *resourceRepository) DeleteBinding(ctx context.Context, resourceID, ownerType, ownerID string) error {
return r.db.WithContext(ctx).
Where("resource_id = ? AND owner_type = ? AND owner_id = ?", resourceID, ownerType, ownerID).
Delete(&types.ResourceBinding{}).Error
}
func (r *resourceRepository) CountBindings(ctx context.Context, resourceID string) (int64, error) {
var count int64
err := r.db.WithContext(ctx).Model(&types.ResourceBinding{}).
Where("resource_id = ?", resourceID).
Count(&count).Error
return count, err
}
func (r *resourceRepository) CreateGrant(ctx context.Context, grant *types.ResourceAccessGrant) error {
return r.db.WithContext(ctx).Create(grant).Error
}
func (r *resourceRepository) GetValidGrant(
ctx context.Context,
tokenHash string,
now time.Time,
) (*types.ResourceAccessGrant, error) {
var grant types.ResourceAccessGrant
err := r.db.WithContext(ctx).
Where("token_hash = ? AND revoked_at IS NULL AND expires_at > ?", tokenHash, now).
First(&grant).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
return &grant, err
}
// DeleteExpiredGrants drops grants that are past their expiry. A revoked grant
// is kept until then on purpose: it is the tombstone that stops a token derived
// for the same resource and window from re-creating the row and reviving access.
func (r *resourceRepository) DeleteExpiredGrants(ctx context.Context, before time.Time) error {
return r.db.WithContext(ctx).
Where("expires_at <= ?", before).
Delete(&types.ResourceAccessGrant{}).Error
}