chore: fix memo parent_id

This commit is contained in:
Steven
2024-01-06 13:22:02 +08:00
parent f5a1739472
commit fd8333eeda
3 changed files with 29 additions and 17 deletions

View File

@ -38,7 +38,7 @@ func (d *DB) CreateMemo(ctx context.Context, create *store.Memo) (*store.Memo, e
} }
func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo, error) { func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo, error) {
where, args := []string{"1 = 1"}, []any{} where, having, args := []string{"1 = 1"}, []string{"1 = 1"}, []any{}
if v := find.ID; v != nil { if v := find.ID; v != nil {
where, args = append(where, "`memo`.`id` = ?"), append(args, *v) where, args = append(where, "`memo`.`id` = ?"), append(args, *v)
@ -69,7 +69,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") having = append(having, "`parent_id` IS NULL")
} }
orders := []string{} orders := []string{}
@ -91,10 +91,10 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
"`memo`.`row_status` AS `row_status`", "`memo`.`row_status` AS `row_status`",
"`memo`.`content` AS `content`", "`memo`.`content` AS `content`",
"`memo`.`visibility` AS `visibility`", "`memo`.`visibility` AS `visibility`",
"MAX(CASE WHEN `memo_organizer`.`pinned` = 1 THEN 1 ELSE 0 END) AS `pinned`", "`memo_organizer`.`pinned` AS `pinned`",
"(SELECT `related_memo_id` from `memo_relation` where `memo_id` = `id` AND `type` = \"COMMENT\" LIMIT 1) as `parent_id`", "`memo_relation`.`related_memo_id` AS `parent_id`",
} }
query := "SELECT " + strings.Join(fields, ",\n") + " FROM `memo` LEFT JOIN `memo_organizer` ON `memo`.`id` = `memo_organizer`.`memo_id` WHERE " + strings.Join(where, " AND ") + " GROUP BY `memo`.`id` ORDER BY " + strings.Join(orders, ", ") query := "SELECT " + strings.Join(fields, ",\n") + " FROM `memo` LEFT JOIN `memo_organizer` ON `memo`.`id` = `memo_organizer`.`memo_id` AND `memo`.`creator_id` = `memo_organizer`.`user_id` LEFT JOIN `memo_relation` ON `memo`.`id` = `memo_relation`.`memo_id` AND `memo_relation`.`type` = \"COMMENT\" WHERE " + strings.Join(where, " AND ") + " HAVING " + strings.Join(having, " AND ") + " ORDER BY " + strings.Join(orders, ", ")
if find.Limit != nil { if find.Limit != nil {
query = fmt.Sprintf("%s LIMIT %d", query, *find.Limit) query = fmt.Sprintf("%s LIMIT %d", query, *find.Limit)
if find.Offset != nil { if find.Offset != nil {
@ -111,6 +111,7 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
list := make([]*store.Memo, 0) list := make([]*store.Memo, 0)
for rows.Next() { for rows.Next() {
var memo store.Memo var memo store.Memo
pinned := sql.NullBool{}
if err := rows.Scan( if err := rows.Scan(
&memo.ID, &memo.ID,
&memo.CreatorID, &memo.CreatorID,
@ -119,11 +120,14 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
&memo.RowStatus, &memo.RowStatus,
&memo.Content, &memo.Content,
&memo.Visibility, &memo.Visibility,
&memo.Pinned, &pinned,
&memo.ParentID, &memo.ParentID,
); err != nil { ); err != nil {
return nil, err return nil, err
} }
if pinned.Valid {
memo.Pinned = pinned.Bool
}
list = append(list, &memo) list = append(list, &memo)
} }

View File

@ -60,7 +60,7 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
where = append(where, fmt.Sprintf("memo.visibility in (%s)", strings.Join(holders, ", "))) where = append(where, fmt.Sprintf("memo.visibility in (%s)", strings.Join(holders, ", ")))
} }
if find.ExcludeComments { if find.ExcludeComments {
where = append(where, "parent_id IS NULL") where = append(where, "memo_relation.related_memo_id IS NULL")
} }
orders := []string{} orders := []string{}
@ -81,8 +81,8 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
`memo.updated_ts AS updated_ts`, `memo.updated_ts AS updated_ts`,
`memo.row_status AS row_status`, `memo.row_status AS row_status`,
`memo.visibility AS visibility`, `memo.visibility AS visibility`,
`MAX(CASE WHEN memo_organizer.pinned = 1 THEN 1 ELSE 0 END) AS pinned`, `memo_organizer.pinned AS pinned`,
"(SELECT related_memo_id from memo_relation where memo_id = id AND type = 'COMMENT' LIMIT 1) as parent_id", `memo_relation.related_memo_id AS parent_id`,
} }
if !find.ExcludeContent { if !find.ExcludeContent {
fields = append(fields, `memo.content AS content`) fields = append(fields, `memo.content AS content`)
@ -90,9 +90,9 @@ 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_organizer ON memo.id = memo_organizer.memo_id FULl JOIN memo_organizer ON memo.id = memo_organizer.memo_id AND memo.creator_id = memo_organizer.user_id
FULL JOIN memo_relation ON memo.id = memo_relation.memo_id AND memo_relation.type = 'COMMENT'
WHERE ` + strings.Join(where, " AND ") + ` WHERE ` + strings.Join(where, " AND ") + `
GROUP BY memo.id
ORDER BY ` + strings.Join(orders, ", ") ORDER BY ` + strings.Join(orders, ", ")
if find.Limit != nil { if find.Limit != nil {
query = fmt.Sprintf("%s LIMIT %d", query, *find.Limit) query = fmt.Sprintf("%s LIMIT %d", query, *find.Limit)
@ -110,6 +110,7 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
list := make([]*store.Memo, 0) list := make([]*store.Memo, 0)
for rows.Next() { for rows.Next() {
var memo store.Memo var memo store.Memo
pinned := sql.NullBool{}
dests := []any{ dests := []any{
&memo.ID, &memo.ID,
&memo.CreatorID, &memo.CreatorID,
@ -117,7 +118,7 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
&memo.UpdatedTs, &memo.UpdatedTs,
&memo.RowStatus, &memo.RowStatus,
&memo.Visibility, &memo.Visibility,
&memo.Pinned, &pinned,
&memo.ParentID, &memo.ParentID,
} }
if !find.ExcludeContent { if !find.ExcludeContent {
@ -126,6 +127,9 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
if err := rows.Scan(dests...); err != nil { if err := rows.Scan(dests...); err != nil {
return nil, err return nil, err
} }
if pinned.Valid {
memo.Pinned = pinned.Bool
}
list = append(list, &memo) list = append(list, &memo)
} }

View File

@ -80,8 +80,8 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
`memo.updated_ts AS updated_ts`, `memo.updated_ts AS updated_ts`,
`memo.row_status AS row_status`, `memo.row_status AS row_status`,
`memo.visibility AS visibility`, `memo.visibility AS visibility`,
`CASE WHEN memo_organizer.pinned = 1 THEN 1 ELSE 0 END AS pinned`, `memo_organizer.pinned AS pinned`,
"(SELECT related_memo_id from memo_relation where memo_id = id AND type = \"COMMENT\" LIMIT 1) as parent_id", `memo_relation.related_memo_id AS parent_id`,
} }
if !find.ExcludeContent { if !find.ExcludeContent {
fields = append(fields, `memo.content AS content`) fields = append(fields, `memo.content AS content`)
@ -89,9 +89,9 @@ 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_organizer ON memo.id = memo_organizer.memo_id LEFT JOIN memo_organizer ON memo.id = memo_organizer.memo_id AND memo.creator_id = memo_organizer.user_id
FULL JOIN memo_relation ON memo.id = memo_relation.memo_id AND memo_relation.type = "COMMENT"
WHERE ` + strings.Join(where, " AND ") + ` WHERE ` + strings.Join(where, " AND ") + `
GROUP BY memo.id
ORDER BY ` + strings.Join(orders, ", ") ORDER BY ` + strings.Join(orders, ", ")
if find.Limit != nil { if find.Limit != nil {
query = fmt.Sprintf("%s LIMIT %d", query, *find.Limit) query = fmt.Sprintf("%s LIMIT %d", query, *find.Limit)
@ -109,6 +109,7 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
list := make([]*store.Memo, 0) list := make([]*store.Memo, 0)
for rows.Next() { for rows.Next() {
var memo store.Memo var memo store.Memo
pinned := sql.NullBool{}
dests := []any{ dests := []any{
&memo.ID, &memo.ID,
&memo.CreatorID, &memo.CreatorID,
@ -116,7 +117,7 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
&memo.UpdatedTs, &memo.UpdatedTs,
&memo.RowStatus, &memo.RowStatus,
&memo.Visibility, &memo.Visibility,
&memo.Pinned, &pinned,
&memo.ParentID, &memo.ParentID,
} }
if !find.ExcludeContent { if !find.ExcludeContent {
@ -125,6 +126,9 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
if err := rows.Scan(dests...); err != nil { if err := rows.Scan(dests...); err != nil {
return nil, err return nil, err
} }
if pinned.Valid {
memo.Pinned = pinned.Bool
}
list = append(list, &memo) list = append(list, &memo)
} }