Files
artalkjs/server/handler/comments_get/scope_page.go
T
qwqcode c2c5a80d8d refactor(comments_get): reduce api complexity by introducing LiteDB (#800)
* refactor(comments_get): reduce interface complexity by introducing LiteDB

* test: add unit test

* add more unit test case
2024-04-07 19:17:01 +08:00

61 lines
1.2 KiB
Go

package comments_get
import (
"slices"
)
type PageScopeOpts struct {
AdminUserIDs []uint
}
type PageScopePayload struct {
Tags []PageScopeTag
SiteName string
PageKey string
}
type PageScopeTag string
const (
AdminOnly PageScopeTag = "admin_only"
)
// Page Scope
func PageScopeQuery(payload PageScopePayload, opts PageScopeOpts) func(liteDB) liteDB {
return func(d liteDB) liteDB {
if payload.SiteName == "" || payload.PageKey == "" {
return d.Where("1 = 0")
}
// Query within site & page
d.Scopes(
CommentsWithinSite(payload.SiteName),
CommentsWithinPage(payload.PageKey))
// Show admin comments only function
if slices.Contains(payload.Tags, AdminOnly) {
d.Scopes(CommentsWithinSomeUsers(opts.AdminUserIDs))
}
return d
}
}
func CommentsWithinSite(siteName string) func(liteDB) liteDB {
return func(d liteDB) liteDB {
return d.Where("site_name = ?", siteName)
}
}
func CommentsWithinPage(pageKey string) func(liteDB) liteDB {
return func(d liteDB) liteDB {
return d.Where("page_key = ?", pageKey)
}
}
func CommentsWithinSomeUsers(allAdminIDs []uint) func(liteDB) liteDB {
return func(d liteDB) liteDB {
return d.Where("user_id IN ?", allAdminIDs)
}
}