feat: associate dataset sources with owners

This commit is contained in:
2026-07-23 21:44:47 +08:00
parent 50d8246915
commit 55ff320cec
7 changed files with 112 additions and 14 deletions

View File

@@ -66,7 +66,7 @@ func newServiceWithFetcher(database *gorm.DB, fetcher FeedFetcher) *Service {
func (s *Service) ListSources(userID uint) ([]SourceRecord, error) {
var sources []models.SaDatasetSource
if err := s.db.Where("created_by = ?", userID).Order("created_at asc, id asc").Find(&sources).Error; err != nil {
if err := s.db.Where("owner_id = ?", userID).Order("created_at asc, id asc").Find(&sources).Error; err != nil {
return nil, err
}
counts := make(map[uint]int64)
@@ -103,7 +103,7 @@ func (s *Service) CreateSource(userID uint, input SourceInput) (*models.SaDatase
return nil, err
}
source := &models.SaDatasetSource{
CreatedBy: userID, Name: normalized.Name, Kind: normalized.Kind,
OwnerID: userID, CreatedBy: userID, Name: normalized.Name, Kind: normalized.Kind,
URL: normalized.URL, IconURL: normalized.IconURL,
Description: normalized.Description, Enabled: normalized.Enabled,
}
@@ -116,7 +116,7 @@ func (s *Service) UpdateSource(userID uint, identity string, input SourceInput)
return nil, err
}
var source models.SaDatasetSource
if err := s.db.Where("identity = ? AND created_by = ?", identity, userID).First(&source).Error; err != nil {
if err := s.db.Where("identity = ? AND owner_id = ?", identity, userID).First(&source).Error; err != nil {
return nil, err
}
if err := s.db.Model(&source).Updates(map[string]any{
@@ -131,7 +131,7 @@ func (s *Service) UpdateSource(userID uint, identity string, input SourceInput)
func (s *Service) DeleteSource(userID uint, identity string) error {
return s.db.Transaction(func(tx *gorm.DB) error {
var source models.SaDatasetSource
if err := tx.Where("identity = ? AND created_by = ?", identity, userID).First(&source).Error; err != nil {
if err := tx.Where("identity = ? AND owner_id = ?", identity, userID).First(&source).Error; err != nil {
return err
}
if err := tx.Where("source_id = ? AND created_by = ?", source.ID, userID).Delete(&models.SaDatasetCron{}).Error; err != nil {
@@ -228,10 +228,10 @@ func (s *Service) SyncSources(ctx context.Context, userID uint) ([]models.SaData
func (s *Service) SyncAllSources(ctx context.Context) ([]models.SaDatasetCron, error) {
var userIDs []uint
if err := s.db.Model(&models.SaDatasetSource{}).
Distinct("created_by").
Distinct("owner_id").
Where("enabled = ? AND kind = ?", true, "rss").
Order("created_by asc").
Pluck("created_by", &userIDs).Error; err != nil {
Order("owner_id asc").
Pluck("owner_id", &userIDs).Error; err != nil {
return nil, err
}
@@ -253,7 +253,7 @@ func (s *Service) SyncAllSources(ctx context.Context) ([]models.SaDatasetCron, e
func (s *Service) syncSources(ctx context.Context, userID uint) ([]models.SaDatasetCron, error) {
result := make([]models.SaDatasetCron, 0)
var sources []models.SaDatasetSource
if err := s.db.Where("created_by = ? AND enabled = ? AND kind = ?", userID, true, "rss").
if err := s.db.Where("owner_id = ? AND enabled = ? AND kind = ?", userID, true, "rss").
Order("id asc").Find(&sources).Error; err != nil {
return nil, err
}
@@ -361,7 +361,7 @@ func truncateRunes(value string, limit int) string {
func (s *Service) findOwnedSource(userID uint, identity string) (*models.SaDatasetSource, error) {
var source models.SaDatasetSource
err := s.db.Where("identity = ? AND created_by = ?", strings.TrimSpace(identity), userID).First(&source).Error
err := s.db.Where("identity = ? AND owner_id = ?", strings.TrimSpace(identity), userID).First(&source).Error
return &source, err
}