memo_organizer.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package sqlite
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "github.com/usememos/memos/store"
  7. )
  8. func (d *DB) UpsertMemoOrganizer(ctx context.Context, upsert *store.MemoOrganizer) (*store.MemoOrganizer, error) {
  9. stmt := `
  10. INSERT INTO memo_organizer (
  11. memo_id,
  12. user_id,
  13. pinned
  14. )
  15. VALUES (?, ?, ?)
  16. ON CONFLICT(memo_id, user_id) DO UPDATE
  17. SET
  18. pinned = EXCLUDED.pinned
  19. `
  20. if _, err := d.db.ExecContext(ctx, stmt, upsert.MemoID, upsert.UserID, upsert.Pinned); err != nil {
  21. return nil, err
  22. }
  23. return upsert, nil
  24. }
  25. func (d *DB) ListMemoOrganizer(ctx context.Context, find *store.FindMemoOrganizer) ([]*store.MemoOrganizer, error) {
  26. where, args := []string{"1 = 1"}, []any{}
  27. if find.MemoID != 0 {
  28. where = append(where, "memo_id = ?")
  29. args = append(args, find.MemoID)
  30. }
  31. if find.UserID != 0 {
  32. where = append(where, "user_id = ?")
  33. args = append(args, find.UserID)
  34. }
  35. query := fmt.Sprintf(`
  36. SELECT
  37. memo_id,
  38. user_id,
  39. pinned
  40. FROM memo_organizer
  41. WHERE %s
  42. `, strings.Join(where, " AND "))
  43. rows, err := d.db.QueryContext(ctx, query, args...)
  44. if err != nil {
  45. return nil, err
  46. }
  47. defer rows.Close()
  48. list := []*store.MemoOrganizer{}
  49. for rows.Next() {
  50. memoOrganizer := &store.MemoOrganizer{}
  51. if err := rows.Scan(
  52. &memoOrganizer.MemoID,
  53. &memoOrganizer.UserID,
  54. &memoOrganizer.Pinned,
  55. ); err != nil {
  56. return nil, err
  57. }
  58. list = append(list, memoOrganizer)
  59. }
  60. if err := rows.Err(); err != nil {
  61. return nil, err
  62. }
  63. return list, nil
  64. }
  65. func (d *DB) DeleteMemoOrganizer(ctx context.Context, delete *store.DeleteMemoOrganizer) error {
  66. where, args := []string{}, []any{}
  67. if v := delete.MemoID; v != nil {
  68. where, args = append(where, "memo_id = ?"), append(args, *v)
  69. }
  70. if v := delete.UserID; v != nil {
  71. where, args = append(where, "user_id = ?"), append(args, *v)
  72. }
  73. stmt := `DELETE FROM memo_organizer WHERE ` + strings.Join(where, " AND ")
  74. if _, err := d.db.ExecContext(ctx, stmt, args...); err != nil {
  75. return err
  76. }
  77. return nil
  78. }