From be046cae8e0fba2da10570284f93b9a08185988a Mon Sep 17 00:00:00 2001 From: Steven Date: Sun, 1 Oct 2023 16:27:40 +0800 Subject: [PATCH] chore: add parent field to memo --- api/v1/memo.go | 17 +++++++++++++++++ store/memo.go | 3 +++ store/mysql/memo.go | 23 ++++++++++++++--------- store/sqlite/memo.go | 23 ++++++++++++++--------- 4 files changed, 48 insertions(+), 18 deletions(-) diff --git a/api/v1/memo.go b/api/v1/memo.go index 39972155..73d56ea8 100644 --- a/api/v1/memo.go +++ b/api/v1/memo.go @@ -57,6 +57,7 @@ type Memo struct { Pinned bool `json:"pinned"` // Related fields + Parent *Memo `json:"parent"` CreatorName string `json:"creatorName"` CreatorUsername string `json:"creatorUsername"` ResourceList []*Resource `json:"resourceList"` @@ -849,6 +850,22 @@ func (s *APIV1Service) convertMemoFromStore(ctx context.Context, memo *store.Mem } memoResponse.ResourceList = resourceList + if memo.ParentID != nil { + parentMemo, err := s.Store.GetMemo(ctx, &store.FindMemo{ + ID: memo.ParentID, + }) + if err != nil { + return nil, err + } + if parentMemo != nil { + parent, err := s.convertMemoFromStore(ctx, parentMemo) + if err != nil { + return nil, err + } + memoResponse.Parent = parent + } + } + return memoResponse, nil } diff --git a/store/memo.go b/store/memo.go index a37d61b9..338d6c4e 100644 --- a/store/memo.go +++ b/store/memo.go @@ -42,6 +42,9 @@ type Memo struct { Visibility Visibility // Composed fields + // For those comment memos, the parent ID is the memo ID of the memo being commented. + // If the parent ID is nil, then this memo is not a comment. + ParentID *int32 Pinned bool ResourceIDList []int32 RelationList []*MemoRelation diff --git a/store/mysql/memo.go b/store/mysql/memo.go index 92142e18..b898db0f 100644 --- a/store/mysql/memo.go +++ b/store/mysql/memo.go @@ -101,14 +101,14 @@ func (d *Driver) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store. MAX(CASE WHEN memo_organizer.pinned = 1 THEN 1 ELSE 0 END) AS pinned, GROUP_CONCAT(resource.id) AS resource_id_list, ( - SELECT - GROUP_CONCAT(related_memo_id,':',type) - FROM - memo_relation - WHERE - memo_relation.memo_id = memo.id - GROUP BY - memo_relation.memo_id + SELECT + GROUP_CONCAT(related_memo_id,':',type) + FROM + memo_relation + WHERE + memo_relation.memo_id = memo.id + GROUP BY + memo_relation.memo_id ) AS relation_list FROM memo @@ -176,11 +176,16 @@ func (d *Driver) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store. if err != nil { return nil, err } + relationType := store.MemoRelationType(relatedMemoTypeList[1]) memo.RelationList = append(memo.RelationList, &store.MemoRelation{ MemoID: memo.ID, RelatedMemoID: relatedMemoID, - Type: store.MemoRelationType(relatedMemoTypeList[1]), + Type: relationType, }) + // Set the first parent ID if relation type is comment. + if memo.ParentID == nil && relationType == store.MemoRelationComment { + memo.ParentID = &relatedMemoID + } } } list = append(list, &memo) diff --git a/store/sqlite/memo.go b/store/sqlite/memo.go index 4d66dc10..6a0cd673 100644 --- a/store/sqlite/memo.go +++ b/store/sqlite/memo.go @@ -101,14 +101,14 @@ func (d *Driver) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store. CASE WHEN memo_organizer.pinned = 1 THEN 1 ELSE 0 END AS pinned, GROUP_CONCAT(resource.id) AS resource_id_list, ( - SELECT - GROUP_CONCAT(related_memo_id || ':' || type) - FROM - memo_relation - WHERE - memo_relation.memo_id = memo.id - GROUP BY - memo_relation.memo_id + SELECT + GROUP_CONCAT(related_memo_id || ':' || type) + FROM + memo_relation + WHERE + memo_relation.memo_id = memo.id + GROUP BY + memo_relation.memo_id ) AS relation_list FROM memo @@ -176,11 +176,16 @@ func (d *Driver) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store. if err != nil { return nil, err } + relationType := store.MemoRelationType(relatedMemoTypeList[1]) memo.RelationList = append(memo.RelationList, &store.MemoRelation{ MemoID: memo.ID, RelatedMemoID: relatedMemoID, - Type: store.MemoRelationType(relatedMemoTypeList[1]), + Type: relationType, }) + // Set the first parent ID if relation type is comment. + if memo.ParentID == nil && relationType == store.MemoRelationComment { + memo.ParentID = &relatedMemoID + } } } list = append(list, &memo)