2023-01-12 12:42:16 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-12-20 14:30:55 +00:00
|
|
|
"github.com/fthvgb1/wp-go/app/pkg/dao"
|
2023-05-04 12:36:17 +00:00
|
|
|
"github.com/fthvgb1/wp-go/app/pkg/logs"
|
|
|
|
"github.com/fthvgb1/wp-go/app/pkg/models"
|
2023-01-19 13:02:39 +00:00
|
|
|
"github.com/fthvgb1/wp-go/cache"
|
2023-11-07 07:18:34 +00:00
|
|
|
"github.com/fthvgb1/wp-go/cache/cachemanager"
|
2023-12-20 14:30:55 +00:00
|
|
|
"github.com/fthvgb1/wp-go/helper"
|
2023-03-07 09:13:12 +00:00
|
|
|
"github.com/fthvgb1/wp-go/helper/number"
|
2023-01-12 12:42:16 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func RecentComments(ctx context.Context, n int) (r []models.Comments) {
|
2023-03-07 09:13:12 +00:00
|
|
|
nn := number.Max(n, 10)
|
2023-11-25 09:49:20 +00:00
|
|
|
r, err := cachemanager.GetVarVal[[]models.Comments]("recentComments", ctx, time.Second, ctx, nn)
|
2023-01-12 12:42:16 +00:00
|
|
|
if len(r) > n {
|
|
|
|
r = r[0:n]
|
|
|
|
}
|
2023-04-07 14:59:07 +00:00
|
|
|
logs.IfError(err, "get recent comment fail")
|
2023-01-12 12:42:16 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-04 15:32:10 +00:00
|
|
|
func PostComments(ctx context.Context, Id uint64) ([]models.Comments, error) {
|
2023-12-20 14:30:55 +00:00
|
|
|
ids, err := cachemanager.Get[[]uint64]("PostCommentsIds", ctx, Id, time.Second)
|
2023-01-12 12:42:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-12-20 14:30:55 +00:00
|
|
|
return GetCommentDataByIds(ctx, ids)
|
2023-01-12 12:42:16 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 15:32:10 +00:00
|
|
|
func GetCommentById(ctx context.Context, id uint64) (models.Comments, error) {
|
|
|
|
return cachemanager.Get[models.Comments]("postCommentData", ctx, id, time.Second)
|
2023-01-12 12:42:16 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 15:32:10 +00:00
|
|
|
func GetCommentDataByIds(ctx context.Context, ids []uint64) ([]models.Comments, error) {
|
|
|
|
return cachemanager.GetMultiple[models.Comments]("postCommentData", ctx, ids, time.Second)
|
2023-01-12 12:42:16 +00:00
|
|
|
}
|
2023-01-19 13:02:39 +00:00
|
|
|
|
|
|
|
func NewCommentCache() *cache.MapCache[string, string] {
|
2023-11-25 09:49:20 +00:00
|
|
|
r, _ := cachemanager.GetMapCache[string, string]("NewComment")
|
|
|
|
return r
|
2023-01-19 13:02:39 +00:00
|
|
|
}
|
2023-12-20 14:30:55 +00:00
|
|
|
|
2024-01-04 15:32:10 +00:00
|
|
|
func PostTopComments(ctx context.Context, _ string, a ...any) (helper.PaginationData[uint64], error) {
|
|
|
|
postId := a[0].(uint64)
|
|
|
|
page := a[1].(int)
|
|
|
|
limit := a[2].(int)
|
|
|
|
total := a[3].(int)
|
|
|
|
v, total, err := dao.PostCommentsIds(ctx, postId, page, limit, total)
|
2023-12-20 14:30:55 +00:00
|
|
|
if err != nil {
|
2024-01-04 15:32:10 +00:00
|
|
|
return helper.PaginationData[uint64]{}, err
|
2023-12-20 14:30:55 +00:00
|
|
|
}
|
2024-01-04 15:32:10 +00:00
|
|
|
return helper.PaginationData[uint64]{
|
|
|
|
Data: v,
|
|
|
|
TotalRaw: total,
|
|
|
|
}, nil
|
2023-12-20 14:30:55 +00:00
|
|
|
}
|