feat(postgres): added GetUserSourceLinks

This commit is contained in:
SoXX 2024-06-14 15:03:34 +02:00
parent 4f8e54f09a
commit 16d5a381df
2 changed files with 42 additions and 2 deletions

View File

@ -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
}

View File

@ -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) {