feat(postgres): added GetAnthroveUser

This commit is contained in:
SoXX 2024-06-14 15:07:23 +02:00
parent bbd08b9a2f
commit 4cdd4450d5
2 changed files with 52 additions and 2 deletions

View File

@ -160,3 +160,54 @@ func GetSpecifiedUserSourceLink(ctx context.Context, db *gorm.DB, anthroveUserID
return userSourceMap, nil return userSourceMap, nil
} }
func GetAnthroveUser(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) (*graphModels.AnthroveUser, error) {
var user pgModels.User
var userSources []pgModels.UserSource
anthroveUser := &graphModels.AnthroveUser{
UserID: anthroveUserID,
}
err := db.WithContext(ctx).First(&user, "id = ?", string(anthroveUserID)).Error
if err != nil {
log.WithFields(log.Fields{
"anthrove_user_id": anthroveUserID,
}).Error("database: failed to get user")
return nil, err
}
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 sources")
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
}
anthroveUser.Relationship = append(anthroveUser.Relationship, 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 anthrove user")
return anthroveUser, nil
}

View File

@ -114,8 +114,7 @@ func (p *postgresqlConnection) GetSpecifiedUserSourceLink(ctx context.Context, a
} }
func (p *postgresqlConnection) GetAnthroveUser(ctx context.Context, anthroveUserID models.AnthroveUserID) (*graphModels.AnthroveUser, error) { func (p *postgresqlConnection) GetAnthroveUser(ctx context.Context, anthroveUserID models.AnthroveUserID) (*graphModels.AnthroveUser, error) {
//TODO implement me return postgres.GetAnthroveUser(ctx, p.db, anthroveUserID)
panic("implement me")
} }
func (p *postgresqlConnection) GetAllAnthroveUserIDs(ctx context.Context) ([]models.AnthroveUserID, error) { func (p *postgresqlConnection) GetAllAnthroveUserIDs(ctx context.Context) ([]models.AnthroveUserID, error) {