diff --git a/internal/postgres/user.go b/internal/postgres/user.go index f149d14..676bfa4 100644 --- a/internal/postgres/user.go +++ b/internal/postgres/user.go @@ -232,3 +232,43 @@ func GetAllAnthroveUserIDs(ctx context.Context, db *gorm.DB) ([]models.AnthroveU return userIDs, nil } + +func GetUserFavoriteNodeWithPagination(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, skip int, limit int) (*graphModels.FavoriteList, error) { + var userFavorites []pgModels.UserFavorite + var favoritePosts []graphModels.FavoritePost + + err := db.WithContext(ctx).Model(&pgModels.UserFavorite{}).Where("user_id = ?", string(anthroveUserID)).Offset(skip).Limit(limit).Find(&userFavorites).Error + if err != nil { + log.WithFields(log.Fields{ + "anthrove_user_id": anthroveUserID, + "skip": skip, + "limit": limit, + }).Error("database: failed to get user favorites with pagination") + return nil, err + } + + for _, userFavorite := range userFavorites { + var post pgModels.Post + err = db.WithContext(ctx).Model(&pgModels.Post{}).Where("id = ?", userFavorite.PostID).First(&post).Error + if err != nil { + log.WithFields(log.Fields{ + "post_id": userFavorite.PostID, + }).Error("database: failed to get post") + return nil, err + } + + favoritePosts = append(favoritePosts, graphModels.FavoritePost{ + AnthrovePost: graphModels.AnthrovePost{ + PostID: models.AnthrovePostID(post.ID), + Rating: post.Rating, + }, + }) + } + + log.WithFields(log.Fields{ + "anthrove_user_id": anthroveUserID, + "anthrove_user_fav_count": len(favoritePosts), + }).Trace("database: got all anthrove user favorites") + + return &graphModels.FavoriteList{Posts: favoritePosts}, nil +} diff --git a/pkg/database/postgres.go b/pkg/database/postgres.go index 7395bdf..a13f15d 100644 --- a/pkg/database/postgres.go +++ b/pkg/database/postgres.go @@ -122,8 +122,7 @@ func (p *postgresqlConnection) GetAllAnthroveUserIDs(ctx context.Context) ([]mod } func (p *postgresqlConnection) GetUserFavoritePostsWithPagination(ctx context.Context, anthroveUserID models.AnthroveUserID, skip int, limit int) (*graphModels.FavoriteList, error) { - //TODO implement me - panic("implement me") + return postgres.GetUserFavoriteNodeWithPagination(ctx, p.db, anthroveUserID, skip, limit) } func (p *postgresqlConnection) GetUserTagsTroughFavedPosts(ctx context.Context, anthroveUserID models.AnthroveUserID) ([]graphModels.TagsWithFrequency, error) {