From 7775a28161593cc8cde6f77d81a3e7f1bdc11eb7 Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 15 Jul 2024 16:06:19 +0200 Subject: [PATCH] feat: added references in posts Signed-off-by: SoXX --- internal/postgres/user.go | 27 ++------------------------- 1 file changed, 2 insertions(+), 25 deletions(-) diff --git a/internal/postgres/user.go b/internal/postgres/user.go index 3cf9fb8..8445086 100644 --- a/internal/postgres/user.go +++ b/internal/postgres/user.go @@ -230,8 +230,8 @@ func GetAllUsers(ctx context.Context, db *gorm.DB) ([]models.User, error) { return users, nil } +// TODO: FIX THE TEST func GetUserFavoriteWithPagination(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, skip int, limit int) (*models.FavoriteList, error) { - var userFavorites []models.UserFavorites var favoritePosts []models.Post if anthroveUserID == "" { @@ -242,30 +242,7 @@ func GetUserFavoriteWithPagination(ctx context.Context, db *gorm.DB, anthroveUse return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort} } - err := db.WithContext(ctx).Model(&models.UserFavorites{}).Where("user_id = ?", string(anthroveUserID)).Offset(skip).Limit(limit).Find(&userFavorites).Error - if err != nil { - if errors.Is(err, gorm.ErrRecordNotFound) { - return nil, &otterError.NoDataFound{} - } - return nil, err - } - - for _, userFavorite := range userFavorites { - var post models.Post - err = db.WithContext(ctx).Model(&models.Post{}).Joins("References").Where("id = ?", userFavorite.PostID).First(&post).Error - if err != nil { - if errors.Is(err, gorm.ErrRecordNotFound) { - return nil, &otterError.NoDataFound{} - } - return nil, err - } - - favoritePosts = append(favoritePosts, - models.Post{ - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: post.ID}, - Rating: post.Rating, - }) - } + db.WithContext(ctx).Joins("RIGHT JOIN \"UserFavorites\" AS of ON \"Post\".id = of.post_id AND of.user_id = ?", anthroveUserID).Preload("References").Offset(skip).Limit(limit).Find(&favoritePosts) log.WithFields(log.Fields{ "anthrove_user_id": anthroveUserID,