From 349c3836047df2f76f91a11d5cec332d168522fb Mon Sep 17 00:00:00 2001 From: Steven Date: Fri, 14 Oct 2022 23:14:08 +0800 Subject: [PATCH] chore: reorder memo resource --- store/memo_resource.go | 21 +++++++++++++++++++++ store/resource.go | 22 ++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/store/memo_resource.go b/store/memo_resource.go index 0ad13a49..b920eb65 100644 --- a/store/memo_resource.go +++ b/store/memo_resource.go @@ -49,6 +49,27 @@ func (s *Store) FindMemoResourceList(ctx context.Context, find *api.MemoResource return list, nil } +func (s *Store) FindMemoResource(ctx context.Context, find *api.MemoResourceFind) (*api.MemoResource, error) { + tx, err := s.db.BeginTx(ctx, nil) + if err != nil { + return nil, FormatError(err) + } + defer tx.Rollback() + + list, err := findMemoResourceList(ctx, tx, find) + if err != nil { + return nil, err + } + + if len(list) == 0 { + return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("not found")} + } + + memoResourceRaw := list[0] + + return memoResourceRaw.toMemoResource(), nil +} + func (s *Store) UpsertMemoResource(ctx context.Context, upsert *api.MemoResourceUpsert) (*api.MemoResource, error) { tx, err := s.db.BeginTx(ctx, nil) if err != nil { diff --git a/store/resource.go b/store/resource.go index 58e55f3f..a1174c8f 100644 --- a/store/resource.go +++ b/store/resource.go @@ -4,6 +4,7 @@ import ( "context" "database/sql" "fmt" + "sort" "strings" "github.com/usememos/memos/api" @@ -52,6 +53,27 @@ func (s *Store) ComposeMemoResourceList(ctx context.Context, memo *api.Memo) err return err } + for _, resource := range resourceList { + memoResource, err := s.FindMemoResource(ctx, &api.MemoResourceFind{ + MemoID: &memo.ID, + ResourceID: &resource.ID, + }) + if err != nil { + return err + } + + resource.CreatedTs = memoResource.CreatedTs + resource.UpdatedTs = memoResource.UpdatedTs + } + + sort.Slice(resourceList, func(i, j int) bool { + if resourceList[i].CreatedTs != resourceList[j].CreatedTs { + return resourceList[i].CreatedTs < resourceList[j].CreatedTs + } + + return resourceList[i].ID < resourceList[j].ID + }) + memo.ResourceList = resourceList return nil