reaction.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package mysql
  2. import (
  3. "context"
  4. "strings"
  5. "github.com/pkg/errors"
  6. storepb "github.com/usememos/memos/proto/gen/store"
  7. "github.com/usememos/memos/store"
  8. )
  9. func (d *DB) UpsertReaction(ctx context.Context, upsert *store.Reaction) (*store.Reaction, error) {
  10. fields := []string{"`creator_id`", "`content_id`", "`reaction_type`"}
  11. placeholder := []string{"?", "?", "?"}
  12. args := []interface{}{upsert.CreatorID, upsert.ContentID, upsert.ReactionType.String()}
  13. stmt := "INSERT INTO `reaction` (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ")"
  14. result, err := d.db.ExecContext(ctx, stmt, args...)
  15. if err != nil {
  16. return nil, err
  17. }
  18. rawID, err := result.LastInsertId()
  19. if err != nil {
  20. return nil, err
  21. }
  22. id := int32(rawID)
  23. reaction, err := d.GetReaction(ctx, &store.FindReaction{ID: &id})
  24. if err != nil {
  25. return nil, err
  26. }
  27. if reaction == nil {
  28. return nil, errors.Errorf("failed to create reaction")
  29. }
  30. return reaction, nil
  31. }
  32. func (d *DB) ListReactions(ctx context.Context, find *store.FindReaction) ([]*store.Reaction, error) {
  33. where, args := []string{"1 = 1"}, []interface{}{}
  34. if find.ID != nil {
  35. where, args = append(where, "`id` = ?"), append(args, *find.ID)
  36. }
  37. if find.CreatorID != nil {
  38. where, args = append(where, "`creator_id` = ?"), append(args, *find.CreatorID)
  39. }
  40. if find.ContentID != nil {
  41. where, args = append(where, "`content_id` = ?"), append(args, *find.ContentID)
  42. }
  43. rows, err := d.db.QueryContext(ctx, `
  44. SELECT
  45. id,
  46. UNIX_TIMESTAMP(created_ts) AS created_ts,
  47. creator_id,
  48. content_id,
  49. reaction_type
  50. FROM reaction
  51. WHERE `+strings.Join(where, " AND ")+`
  52. ORDER BY id ASC`,
  53. args...,
  54. )
  55. if err != nil {
  56. return nil, err
  57. }
  58. defer rows.Close()
  59. list := []*store.Reaction{}
  60. for rows.Next() {
  61. reaction := &store.Reaction{}
  62. var reactionType string
  63. if err := rows.Scan(
  64. &reaction.ID,
  65. &reaction.CreatedTs,
  66. &reaction.CreatorID,
  67. &reaction.ContentID,
  68. &reactionType,
  69. ); err != nil {
  70. return nil, err
  71. }
  72. reaction.ReactionType = storepb.ReactionType(storepb.ReactionType_value[reactionType])
  73. list = append(list, reaction)
  74. }
  75. if err := rows.Err(); err != nil {
  76. return nil, err
  77. }
  78. return list, nil
  79. }
  80. func (d *DB) GetReaction(ctx context.Context, find *store.FindReaction) (*store.Reaction, error) {
  81. list, err := d.ListReactions(ctx, find)
  82. if err != nil {
  83. return nil, err
  84. }
  85. if len(list) == 0 {
  86. return nil, nil
  87. }
  88. reaction := list[0]
  89. return reaction, nil
  90. }
  91. func (d *DB) DeleteReaction(ctx context.Context, delete *store.DeleteReaction) error {
  92. _, err := d.db.ExecContext(ctx, "DELETE FROM `reaction` WHERE `id` = ?", delete.ID)
  93. return err
  94. }