Skip to content

Commit

Permalink
feat(backend): implement ListDrafts
Browse files Browse the repository at this point in the history
Former-commit-id: 0a3e721
  • Loading branch information
burdiyan committed May 9, 2022
1 parent f1fc59b commit 7272793
Show file tree
Hide file tree
Showing 8 changed files with 267 additions and 90 deletions.
35 changes: 2 additions & 33 deletions backend/api_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func newDocsAPI(back *backend) DocsServer {

vcs := vcs.New(back.pool)

docsapi := vcstypes.NewDocsAPI(id, vcs)
docsapi := vcstypes.NewDocsAPI(id, back.pool, vcs)
srv.DocsAPI = docsapi

}()
Expand Down Expand Up @@ -145,39 +145,8 @@ func draftToProto(d Draft) *documents.Document {
}
}

func (srv *docsAPI) ListDrafts(ctx context.Context, in *documents.ListDraftsRequest) (*documents.ListDraftsResponse, error) {
list, err := srv.back.ListDrafts(ctx)
if err != nil {
return nil, err
}

out := &documents.ListDraftsResponse{
Documents: make([]*documents.Document, len(list)),
}

acc, err := srv.back.Account()
if err != nil {
return nil, err
}

aid := acc.id.String()

for i, l := range list {
out.Documents[i] = &documents.Document{
Id: cid.NewCidV1(uint64(l.ObjectsCodec), l.ObjectsMultihash).String(),
Author: aid,
Title: l.DraftsTitle,
Subtitle: l.DraftsSubtitle,
CreateTime: timestamppb.New(timeFromSeconds(l.DraftsCreateTime)),
UpdateTime: timestamppb.New(timeFromSeconds(l.DraftsUpdateTime)),
}
}

return out, nil
}

