diff --git a/internal/postgres/user.go b/internal/postgres/user.go index acd9695..a2e60fe 100644 --- a/internal/postgres/user.go +++ b/internal/postgres/user.go @@ -3,6 +3,7 @@ package postgres import ( "context" "git.dragse.it/anthrove/otter-space-sdk/pkg/models" + "git.dragse.it/anthrove/otter-space-sdk/pkg/models/graphModels" "git.dragse.it/anthrove/otter-space-sdk/pkg/models/pgModels" log "github.com/sirupsen/logrus" "gorm.io/gorm" @@ -77,3 +78,43 @@ func GetUserFavoritesCount(ctx context.Context, db *gorm.DB, anthroveUserID mode return count, nil } + +func GetUserSourceLink(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) (map[string]graphModels.AnthroveUserRelationship, error) { + var userSources []pgModels.UserSource + userSourceMap := make(map[string]graphModels.AnthroveUserRelationship) + + err := db.WithContext(ctx).Model(&pgModels.UserSource{}).Where("user_id = ?", string(anthroveUserID)).Find(&userSources).Error + if err != nil { + log.WithFields(log.Fields{ + "anthrove_user_id": anthroveUserID, + }).Error("database: failed to get user source link") + return nil, err + } + + for _, userSource := range userSources { + var source pgModels.Source + err = db.WithContext(ctx).Model(&pgModels.Source{}).Where("id = ?", userSource.SourceID).First(&source).Error + if err != nil { + log.WithFields(log.Fields{ + "source_id": userSource.SourceID, + }).Error("database: failed to get source") + return nil, err + } + + userSourceMap[source.DisplayName] = graphModels.AnthroveUserRelationship{ + UserID: userSource.AccountID, + Username: userSource.AccountUsername, + Source: graphModels.AnthroveSource{ + DisplayName: source.DisplayName, + Domain: source.Domain, + Icon: source.Icon, + }, + } + } + + log.WithFields(log.Fields{ + "anthrove_user_id": anthroveUserID, + }).Trace("database: got user source link") + + return userSourceMap, nil +} diff --git a/pkg/database/postgres.go b/pkg/database/postgres.go index 883de13..7d095b0 100644 --- a/pkg/database/postgres.go +++ b/pkg/database/postgres.go @@ -106,8 +106,7 @@ func (p *postgresqlConnection) GetUserFavoriteCount(ctx context.Context, anthrov } func (p *postgresqlConnection) GetUserSourceLinks(ctx context.Context, anthroveUserID models.AnthroveUserID) (map[string]graphModels.AnthroveUserRelationship, error) { - //TODO implement me - panic("implement me") + return postgres.GetUserSourceLink(ctx, p.db, anthroveUserID) } func (p *postgresqlConnection) GetSpecifiedUserSourceLink(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceDisplayName string) (map[string]graphModels.AnthroveUserRelationship, error) {