memo_organizer.go 1.9 KB

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