func (srv *docsAPI) UpdateDraft(ctx context.Context, in *documents.UpdateDraftRequest) (*documents.Document, error) {
return nil, status.Error(codes.Unimplemented, "not implemented")
return nil, status.Error(codes.Unimplemented, "deprecated")

c, err := srv.parseDocumentID(in.Document.Id)
if err != nil {
Expand Down
91 changes: 91 additions & 0 deletions backend/vcs/vcssql/sqlite_queries.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions backend/vcs/vcssql/sqlite_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,56 @@ func generateQueries() error {
"AND", s.NamedVersionsName, "=", qb.VarCol(s.NamedVersionsName), qb.Line,
"LIMIT 1",
),

qb.MakeQuery(s.Schema, "DraftsInsert", sgen.QueryKindExec,
"INSERT INTO", s.Drafts, qb.ListColShort(
s.DraftsID,
s.DraftsTitle,
s.DraftsSubtitle,
s.DraftsCreateTime,
s.DraftsUpdateTime,
), qb.Line,
"VALUES", qb.List(
qb.LookupSubQuery(s.ObjectsID, s.Objects,
"WHERE", s.ObjectsMultihash, "=", qb.VarCol(s.ObjectsMultihash),
"AND", s.ObjectsCodec, "=", qb.VarCol(s.ObjectsCodec),
),
qb.VarCol(s.DraftsTitle),
qb.VarCol(s.DraftsSubtitle),
qb.VarCol(s.DraftsCreateTime),
qb.VarCol(s.DraftsUpdateTime),
),
),

qb.MakeQuery(s.Schema, "DraftsUpdate", sgen.QueryKindExec,
"UPDATE", s.Drafts, qb.Line,
"SET", qb.ListColShort(
s.DraftsTitle,
s.DraftsSubtitle,
s.DraftsUpdateTime,
), "=", qb.List(
qb.VarCol(s.DraftsTitle),
qb.VarCol(s.DraftsSubtitle),
qb.VarCol(s.DraftsUpdateTime),
), qb.Line,
"WHERE", s.DraftsID, "=", qb.LookupSubQuery(s.ObjectsID, s.Objects,
"WHERE", s.ObjectsMultihash, "=", qb.VarCol(s.ObjectsMultihash),
"AND", s.ObjectsCodec, "=", qb.VarCol(s.ObjectsCodec),
),
),

qb.MakeQuery(s.Schema, "DraftsList", sgen.QueryKindMany,
"SELECT", qb.Results(
qb.ResultCol(s.ObjectsMultihash),
qb.ResultCol(s.ObjectsCodec),
qb.ResultCol(s.DraftsTitle),
qb.ResultCol(s.DraftsSubtitle),
qb.ResultCol(s.DraftsCreateTime),
qb.ResultCol(s.DraftsUpdateTime),
), qb.Line,
"FROM", s.Drafts, qb.Line,
"JOIN", s.Objects, "ON", s.ObjectsID, "=", s.DraftsID, qb.Line,
),
)

if err != nil {
Expand Down
69 changes: 67 additions & 2 deletions backend/vcs/vcstypes/api.go → backend/vcs/vcstypes/api_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import (
documents "mintter/backend/api/documents/v1alpha"
"mintter/backend/core"
"mintter/backend/crdt"
"mintter/backend/ipfs"
"mintter/backend/vcs"
"mintter/backend/vcs/vcssql"

"crawshaw.io/sqlite/sqlitex"
"github.com/ipfs/go-cid"
cbornode "github.com/ipfs/go-ipld-cbor"
"google.golang.org/grpc/codes"
Expand All @@ -17,13 +20,15 @@ import (
)

type DocsAPI struct {
vcs *vcs.SQLite
me core.Identity
db *sqlitex.Pool
vcs *vcs.SQLite
}

func NewDocsAPI(me core.Identity, vcs *vcs.SQLite) *DocsAPI {
func NewDocsAPI(me core.Identity, db *sqlitex.Pool, vcs *vcs.SQLite) *DocsAPI {
return &DocsAPI{
me: me,
db: db,
vcs: vcs,
}
}
Expand Down Expand Up @@ -54,6 +59,20 @@ func (api *DocsAPI) CreateDraft(ctx context.Context, in *documents.CreateDraftRe
return nil, err
}

{
conn, release, err := api.db.Conn(ctx)
if err != nil {
return nil, err
}
defer release()

ocodec, ohash := ipfs.DecodeCID(permablk.Cid())

if err := vcssql.DraftsInsert(conn, ohash, int(ocodec), "", "", int(p.CreateTime.Unix()), int(p.CreateTime.Unix())); err != nil {
return nil, err
}
}

return &documents.Document{
Id: permablk.Cid().String(),
Author: me.String(),
Expand Down Expand Up @@ -125,6 +144,20 @@ func (api *DocsAPI) UpdateDraftV2(ctx context.Context, in *documents.UpdateDraft
return nil, fmt.Errorf("failed to save draft working copy: %w", err)
}

{
conn, release, err := api.db.Conn(ctx)
if err != nil {
return nil, err
}
defer release()

ocodec, ohash := ipfs.DecodeCID(oid)

if err := vcssql.DraftsUpdate(conn, doc.state.Title, doc.state.Subtitle, int(doc.state.UpdateTime.Unix()), ohash, int(ocodec)); err != nil {
return nil, err
}
}

// TODO: index links.
// Move old links insert new links.

Expand All @@ -145,6 +178,38 @@ func (api *DocsAPI) GetDraft(ctx context.Context, in *documents.GetDraftRequest)
return docToProto(draft.doc)
}

func (api *DocsAPI) ListDrafts(ctx context.Context, in *documents.ListDraftsRequest) (*documents.ListDraftsResponse, error) {
conn, release, err := api.db.Conn(ctx)
if err != nil {
return nil, err
}
defer release()

res, err := vcssql.DraftsList(conn)
if err != nil {
return nil, err
}

out := &documents.ListDraftsResponse{
Documents: make([]*documents.Document, len(res)),
}

aid := api.me.AccountID().String()

for i, l := range res {
out.Documents[i] = &documents.Document{
Id: cid.NewCidV1(uint64(l.ObjectsCodec), l.ObjectsMultihash).String(),
Author: aid,
Title: l.DraftsTitle,
Subtitle: l.DraftsSubtitle,
CreateTime: &timestamppb.Timestamp{Seconds: int64(l.DraftsCreateTime)},
UpdateTime: &timestamppb.Timestamp{Seconds: int64(l.DraftsUpdateTime)},
}
}

return out, nil
}

func (api *DocsAPI) PublishDraft(ctx context.Context, in *documents.PublishDraftRequest) (*documents.Publication, error) {
oid, err := cid.Decode(in.DocumentId)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,17 @@ func TestAPIUpdateDraft(t *testing.T) {

testutil.ProtoEqual(t, want, updated, "UpdateDraft should return the updated document")

// list, err := api.ListDrafts(ctx, &documents.ListDraftsRequest{})
// require.NoError(t, err)
// require.Len(t, list.Documents, 1)
// require.Equal(t, updated.Id, list.Documents[0].Id)
// require.Equal(t, updated.Author, list.Documents[0].Author)
// require.Equal(t, updated.Title, list.Documents[0].Title)
list, err := api.ListDrafts(ctx, &documents.ListDraftsRequest{})
require.NoError(t, err)
require.Len(t, list.Documents, 1)
require.Equal(t, updated.Id, list.Documents[0].Id)
require.Equal(t, updated.Author, list.Documents[0].Author)
require.Equal(t, updated.Title, list.Documents[0].Title)

// got, err := api.GetDraft(ctx, &documents.GetDraftRequest{DocumentId: draft.Id})
// require.NoError(t, err)
got, err := api.GetDraft(ctx, &documents.GetDraftRequest{DocumentId: draft.Id})
require.NoError(t, err)

// testutil.ProtoEqual(t, draft, got, "must get draft that was updated")
testutil.ProtoEqual(t, updated, got, "must get draft that was updated")
}

func TestAPIUpdateDraft_Complex(t *testing.T) {
Expand Down Expand Up @@ -435,9 +435,10 @@ func updateDraft(ctx context.Context, t *testing.T, api *DocsAPI, id string, upd
func newTestDocsAPI(t *testing.T, name string) *DocsAPI {
u := coretest.NewTester("alice")

v := vcs.New(newTestSQLite(t))
db := newTestSQLite(t)
v := vcs.New(db)

return NewDocsAPI(u.Identity, v)
return NewDocsAPI(u.Identity, db, v)
}

func newTestSQLite(t *testing.T) *sqlitex.Pool {
Expand Down
4 changes: 2 additions & 2 deletions backend/vcs_queries.gensum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
srcs: 6e7d4655b12a9ee7ff61f4a6bee4864c
outs: b2040e5b02f8b6c794a89e8d5c73e003
srcs: 33841ca30fc2fc523cfa3b1c9d22ad9c
outs: 371399a31c02115a9bf265cd0ab8771e
1 change: 1 addition & 0 deletions dev
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def main():

@cmd(cmds, "run-desktop", "Run Tauri desktop app for development.")
def run_desktop(args):
run("rm -rf target/debug/mintter*")
run("plz build //backend:mintterd //:yarn")
return run("cd desktop/app && cargo tauri dev", args=args)

Expand Down
Loading

0 comments on commit 7272793

Please sign in to comment.