diff --git a/internal/postgres/user.go b/internal/postgres/user.go index a2e60fe..b27f55b 100644 --- a/internal/postgres/user.go +++ b/internal/postgres/user.go @@ -118,3 +118,45 @@ func GetUserSourceLink(ctx context.Context, db *gorm.DB, anthroveUserID models.A return userSourceMap, nil } + +func GetSpecifiedUserSourceLink(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, sourceDisplayName string) (map[string]graphModels.AnthroveUserRelationship, error) { + var userSources []pgModels.UserSource + userSourceMap := make(map[string]graphModels.AnthroveUserRelationship) + + err := db.WithContext(ctx).Model(&pgModels.UserSource{}).Joins("Source").Where("user_id = ? AND display_name = ?", string(anthroveUserID), sourceDisplayName).Find(&userSources).Error + if err != nil { + log.WithFields(log.Fields{ + "anthrove_user_id": anthroveUserID, + "source_display_name": sourceDisplayName, + }).Error("database: failed to get specified 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, + "source_display_name": sourceDisplayName, + }).Trace("database: got specified user source link") + + return userSourceMap, nil +} diff --git a/pkg/database/postgres.go b/pkg/database/postgres.go index 7d095b0..b738d79 100644 --- a/pkg/database/postgres.go +++ b/pkg/database/postgres.go @@ -110,8 +110,7 @@ func (p *postgresqlConnection) GetUserSourceLinks(ctx context.Context, anthroveU } func (p *postgresqlConnection) GetSpecifiedUserSourceLink(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceDisplayName string) (map[string]graphModels.AnthroveUserRelationship, error) { - //TODO implement me - panic("implement me") + return postgres.GetSpecifiedUserSourceLink(ctx, p.db, anthroveUserID, sourceDisplayName) } func (p *postgresqlConnection) GetAnthroveUser(ctx context.Context, anthroveUserID models.AnthroveUserID) (*graphModels.AnthroveUser, error) {