memos/store/db/mysql/reaction.go

108 lines
2.6 KiB
Go
Raw Normal View History

2024-02-07 23:40:23 +08:00
package mysql
import (
"context"
"strings"
2024-02-09 21:59:45 +08:00
"github.com/pkg/errors"
2024-02-09 22:18:55 +08:00
2024-02-07 23:40:23 +08:00
storepb "github.com/usememos/memos/proto/gen/store"
"github.com/usememos/memos/store"
)
2024-04-13 11:54:37 +08:00
func (d *DB) UpsertReaction(ctx context.Context, upsert *store.Reaction) (*store.Reaction, error) {
2024-02-07 23:40:23 +08:00
fields := []string{"`creator_id`", "`content_id`", "`reaction_type`"}
placeholder := []string{"?", "?", "?"}
2024-04-13 11:54:37 +08:00
args := []interface{}{upsert.CreatorID, upsert.ContentID, upsert.ReactionType.String()}
2024-02-09 21:59:45 +08:00
stmt := "INSERT INTO `reaction` (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ")"
result, err := d.db.ExecContext(ctx, stmt, args...)
if err != nil {
2024-02-07 23:40:23 +08:00
return nil, err
}
2024-02-09 21:59:45 +08:00
rawID, err := result.LastInsertId()
if err != nil {
return nil, err
}
id := int32(rawID)
reaction, err := d.GetReaction(ctx, &store.FindReaction{ID: &id})
if err != nil {
return nil, err
}
if reaction == nil {
return nil, errors.Errorf("failed to create reaction")
}
2024-02-07 23:40:23 +08:00
return reaction, nil
}
2024-04-13 11:54:37 +08:00
func (d *DB) ListReactions(ctx context.Context, find *store.FindReaction) ([]*store.Reaction, error) {
2024-02-07 23:40:23 +08:00
where, args := []string{"1 = 1"}, []interface{}{}
if find.ID != nil {
where, args = append(where, "`id` = ?"), append(args, *find.ID)
}
if find.CreatorID != nil {
where, args = append(where, "`creator_id` = ?"), append(args, *find.CreatorID)
}
if find.ContentID != nil {
where, args = append(where, "`content_id` = ?"), append(args, *find.ContentID)
}
rows, err := d.db.QueryContext(ctx, `
SELECT
id,
2024-02-09 21:59:45 +08:00
UNIX_TIMESTAMP(created_ts) AS created_ts,
2024-02-07 23:40:23 +08:00
creator_id,
content_id,
reaction_type
FROM reaction
WHERE `+strings.Join(where, " AND ")+`
2024-02-08 21:20:51 +08:00
ORDER BY id ASC`,
2024-02-07 23:40:23 +08:00
args...,
)
if err != nil {
return nil, err
}
defer rows.Close()
2024-04-13 11:54:37 +08:00
list := []*store.Reaction{}
2024-02-07 23:40:23 +08:00
for rows.Next() {
2024-04-13 11:54:37 +08:00
reaction := &store.Reaction{}
2024-02-07 23:40:23 +08:00
var reactionType string
if err := rows.Scan(
2024-04-13 11:54:37 +08:00
&reaction.ID,
2024-02-07 23:40:23 +08:00
&reaction.CreatedTs,
2024-04-13 11:54:37 +08:00
&reaction.CreatorID,
&reaction.ContentID,
2024-02-07 23:40:23 +08:00
&reactionType,
); err != nil {
return nil, err
}
2024-04-13 11:54:37 +08:00
reaction.ReactionType = storepb.ReactionType(storepb.ReactionType_value[reactionType])
2024-02-07 23:40:23 +08:00
list = append(list, reaction)
}
if err := rows.Err(); err != nil {
return nil, err
}
return list, nil
}
2024-04-13 11:54:37 +08:00
func (d *DB) GetReaction(ctx context.Context, find *store.FindReaction) (*store.Reaction, error) {
2024-02-09 21:59:45 +08:00
list, err := d.ListReactions(ctx, find)
if err != nil {
return nil, err
}
if len(list) == 0 {
return nil, nil
}
reaction := list[0]
return reaction, nil
}
2024-02-07 23:40:23 +08:00
func (d *DB) DeleteReaction(ctx context.Context, delete *store.DeleteReaction) error {
_, err := d.db.ExecContext(ctx, "DELETE FROM `reaction` WHERE `id` = ?", delete.ID)
return err
}