Skip to content

Commit

Permalink
fix: filter doc api
Browse files Browse the repository at this point in the history
Signed-off-by: zwwhdls <zww@hdls.me>
  • Loading branch information
zwwhdls committed Dec 8, 2024
1 parent 56d31e3 commit 1a87773
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 19 deletions.
67 changes: 49 additions & 18 deletions api/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/gin-gonic/gin"

"github.com/basenana/friday/pkg/models/doc"
"github.com/basenana/friday/pkg/utils"
)

func (s *HttpServer) store() gin.HandlerFunc {
Expand Down Expand Up @@ -118,32 +119,62 @@ func (s *HttpServer) filter() gin.HandlerFunc {
c.String(400, fmt.Sprintf("invalid pagesize: %s", c.Query("page")))
return
}
sort := c.DefaultQuery("sort", "createdAt")
desc := c.DefaultQuery("desc", "true") == "true"
var (
unread *bool
mark *bool
)
if c.Query("unread") != "" {
b := c.Query("unread") == "true"
unread = &b
}
if c.Query("mark") != "" {
b := c.Query("mark") == "true"
mark = &b
}
docQuery := DocQuery{
Namespace: namespace,
Source: c.Query("source"),
WebUrl: c.Query("webUrl"),
ParentID: c.Query("parentID"),
UnRead: unread,
Mark: mark,
Search: c.Query("search"),
HitsPerPage: int64(pageSize),
Page: int64(page),
Sort: sort,
Desc: desc,
Sort: c.DefaultQuery("sort", "createdAt"),
Desc: c.DefaultQuery("desc", "false") == "true",
}
createAtStart := c.Query("createAtStart")
if createAtStart != "" {
createAtStartTimestamp, err := strconv.Atoi(createAtStart)
if err != nil {
c.String(400, fmt.Sprintf("invalid createAtStart: %s", c.Query("page")))
return
}
docQuery.CreatedAtStart = utils.ToPtr(int64(createAtStartTimestamp))
}
createAtEnd := c.Query("createAtEnd")
if createAtEnd != "" {
createAtEndTimestamp, err := strconv.Atoi(createAtEnd)
if err != nil {
c.String(400, fmt.Sprintf("invalid createAtEnd: %s", c.Query("page")))
return
}
docQuery.ChangedAtEnd = utils.ToPtr(int64(createAtEndTimestamp))
}
updatedAtStart := c.Query("updatedAtStart")
if updatedAtStart != "" {
updatedAtStartTimestamp, err := strconv.Atoi(updatedAtStart)
if err != nil {
c.String(400, fmt.Sprintf("invalid updatedAtStart: %s", c.Query("page")))
return
}
docQuery.ChangedAtStart = utils.ToPtr(int64(updatedAtStartTimestamp))
}
updatedAtEnd := c.Query("updatedAtEnd")
if updatedAtEnd != "" {
updatedAtEndTimestamp, err := strconv.Atoi(updatedAtEnd)
if err != nil {
c.String(400, fmt.Sprintf("invalid updatedAtEnd: %s", c.Query("page")))
return
}
docQuery.ChangedAtEnd = utils.ToPtr(int64(updatedAtEndTimestamp))
}
fuzzyName := c.Query("fuzzyName")
if fuzzyName != "" {
docQuery.FuzzyName = &fuzzyName
}
if c.Query("unread") != "" {
docQuery.UnRead = utils.ToPtr(c.Query("unread") == "true")
}
if c.Query("mark") != "" {
docQuery.Mark = utils.ToPtr(c.Query("mark") == "true")
}
docs, err := s.chain.Search(c, docQuery.ToQuery(), docQuery.GetAttrQueries())
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/models/doc/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
var (
DocFilterableAttrs = []string{"namespace", "id", "entryId", "kind", "name", "source", "webUrl", "createdAt", "updatedAt"}
DocAttrFilterableAttrs = []string{"namespace", "entryId", "key", "id", "kind", "value"}
DocSortAttrs = []string{"createdAt", "updatedAt"}
DocSortAttrs = []string{"createdAt", "updatedAt", "name"}
)

type DocPtrInterface interface {
Expand Down
21 changes: 21 additions & 0 deletions pkg/utils/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
Copyright 2024 Friday Author.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package utils

func ToPtr[T any](t T) *T {
return &t
}

0 comments on commit 1a87773

Please sign in to comment.