-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from basenana/document
feat: add document management in meiliserch & api
- Loading branch information
Showing
54 changed files
with
2,367 additions
and
345 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
/* | ||
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 api | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/gin-gonic/gin" | ||
|
||
"github.com/basenana/friday/pkg/models/doc" | ||
) | ||
|
||
func (s *HttpServer) store() gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
entryId := c.Param("entryId") | ||
namespace := c.Param("namespace") | ||
body := &DocRequest{} | ||
if err := c.ShouldBindJSON(&body); err != nil { | ||
c.JSON(400, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
body.Namespace = namespace | ||
body.EntryId = entryId | ||
// store the document | ||
doc := body.ToDocument() | ||
if err := s.chain.Store(c, doc); err != nil { | ||
c.String(500, fmt.Sprintf("store document error: %s", err)) | ||
return | ||
} | ||
c.JSON(200, doc) | ||
} | ||
} | ||
|
||
func (s *HttpServer) update() gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
entryId := c.Param("entryId") | ||
namespace := c.Param("namespace") | ||
body := &DocAttrRequest{} | ||
if err := c.ShouldBindJSON(&body); err != nil { | ||
c.JSON(400, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
body.Namespace = namespace | ||
body.EntryId = entryId | ||
// update the document | ||
attrs := body.ToDocAttr() | ||
for _, attr := range attrs { | ||
if err := s.chain.StoreAttr(c, attr); err != nil { | ||
c.String(500, fmt.Sprintf("update document error: %s", err)) | ||
return | ||
} | ||
} | ||
c.JSON(200, body) | ||
} | ||
} | ||
|
||
func (s *HttpServer) search() gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
namespace := c.Param("namespace") | ||
page, err := strconv.Atoi(c.DefaultQuery("page", "0")) | ||
if err != nil { | ||
c.String(400, fmt.Sprintf("invalid page number: %s", c.Query("page"))) | ||
return | ||
} | ||
pageSize, err := strconv.Atoi(c.DefaultQuery("pageSize", "10")) | ||
if err != nil { | ||
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, | ||
} | ||
docs, err := s.chain.Search(c, docQuery.ToQuery(), docQuery.GetAttrQueries()) | ||
if err != nil { | ||
c.String(500, fmt.Sprintf("search document error: %s", err)) | ||
return | ||
} | ||
c.JSON(200, docs) | ||
} | ||
} | ||
|
||
func (s *HttpServer) delete() gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
namespace := c.Param("namespace") | ||
queries := []doc.AttrQuery{} | ||
entryId := c.Param("entryId") | ||
queries = append(queries, | ||
doc.AttrQuery{ | ||
Attr: "entryId", | ||
Option: "=", | ||
Value: entryId, | ||
}, | ||
doc.AttrQuery{ | ||
Attr: "namespace", | ||
Option: "=", | ||
Value: namespace, | ||
}, | ||
) | ||
if err := s.chain.DeleteByFilter(c, queries); err != nil { | ||
c.String(500, fmt.Sprintf("delete document error: %s", err)) | ||
return | ||
} | ||
c.JSON(200, gin.H{"entryId": entryId}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,225 @@ | ||
/* | ||
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 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"` | ||
} | ||
|
||
func (r *DocRequest) ToDocument() *doc.Document { | ||
return &doc.Document{ | ||
Id: uuid.New().String(), | ||
EntryId: r.EntryId, | ||
Name: r.Name, | ||
Namespace: r.Namespace, | ||
Source: r.Source, | ||
WebUrl: r.WebUrl, | ||
Content: r.Content, | ||
CreatedAt: r.CreatedAt, | ||
UpdatedAt: r.ChangedAt, | ||
} | ||
} | ||
|
||
type DocAttrRequest struct { | ||
Namespace string `json:"namespace"` | ||
EntryId string `json:"entryId,omitempty"` | ||
ParentID string `json:"parentId,omitempty"` | ||
UnRead *bool `json:"unRead,omitempty"` | ||
Mark *bool `json:"mark,omitempty"` | ||
} | ||
|
||
func (r *DocAttrRequest) ToDocAttr() []*doc.DocumentAttr { | ||
attrs := []*doc.DocumentAttr{} | ||
if r.ParentID != "" { | ||
attrs = append(attrs, &doc.DocumentAttr{ | ||
Id: uuid.New().String(), | ||
Namespace: r.Namespace, | ||
EntryId: r.EntryId, | ||
Key: "parentId", | ||
Value: r.ParentID, | ||
}) | ||
} | ||
if r.Mark != nil { | ||
attrs = append(attrs, &doc.DocumentAttr{ | ||
Id: uuid.New().String(), | ||
Namespace: r.Namespace, | ||
EntryId: r.EntryId, | ||
Key: "mark", | ||
Value: *r.Mark, | ||
}) | ||
} | ||
if r.UnRead != nil { | ||
attrs = append(attrs, &doc.DocumentAttr{ | ||
Id: uuid.New().String(), | ||
Namespace: r.Namespace, | ||
EntryId: r.EntryId, | ||
Key: "unRead", | ||
Value: *r.UnRead, | ||
}) | ||
|
||
} | ||
return attrs | ||
} | ||
|
||
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"` | ||
|
||
Search string `json:"search"` | ||
|
||
HitsPerPage int64 `json:"hitsPerPage,omitempty"` | ||
Page int64 `json:"page,omitempty"` | ||
Limit int64 `json:"limit,omitempty"` | ||
Sort string `json:"sort,omitempty"` | ||
Desc bool `json:"desc,omitempty"` | ||
} | ||
|
||
func (q *DocQuery) ToQuery() *doc.DocumentQuery { | ||
query := &doc.DocumentQuery{ | ||
Search: q.Search, | ||
HitsPerPage: q.HitsPerPage, | ||
Page: q.Page, | ||
Sort: []doc.Sort{{ | ||
Attr: q.Sort, | ||
Asc: !q.Desc, | ||
}}, | ||
} | ||
attrQueries := []doc.AttrQuery{{ | ||
Attr: "namespace", | ||
Option: "=", | ||
Value: q.Namespace, | ||
}} | ||
if q.Source != "" { | ||
attrQueries = append(attrQueries, doc.AttrQuery{ | ||
Attr: "source", | ||
Option: "=", | ||
Value: q.Source, | ||
}) | ||
} | ||
if q.WebUrl != "" { | ||
attrQueries = append(attrQueries, doc.AttrQuery{ | ||
Attr: "webUrl", | ||
Option: "=", | ||
Value: q.WebUrl, | ||
}) | ||
} | ||
if q.ParentID != "" { | ||
attrQueries = append(attrQueries, doc.AttrQuery{ | ||
Attr: "parentId", | ||
Option: "=", | ||
Value: q.ParentID, | ||
}) | ||
} | ||
if q.UnRead != nil { | ||
attrQueries = append(attrQueries, doc.AttrQuery{ | ||
Attr: "unRead", | ||
Option: "=", | ||
Value: true, | ||
}) | ||
} | ||
|
||
query.AttrQueries = attrQueries | ||
return query | ||
} | ||
|
||
func (q *DocQuery) GetAttrQueries() []*doc.DocumentAttrQuery { | ||
attrQueries := []*doc.DocumentAttrQuery{} | ||
if q.UnRead != nil { | ||
attrQueries = append(attrQueries, &doc.DocumentAttrQuery{ | ||
AttrQueries: []doc.AttrQuery{ | ||
{ | ||
Attr: "namespace", | ||
Option: "=", | ||
Value: q.Namespace, | ||
}, | ||
{ | ||
Attr: "key", | ||
Option: "=", | ||
Value: "unRead", | ||
}, | ||
{ | ||
Attr: "value", | ||
Option: "=", | ||
Value: *q.UnRead, | ||
}, | ||
}, | ||
}) | ||
} | ||
if q.Mark != nil { | ||
attrQueries = append(attrQueries, &doc.DocumentAttrQuery{ | ||
AttrQueries: []doc.AttrQuery{ | ||
{ | ||
Attr: "namespace", | ||
Option: "=", | ||
Value: q.Namespace, | ||
}, | ||
{ | ||
Attr: "key", | ||
Option: "=", | ||
Value: "mark", | ||
}, | ||
{ | ||
Attr: "value", | ||
Option: "=", | ||
Value: *q.Mark, | ||
}, | ||
}, | ||
}) | ||
} | ||
if q.ParentID != "" { | ||
attrQueries = append(attrQueries, &doc.DocumentAttrQuery{ | ||
AttrQueries: []doc.AttrQuery{ | ||
{ | ||
Attr: "namespace", | ||
Option: "=", | ||
Value: q.Namespace, | ||
}, | ||
{ | ||
Attr: "key", | ||
Option: "=", | ||
Value: "parentId", | ||
}, | ||
{ | ||
Attr: "value", | ||
Option: "=", | ||
Value: q.ParentID, | ||
}, | ||
}, | ||
}) | ||
} | ||
return attrQueries | ||
} |
Oops, something went wrong.