refactor: update memo tags

This commit is contained in:
Steven
2024-05-08 20:03:01 +08:00
parent 2c270438ec
commit d0655ece53
61 changed files with 1815 additions and 2855 deletions

View File

@@ -2,6 +2,7 @@ package mysql
import (
"context"
"encoding/json"
"fmt"
"strings"
@@ -11,9 +12,17 @@ import (
)
func (d *DB) CreateMemo(ctx context.Context, create *store.Memo) (*store.Memo, error) {
fields := []string{"`uid`", "`creator_id`", "`content`", "`visibility`"}
placeholder := []string{"?", "?", "?", "?"}
args := []any{create.UID, create.CreatorID, create.Content, create.Visibility}
fields := []string{"`uid`", "`creator_id`", "`content`", "`visibility`", "`tags`"}
placeholder := []string{"?", "?", "?", "?", "?"}
tags := "[]"
if len(create.Tags) != 0 {
tagsBytes, err := json.Marshal(create.Tags)
if err != nil {
return nil, err
}
tags = string(tagsBytes)
}
args := []any{create.UID, create.CreatorID, create.Content, create.Visibility, tags}
stmt := "INSERT INTO `memo` (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ")"
result, err := d.db.ExecContext(ctx, stmt, args...)
@@ -76,6 +85,9 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
}
where = append(where, fmt.Sprintf("`memo`.`visibility` in (%s)", strings.Join(placeholder, ",")))
}
if v := find.Tag; v != nil {
where, args = append(where, "JSON_CONTAINS(`memo`.`tags`, ?, '$')"), append(args, *v)
}
if find.ExcludeComments {
having = append(having, "`parent_id` IS NULL")
}
@@ -102,6 +114,7 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
"UNIX_TIMESTAMP(`memo`.`updated_ts`) AS `updated_ts`",
"`memo`.`row_status` AS `row_status`",
"`memo`.`visibility` AS `visibility`",
"`memo`.`tags` AS `tags`",
"IFNULL(`memo_organizer`.`pinned`, 0) AS `pinned`",
"`memo_relation`.`related_memo_id` AS `parent_id`",
}
@@ -126,6 +139,7 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
list := make([]*store.Memo, 0)
for rows.Next() {
var memo store.Memo
var tagsBytes []byte
dests := []any{
&memo.ID,
&memo.UID,
@@ -134,6 +148,7 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
&memo.UpdatedTs,
&memo.RowStatus,
&memo.Visibility,
&tagsBytes,
&memo.Pinned,
&memo.ParentID,
}
@@ -143,6 +158,9 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
if err := rows.Scan(dests...); err != nil {
return nil, err
}
if err := json.Unmarshal(tagsBytes, &memo.Tags); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal tags")
}
list = append(list, &memo)
}
@@ -186,6 +204,13 @@ func (d *DB) UpdateMemo(ctx context.Context, update *store.UpdateMemo) error {
if v := update.Visibility; v != nil {
set, args = append(set, "`visibility` = ?"), append(args, *v)
}
if v := update.Tags; v != nil {
tagsBytes, err := json.Marshal(v)
if err != nil {
return err
}
set, args = append(set, "`tags` = ?"), append(args, string(tagsBytes))
}
args = append(args, update.ID)
stmt := "UPDATE `memo` SET " + strings.Join(set, ", ") + " WHERE `id` = ?"