reaction.go 2.5 KB

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