wp-go/app/pkg/dao/comments.go

63 lines
1.7 KiB
Go
Raw Normal View History

package dao
2023-01-12 12:42:16 +00:00
import (
"context"
2023-05-04 12:36:17 +00:00
"github.com/fthvgb1/wp-go/app/pkg/models"
2023-02-06 10:34:35 +00:00
"github.com/fthvgb1/wp-go/helper/number"
2023-01-21 11:31:23 +00:00
"github.com/fthvgb1/wp-go/helper/slice"
"github.com/fthvgb1/wp-go/model"
2023-01-12 12:42:16 +00:00
)
// RecentComments
// param context.Context
func RecentComments(a ...any) (r []models.Comments, err error) {
ctx := a[0].(context.Context)
2023-02-09 07:43:20 +00:00
n := a[1].(int)
2023-02-06 10:34:35 +00:00
return model.Finds[models.Comments](ctx, model.Conditions(
model.Where(model.SqlBuilder{
{"comment_approved", "1"},
{"post_status", "publish"},
}),
model.Fields("comment_ID,comment_author,comment_post_ID,post_title"),
model.Order(model.SqlBuilder{{"comment_date_gmt", "desc"}}),
model.Join(model.SqlBuilder{{"a", "left join", "wp_posts b", "a.comment_post_ID=b.ID"}}),
2023-02-09 07:43:20 +00:00
model.Limit(n),
2023-02-06 10:34:35 +00:00
))
2023-01-12 12:42:16 +00:00
}
// PostComments
// param1 context.Context
// param2 postId
func PostComments(ctx context.Context, postId uint64, _ ...any) ([]uint64, error) {
2023-02-06 10:34:35 +00:00
r, err := model.Finds[models.Comments](ctx, model.Conditions(
model.Where(model.SqlBuilder{
{"comment_approved", "1"},
2023-05-21 11:53:37 +00:00
{"comment_post_ID", "=", number.IntToString(postId), "int"},
2023-02-06 10:34:35 +00:00
}),
model.Fields("comment_ID"),
model.Order(model.SqlBuilder{
{"comment_date_gmt", "asc"},
{"comment_ID", "asc"},
})),
)
2023-01-12 12:42:16 +00:00
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(ctx context.Context, ids []uint64, _ ...any) (map[uint64]models.Comments, error) {
2023-01-12 12:42:16 +00:00
m := make(map[uint64]models.Comments)
r, err := model.SimpleFind[models.Comments](ctx, model.SqlBuilder{
{"comment_ID", "in", ""}, {"comment_approved", "1"},
2023-01-23 15:42:37 +00:00
}, "*", slice.ToAnySlice(ids))
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
}