Skip to content

Commit

Permalink
Merge pull request #50 from basenana/feature/get_attr
Browse files Browse the repository at this point in the history
feat: get api & return attr with doc in filter api
  • Loading branch information
zwwhdls authored Dec 7, 2024
2 parents f0cc594 + c6aeb13 commit 56d31e3
Show file tree
Hide file tree
Showing 7 changed files with 282 additions and 34 deletions.
78 changes: 76 additions & 2 deletions api/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,43 @@ func (s *HttpServer) update() gin.HandlerFunc {
}
}

func (s *HttpServer) search() gin.HandlerFunc {
func (s *HttpServer) get() gin.HandlerFunc {
return func(c *gin.Context) {
namespace := c.Param("namespace")
entryId := c.Param("entryId")
document, err := s.chain.GetDocument(c, namespace, entryId)
if err != nil {
c.String(500, fmt.Sprintf("get document error: %s", err))
return
}
docWithAttr := &DocumentWithAttr{
Document: document,
}

attrs, err := s.chain.GetDocumentAttrs(c, namespace, entryId)
if err != nil {
c.String(500, fmt.Sprintf("get document attrs error: %s", err))
return
}
for _, attr := range attrs {
if attr.Key == "parentId" {
docWithAttr.ParentID = attr.Value.(string)
}
if attr.Key == "mark" {
marked := attr.Value.(bool)
docWithAttr.Mark = &marked
}
if attr.Key == "unRead" {
unRead := attr.Value.(bool)
docWithAttr.UnRead = &unRead
}
}

c.JSON(200, attrs)
}
}

func (s *HttpServer) filter() gin.HandlerFunc {
return func(c *gin.Context) {
namespace := c.Param("namespace")
page, err := strconv.Atoi(c.DefaultQuery("page", "0"))
Expand Down Expand Up @@ -114,7 +150,45 @@ func (s *HttpServer) search() gin.HandlerFunc {
c.String(500, fmt.Sprintf("search document error: %s", err))
return
}
c.JSON(200, docs)
ids := []string{}
for _, doc := range docs {
ids = append(ids, doc.EntryId)
}

allAttrs, err := s.chain.ListDocumentAttrs(c, namespace, ids)
if err != nil {
c.String(500, fmt.Sprintf("list document attrs error: %s", err))
return
}
attrsMap := map[string][]doc.DocumentAttr{}
for _, attr := range allAttrs {
if attrsMap[attr.EntryId] == nil {
attrsMap[attr.EntryId] = []doc.DocumentAttr{}
}
attrsMap[attr.EntryId] = append(attrsMap[attr.EntryId], attr)
}

var docWithAttrs []DocumentWithAttr
for _, document := range docs {
docWithAttr := DocumentWithAttr{Document: document}
attrs := attrsMap[document.EntryId]
for _, attr := range attrs {
if attr.Key == "parentId" {
docWithAttr.ParentID = attr.Value.(string)
}
if attr.Key == "mark" {
marked := attr.Value.(bool)
docWithAttr.Mark = &marked
}
if attr.Key == "unRead" {
unRead := attr.Value.(bool)
docWithAttr.UnRead = &unRead
}
}
docWithAttrs = append(docWithAttrs, docWithAttr)
}

c.JSON(200, docWithAttrs)
}
}

Expand Down
80 changes: 63 additions & 17 deletions api/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,20 @@
package api

import (
"time"

"github.com/google/uuid"

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

type DocRequest struct {
EntryId string `json:"entryId,omitempty"`
Name string `json:"name"`
Namespace string `json:"namespace"`
Source string `json:"source,omitempty"`
WebUrl string `json:"webUrl,omitempty"`
Content string `json:"content"`
CreatedAt time.Time `json:"createdAt,omitempty"`
ChangedAt time.Time `json:"changedAt,omitempty"`
EntryId string `json:"entryId,omitempty"`
Name string `json:"name"`
Namespace string `json:"namespace"`
Source string `json:"source,omitempty"`
WebUrl string `json:"webUrl,omitempty"`
Content string `json:"content"`
CreatedAt int64 `json:"createdAt,omitempty"`
ChangedAt int64 `json:"changedAt,omitempty"`
}

func (r *DocRequest) ToDocument() *doc.Document {
Expand Down Expand Up @@ -91,13 +89,18 @@ func (r *DocAttrRequest) ToDocAttr() []*doc.DocumentAttr {
}

type DocQuery struct {
IDs []string `json:"ids"`
Namespace string `json:"namespace"`
Source string `json:"source,omitempty"`
WebUrl string `json:"webUrl,omitempty"`
ParentID string `json:"parentId,omitempty"`
UnRead *bool `json:"unRead,omitempty"`
Mark *bool `json:"mark,omitempty"`
IDs []string `json:"ids"`
Namespace string `json:"namespace"`
Source string `json:"source,omitempty"`
WebUrl string `json:"webUrl,omitempty"`
ParentID string `json:"parentId,omitempty"`
UnRead *bool `json:"unRead,omitempty"`
Mark *bool `json:"mark,omitempty"`
CreatedAtStart *int64 `json:"createdAtStart,omitempty"`
CreatedAtEnd *int64 `json:"createdAtEnd,omitempty"`
ChangedAtStart *int64 `json:"changedAtStart,omitempty"`
ChangedAtEnd *int64 `json:"changedAtEnd,omitempty"`
FuzzyName *string `json:"fuzzyName,omitempty"`

Search string `json:"search"`

Expand Down Expand Up @@ -137,6 +140,41 @@ func (q *DocQuery) ToQuery() *doc.DocumentQuery {
Value: q.WebUrl,
})
}
if q.CreatedAtStart != nil {
attrQueries = append(attrQueries, doc.AttrQuery{
Attr: "createdAt",
Option: ">=",
Value: *q.CreatedAtStart,
})
}
if q.ChangedAtStart != nil {
attrQueries = append(attrQueries, doc.AttrQuery{
Attr: "updatedAt",
Option: ">=",
Value: *q.ChangedAtStart,
})
}
if q.CreatedAtEnd != nil {
attrQueries = append(attrQueries, doc.AttrQuery{
Attr: "createdAt",
Option: "<=",
Value: *q.CreatedAtEnd,
})
}
if q.ChangedAtEnd != nil {
attrQueries = append(attrQueries, doc.AttrQuery{
Attr: "updatedAt",
Option: "<=",
Value: *q.ChangedAtEnd,
})
}
if q.FuzzyName != nil {
attrQueries = append(attrQueries, doc.AttrQuery{
Attr: "name",
Option: "CONTAINS",
Value: *q.FuzzyName,
})
}

query.AttrQueries = attrQueries
return query
Expand Down Expand Up @@ -209,3 +247,11 @@ func (q *DocQuery) GetAttrQueries() []*doc.DocumentAttrQuery {
}
return attrQueries
}

type DocumentWithAttr struct {
doc.Document

ParentID string `json:"parentId,omitempty"`
UnRead *bool `json:"unRead,omitempty"`
Mark *bool `json:"mark,omitempty"`
}
3 changes: 2 additions & 1 deletion api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,6 @@ func (s *HttpServer) handle(group *gin.RouterGroup) {
docGroup.POST("/entry/:entryId", s.store())
docGroup.DELETE("/entry/:entryId", s.delete())
docGroup.PUT("/entry/:entryId", s.update())
docGroup.GET("/search", s.search())
docGroup.GET("/entry/:entryId", s.get())
docGroup.GET("/filter", s.filter())
}
11 changes: 6 additions & 5 deletions pkg/models/doc/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ package doc
import (
"encoding/json"
"fmt"
"time"

"github.com/meilisearch/meilisearch-go"
)

var (
DocFilterableAttrs = []string{"namespace", "id", "entryId", "name", "source", "webUrl", "createdAt", "updatedAt"}
DocAttrFilterableAttrs = []string{"namespace", "entryId", "key", "id", "value"}
DocFilterableAttrs = []string{"namespace", "id", "entryId", "kind", "name", "source", "webUrl", "createdAt", "updatedAt"}
DocAttrFilterableAttrs = []string{"namespace", "entryId", "key", "id", "kind", "value"}
DocSortAttrs = []string{"createdAt", "updatedAt"}
)

Expand All @@ -38,6 +37,7 @@ type DocPtrInterface interface {

type Document struct {
Id string `json:"id"`
Kind string `json:"kind"`
Namespace string `json:"namespace"`
EntryId string `json:"entryId"`
Name string `json:"name"`
Expand All @@ -49,8 +49,8 @@ type Document struct {
HeaderImage string `json:"headerImage,omitempty"`
SubContent string `json:"subContent,omitempty"`

CreatedAt time.Time `json:"createdAt,omitempty"`
UpdatedAt time.Time `json:"updatedAt,omitempty"`
CreatedAt int64 `json:"createdAt,omitempty"`
UpdatedAt int64 `json:"updatedAt,omitempty"`
}

func (d *Document) ID() string {
Expand All @@ -67,6 +67,7 @@ func (d *Document) Type() string {

type DocumentAttr struct {
Id string `json:"id"`
Kind string `json:"kind"`
Namespace string `json:"namespace"`
EntryId string `json:"entryId"`
Key string `json:"key"`
Expand Down
92 changes: 92 additions & 0 deletions pkg/service/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,95 @@ func (c *Chain) StoreAttr(ctx context.Context, docAttr *doc.DocumentAttr) error
})
}

func (c *Chain) GetDocument(ctx context.Context, namespace, entryId string) (doc.Document, error) {
docs, err := c.MeiliClient.Search(ctx, &doc.DocumentQuery{
AttrQueries: []doc.AttrQuery{
{
Attr: "namespace",
Option: "=",
Value: namespace,
},
{
Attr: "entryId",
Option: "=",
Value: entryId,
},
{
Attr: "kind",
Option: "=",
Value: "document",
},
},
})
if err != nil {
return doc.Document{}, err
}
if len(docs) == 0 {
return doc.Document{}, nil
}
return docs[0], nil
}

func (c *Chain) ListDocumentAttrs(ctx context.Context, namespace string, entryIds []string) ([]doc.DocumentAttr, error) {
attrs, err := c.MeiliClient.FilterAttr(ctx, &doc.DocumentAttrQuery{
AttrQueries: []doc.AttrQuery{
{
Attr: "namespace",
Option: "=",
Value: namespace,
},
{
Attr: "entryId",
Option: "IN",
Value: entryIds,
},
{
Attr: "kind",
Option: "=",
Value: "attr",
},
},
})
if err != nil {
return nil, err
}
return attrs, nil
}

func (c *Chain) GetDocumentAttrs(ctx context.Context, namespace, entryId string) ([]doc.DocumentAttr, error) {
attrs, err := c.MeiliClient.FilterAttr(ctx, &doc.DocumentAttrQuery{
AttrQueries: []doc.AttrQuery{
{
Attr: "namespace",
Option: "=",
Value: namespace,
},
{
Attr: "entryId",
Option: "=",
Value: entryId,
},
{
Attr: "kind",
Option: "=",
Value: "attr",
},
},
})
if err != nil {
return nil, err
}
return attrs, nil
}

func (c *Chain) Search(ctx context.Context, query *doc.DocumentQuery, attrQueries []*doc.DocumentAttrQuery) ([]doc.Document, error) {
attrs := []doc.DocumentAttr{}
for _, attrQuery := range attrQueries {
attrQuery.AttrQueries = append(attrQuery.AttrQueries, doc.AttrQuery{
Attr: "kind",
Option: "=",
Value: "attr",
})
attr, err := c.MeiliClient.FilterAttr(ctx, attrQuery)
if err != nil {
return nil, err
Expand All @@ -101,6 +187,12 @@ func (c *Chain) Search(ctx context.Context, query *doc.DocumentQuery, attrQuerie
if len(ids) == 0 && len(attrQueries) != 0 {
return []doc.Document{}, nil
}

query.AttrQueries = append(query.AttrQueries, doc.AttrQuery{
Attr: "kind",
Option: "=",
Value: "document",
})
if len(ids) != 0 {
query.AttrQueries = append(query.AttrQueries, doc.AttrQuery{
Attr: "entryId",
Expand Down
Loading

0 comments on commit 56d31e3

Please sign in to comment.