Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add update token api #63

Merged
merged 1 commit into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions api/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,20 @@ func (s *HttpServer) update() gin.HandlerFunc {
body.Namespace = namespace
enId, _ := strconv.Atoi(entryId)
body.EntryId = int64(enId)
// update the document
if err := s.chain.UpdateDocument(c, body.ToModel()); err != nil {
c.String(500, fmt.Sprintf("update document error: %s", err))
return

if body.IsUpdate() {
// update the document
if err := s.chain.UpdateDocument(c, body.ToModel()); err != nil {
c.String(500, fmt.Sprintf("update document error: %s", err))
return
}
}
if body.IsUpdateToken() {
// update token
if err := s.chain.UpdateTokens(c, namespace, body.EntryId); err != nil {
c.String(500, fmt.Sprintf("update document token error: %s", err))
return
}
}
c.JSON(200, body)
}
Expand Down
19 changes: 14 additions & 5 deletions api/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ func (r *DocRequest) Valid() error {
}

type DocUpdateRequest struct {
Namespace string `json:"namespace"`
EntryId int64 `json:"entryId,omitempty"`
ParentID *int64 `json:"parentId,omitempty"`
UnRead *bool `json:"unRead,omitempty"`
Mark *bool `json:"mark,omitempty"`
Namespace string `json:"namespace"`
EntryId int64 `json:"entryId,omitempty"`
ParentID *int64 `json:"parentId,omitempty"`
UnRead *bool `json:"unRead,omitempty"`
Mark *bool `json:"mark,omitempty"`
UpdateToken *bool `json:"updateToken,omitempty"`
}

func (r *DocUpdateRequest) Valid() error {
Expand All @@ -55,6 +56,14 @@ func (r *DocUpdateRequest) Valid() error {
return nil
}

func (r *DocUpdateRequest) IsUpdate() bool {
return r.ParentID != nil || r.UnRead != nil || r.Mark != nil
}

func (r *DocUpdateRequest) IsUpdateToken() bool {
return r.UpdateToken != nil && *r.UpdateToken
}

func (r *DocUpdateRequest) ToModel() *doc.Document {
return &doc.Document{
EntryId: r.EntryId,
Expand Down
27 changes: 27 additions & 0 deletions pkg/service/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,33 @@ func (c *Chain) CreateDocument(ctx context.Context, document *doc.Document) erro
})
}

func (c *Chain) UpdateTokens(ctx context.Context, namespace string, entryId int64) error {
return ChainPool.Run(ctx, func(ctx context.Context) error {
ctx = c.WithNamespace(ctx, namespace)
c.Log.Debugf("update document tokens entryId: %d", entryId)
var (
document *doc.Document
err error
)
if document, err = c.GetDocument(ctx, namespace, entryId); err != nil {
c.Log.Errorf("get document error: %s", err)
return err
} else if document == nil {
c.Log.Debugf("document of entryId not found: %d", entryId)
return fmt.Errorf("document of entryId %d not found", entryId)
}
for _, plugin := range c.Plugins {
err := plugin.Run(ctx, document)
if err != nil {
c.Log.Errorf("plugin error: %s", err)
return err
}
}
c.Log.Debugf("update tokens: %+v", document.Name)
return c.DocClient.UpdateTokens(ctx, document)
})
}

func (c *Chain) UpdateDocument(ctx context.Context, document *doc.Document) error {
return ChainPool.Run(ctx, func(ctx context.Context) error {
ctx = c.WithNamespace(ctx, document.Namespace)
Expand Down
Loading