feat: added references in posts

Signed-off-by: SoXX <soxx@fenpa.ws>
This commit is contained in:
SoXX 2024-07-15 16:06:19 +02:00
parent c3ca361506
commit 7775a28161

View File

@ -230,8 +230,8 @@ func GetAllUsers(ctx context.Context, db *gorm.DB) ([]models.User, error) {
return users, nil 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) { 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 var favoritePosts []models.Post
if anthroveUserID == "" { if anthroveUserID == "" {
@ -242,30 +242,7 @@ func GetUserFavoriteWithPagination(ctx context.Context, db *gorm.DB, anthroveUse
return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort} 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 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)
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,
})
}
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"anthrove_user_id": anthroveUserID, "anthrove_user_id": anthroveUserID,