2023-01-12 12:42:16 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-01-18 15:02:59 +00:00
|
|
|
"github.com/fthvgb1/wp-go/helper"
|
2023-01-21 11:31:23 +00:00
|
|
|
"github.com/fthvgb1/wp-go/helper/slice"
|
2023-01-18 15:02:59 +00:00
|
|
|
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
|
|
|
"github.com/fthvgb1/wp-go/model"
|
2023-01-12 12:42:16 +00:00
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RecentComments
|
|
|
|
// param context.Context
|
|
|
|
func RecentComments(a ...any) (r []models.Comments, err error) {
|
|
|
|
ctx := a[0].(context.Context)
|
|
|
|
return model.Find[models.Comments](ctx, model.SqlBuilder{
|
|
|
|
{"comment_approved", "1"},
|
|
|
|
{"post_status", "publish"},
|
|
|
|
}, "comment_ID,comment_author,comment_post_ID,post_title", "", model.SqlBuilder{{"comment_date_gmt", "desc"}}, model.SqlBuilder{
|
|
|
|
{"a", "left join", "wp_posts b", "a.comment_post_ID=b.ID"},
|
|
|
|
}, nil, 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PostComments
|
|
|
|
// param1 context.Context
|
|
|
|
// param2 postId
|
|
|
|
func PostComments(args ...any) ([]uint64, error) {
|
|
|
|
ctx := args[0].(context.Context)
|
|
|
|
postId := args[1].(uint64)
|
|
|
|
r, err := model.Find[models.Comments](ctx, model.SqlBuilder{
|
|
|
|
{"comment_approved", "1"},
|
|
|
|
{"comment_post_ID", "=", strconv.FormatUint(postId, 10), "int"},
|
|
|
|
}, "comment_ID", "", model.SqlBuilder{
|
|
|
|
{"comment_date_gmt", "asc"},
|
|
|
|
{"comment_ID", "asc"},
|
|
|
|
}, nil, nil, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-01-21 11:31:23 +00:00
|
|
|
return slice.Map(r, func(t models.Comments) uint64 {
|
2023-01-12 12:42:16 +00:00
|
|
|
return t.CommentId
|
|
|
|
}), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetCommentByIds(args ...any) (map[uint64]models.Comments, error) {
|
|
|
|
ctx := args[0].(context.Context)
|
|
|
|
ids := args[1].([]uint64)
|
|
|
|
m := make(map[uint64]models.Comments)
|
|
|
|
r, err := model.SimpleFind[models.Comments](ctx, model.SqlBuilder{
|
|
|
|
{"comment_ID", "in", ""}, {"comment_approved", "1"},
|
2023-01-21 11:31:23 +00:00
|
|
|
}, "*", slice.Map(ids, helper.ToAny[uint64]))
|
2023-01-12 12:42:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return m, err
|
|
|
|
}
|
2023-01-21 11:31:23 +00:00
|
|
|
return slice.SimpleToMap(r, func(t models.Comments) uint64 {
|
2023-01-12 12:42:16 +00:00
|
|
|
return t.CommentId
|
|
|
|
}), err
|
|
|
|
}
|