Fixed Pagination & modles #14
@ -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,
|
||||
|
Reference in New Issue
Block a user