Skip to content

Commit

Permalink
Improve /v1/publication/estimate api.
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Sep 1, 2023
1 parent b97a75a commit 0d3db9f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
17 changes: 6 additions & 11 deletions src/api/publication.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,12 @@ func (a *Publication) Estimate(ctx *gear.Context) error {
return gear.ErrInternalServerError.From(err)
}

teContents, err := src.ToTEContents()
if err != nil {
return gear.ErrInternalServerError.From(err)
}

teTokens, _ := json.Marshal(teContents)
teTokens, err := src.ToEstimateToken()
if err != nil {
return gear.ErrInternalServerError.From(err)
}

tokens := a.blls.Tiktokens(string(teTokens)) * 2
tokens := a.blls.Tiktokens(teTokens) * 2
output := &EstimateOutput{
Balance: wallet.Balance(),
Tokens: tokens,
Expand Down Expand Up @@ -130,18 +125,18 @@ func (a *Publication) Create(ctx *gear.Context) error {
}

if wallet.Balance() < 1 {
return gear.ErrPaymentRequired.WithMsgf("insufficient balance")
return gear.ErrPaymentRequired.WithMsg("insufficient balance")
}

_, err = a.blls.Writing.GetPublication(ctx, &bll.QueryPublication{
dst, _ := a.blls.Writing.GetPublication(ctx, &bll.QueryPublication{
GID: input.GID,
CID: input.CID,
Language: *input.ToLanguage,
Version: input.Version,
Fields: "status,creator,updated_at",
})
if err == nil {
return gear.ErrConflict.WithMsg("%s publication already exists", *input.ToLanguage)
if dst != nil {
return gear.ErrConflict.WithMsgf("%s publication already exists", *input.ToLanguage)
}

src, err := a.blls.Writing.GetPublication(ctx, &bll.QueryPublication{
Expand Down
21 changes: 21 additions & 0 deletions src/bll/writing_publication.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bll

import (
"context"
"encoding/json"
"net/url"
"strconv"

Expand Down Expand Up @@ -155,6 +156,26 @@ func (i *PublicationOutput) ToTEContents() (content.TEContents, error) {
return contents, nil
}

func (i *PublicationOutput) ToEstimateToken() (string, error) {
if i.Content == nil {
return "", gear.ErrInternalServerError.WithMsg("empty content")
}
doc, err := content.ParseDocumentNode(*i.Content)
if err != nil {
return "", gear.ErrInternalServerError.From(err)
}
contents := doc.ToTEContents()
for i := range contents {
contents[i].ID = ""
}

teTokens, err := json.Marshal(contents)
if err != nil {
return "", gear.ErrInternalServerError.From(err)
}
return string(teTokens), nil
}

func (i *PublicationOutput) IntoPublicationDraft(gid util.ID, language, model string, input []byte) (*PublicationDraft, error) {
draft := &PublicationDraft{
GID: gid,
Expand Down

0 comments on commit 0d3db9f

Please sign in to comment.