chore: add parent field to memo

This commit is contained in:
Steven 2023-10-01 16:27:40 +08:00
parent 922de07751
commit be046cae8e
4 changed files with 48 additions and 18 deletions

View file

@ -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
}

View file

@ -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

View file

@ -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)

View file

@ -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)