refactor: remove call to db for parent memo name (#4947)

pull/4966/head
varsnotwars 2 months ago committed by GitHub
parent 896a3dccf2
commit fa2fa8a5d7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -43,15 +43,9 @@ func (s *APIV1Service) convertMemoFromStore(ctx context.Context, memo *store.Mem
memoMessage.Property = convertMemoPropertyFromStore(memo.Payload.Property) memoMessage.Property = convertMemoPropertyFromStore(memo.Payload.Property)
memoMessage.Location = convertLocationFromStore(memo.Payload.Location) memoMessage.Location = convertLocationFromStore(memo.Payload.Location)
} }
if memo.ParentID != nil {
parent, err := s.Store.GetMemo(ctx, &store.FindMemo{ if memo.ParentUID != nil {
ID: memo.ParentID, parentName := fmt.Sprintf("%s%s", MemoNamePrefix, *memo.ParentUID)
ExcludeContent: true,
})
if err != nil {
return nil, errors.Wrap(err, "failed to get parent memo")
}
parentName := fmt.Sprintf("%s%s", MemoNamePrefix, parent.UID)
memoMessage.Parent = &parentName memoMessage.Parent = &parentName
} }

@ -90,7 +90,7 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
where = append(where, fmt.Sprintf("`memo`.`visibility` in (%s)", strings.Join(placeholder, ","))) where = append(where, fmt.Sprintf("`memo`.`visibility` in (%s)", strings.Join(placeholder, ",")))
} }
if find.ExcludeComments { if find.ExcludeComments {
having = append(having, "`parent_id` IS NULL") having = append(having, "`parent_uid` IS NULL")
} }
order := "DESC" order := "DESC"
@ -113,7 +113,7 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
"`memo`.`visibility` AS `visibility`", "`memo`.`visibility` AS `visibility`",
"`memo`.`pinned` AS `pinned`", "`memo`.`pinned` AS `pinned`",
"`memo`.`payload` AS `payload`", "`memo`.`payload` AS `payload`",
"`memo_relation`.`related_memo_id` AS `parent_id`", "CASE WHEN `parent_memo`.`uid` IS NOT NULL THEN `parent_memo`.`uid` ELSE NULL END AS `parent_uid`",
} }
if !find.ExcludeContent { if !find.ExcludeContent {
fields = append(fields, "`memo`.`content` AS `content`") fields = append(fields, "`memo`.`content` AS `content`")
@ -121,6 +121,7 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
query := "SELECT " + strings.Join(fields, ", ") + " FROM `memo`" + " " + query := "SELECT " + strings.Join(fields, ", ") + " FROM `memo`" + " " +
"LEFT JOIN `memo_relation` ON `memo`.`id` = `memo_relation`.`memo_id` AND `memo_relation`.`type` = 'COMMENT'" + " " + "LEFT JOIN `memo_relation` ON `memo`.`id` = `memo_relation`.`memo_id` AND `memo_relation`.`type` = 'COMMENT'" + " " +
"LEFT JOIN `memo` AS `parent_memo` ON `memo_relation`.`related_memo_id` = `parent_memo`.`id`" + " " +
"WHERE " + strings.Join(where, " AND ") + " " + "WHERE " + strings.Join(where, " AND ") + " " +
"HAVING " + strings.Join(having, " AND ") + " " + "HAVING " + strings.Join(having, " AND ") + " " +
"ORDER BY " + strings.Join(orderBy, ", ") "ORDER BY " + strings.Join(orderBy, ", ")
@ -151,7 +152,7 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
&memo.Visibility, &memo.Visibility,
&memo.Pinned, &memo.Pinned,
&payloadBytes, &payloadBytes,
&memo.ParentID, &memo.ParentUID,
} }
if !find.ExcludeContent { if !find.ExcludeContent {
dests = append(dests, &memo.Content) dests = append(dests, &memo.Content)

@ -105,7 +105,7 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
`memo.visibility AS visibility`, `memo.visibility AS visibility`,
`memo.pinned AS pinned`, `memo.pinned AS pinned`,
`memo.payload AS payload`, `memo.payload AS payload`,
`memo_relation.related_memo_id AS parent_id`, `CASE WHEN parent_memo.uid IS NOT NULL THEN parent_memo.uid ELSE NULL END AS parent_uid`,
} }
if !find.ExcludeContent { if !find.ExcludeContent {
fields = append(fields, `memo.content AS content`) fields = append(fields, `memo.content AS content`)
@ -114,6 +114,7 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
query := `SELECT ` + strings.Join(fields, ", ") + ` query := `SELECT ` + strings.Join(fields, ", ") + `
FROM memo FROM memo
LEFT JOIN memo_relation ON memo.id = memo_relation.memo_id AND memo_relation.type = 'COMMENT' LEFT JOIN memo_relation ON memo.id = memo_relation.memo_id AND memo_relation.type = 'COMMENT'
LEFT JOIN memo AS parent_memo ON memo_relation.related_memo_id = parent_memo.id
WHERE ` + strings.Join(where, " AND ") + ` WHERE ` + strings.Join(where, " AND ") + `
ORDER BY ` + strings.Join(orderBy, ", ") ORDER BY ` + strings.Join(orderBy, ", ")
if find.Limit != nil { if find.Limit != nil {
@ -143,7 +144,7 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
&memo.Visibility, &memo.Visibility,
&memo.Pinned, &memo.Pinned,
&payloadBytes, &payloadBytes,
&memo.ParentID, &memo.ParentUID,
} }
if !find.ExcludeContent { if !find.ExcludeContent {
dests = append(dests, &memo.Content) dests = append(dests, &memo.Content)

@ -82,7 +82,7 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
where = append(where, fmt.Sprintf("`memo`.`visibility` IN (%s)", strings.Join(placeholder, ","))) where = append(where, fmt.Sprintf("`memo`.`visibility` IN (%s)", strings.Join(placeholder, ",")))
} }
if find.ExcludeComments { if find.ExcludeComments {
where = append(where, "`parent_id` IS NULL") where = append(where, "`parent_uid` IS NULL")
} }
order := "DESC" order := "DESC"
@ -105,7 +105,7 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
"`memo`.`visibility` AS `visibility`", "`memo`.`visibility` AS `visibility`",
"`memo`.`pinned` AS `pinned`", "`memo`.`pinned` AS `pinned`",
"`memo`.`payload` AS `payload`", "`memo`.`payload` AS `payload`",
"`memo_relation`.`related_memo_id` AS `parent_id`", "CASE WHEN `parent_memo`.`uid` IS NOT NULL THEN `parent_memo`.`uid` ELSE NULL END AS `parent_uid`",
} }
if !find.ExcludeContent { if !find.ExcludeContent {
fields = append(fields, "`memo`.`content` AS `content`") fields = append(fields, "`memo`.`content` AS `content`")
@ -113,6 +113,7 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
query := "SELECT " + strings.Join(fields, ", ") + "FROM `memo` " + query := "SELECT " + strings.Join(fields, ", ") + "FROM `memo` " +
"LEFT JOIN `memo_relation` ON `memo`.`id` = `memo_relation`.`memo_id` AND `memo_relation`.`type` = \"COMMENT\" " + "LEFT JOIN `memo_relation` ON `memo`.`id` = `memo_relation`.`memo_id` AND `memo_relation`.`type` = \"COMMENT\" " +
"LEFT JOIN `memo` AS `parent_memo` ON `memo_relation`.`related_memo_id` = `parent_memo`.`id` " +
"WHERE " + strings.Join(where, " AND ") + " " + "WHERE " + strings.Join(where, " AND ") + " " +
"ORDER BY " + strings.Join(orderBy, ", ") "ORDER BY " + strings.Join(orderBy, ", ")
if find.Limit != nil { if find.Limit != nil {
@ -142,7 +143,7 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
&memo.Visibility, &memo.Visibility,
&memo.Pinned, &memo.Pinned,
&payloadBytes, &payloadBytes,
&memo.ParentID, &memo.ParentUID,
} }
if !find.ExcludeContent { if !find.ExcludeContent {
dests = append(dests, &memo.Content) dests = append(dests, &memo.Content)

@ -52,7 +52,7 @@ type Memo struct {
Payload *storepb.MemoPayload Payload *storepb.MemoPayload
// Composed fields // Composed fields
ParentID *int32 ParentUID *string
} }
type FindMemo struct { type FindMemo struct {

Loading…
Cancel
Save