memo.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package store
  2. import (
  3. "context"
  4. "errors"
  5. "github.com/usememos/memos/internal/util"
  6. storepb "github.com/usememos/memos/proto/gen/store"
  7. )
  8. // Visibility is the type of a visibility.
  9. type Visibility string
  10. const (
  11. // Public is the PUBLIC visibility.
  12. Public Visibility = "PUBLIC"
  13. // Protected is the PROTECTED visibility.
  14. Protected Visibility = "PROTECTED"
  15. // Private is the PRIVATE visibility.
  16. Private Visibility = "PRIVATE"
  17. )
  18. func (v Visibility) String() string {
  19. switch v {
  20. case Public:
  21. return "PUBLIC"
  22. case Protected:
  23. return "PROTECTED"
  24. case Private:
  25. return "PRIVATE"
  26. }
  27. return "PRIVATE"
  28. }
  29. type Memo struct {
  30. // ID is the system generated unique identifier for the memo.
  31. ID int32
  32. // UID is the user defined unique identifier for the memo.
  33. UID string
  34. // Standard fields
  35. RowStatus RowStatus
  36. CreatorID int32
  37. CreatedTs int64
  38. UpdatedTs int64
  39. // Domain specific fields
  40. Content string
  41. Visibility Visibility
  42. Payload *storepb.MemoPayload
  43. // Composed fields
  44. Pinned bool
  45. ParentID *int32
  46. }
  47. type FindMemo struct {
  48. ID *int32
  49. UID *string
  50. // Standard fields
  51. RowStatus *RowStatus
  52. CreatorID *int32
  53. CreatedTsAfter *int64
  54. CreatedTsBefore *int64
  55. UpdatedTsAfter *int64
  56. UpdatedTsBefore *int64
  57. // Domain specific fields
  58. ContentSearch []string
  59. VisibilityList []Visibility
  60. PayloadFind *FindMemoPayload
  61. ExcludeContent bool
  62. ExcludeComments bool
  63. Random bool
  64. // Pagination
  65. Limit *int
  66. Offset *int
  67. // Ordering
  68. OrderByUpdatedTs bool
  69. OrderByPinned bool
  70. OrderByTimeAsc bool
  71. }
  72. type FindMemoPayload struct {
  73. Raw *string
  74. TagSearch []string
  75. HasLink bool
  76. HasTaskList bool
  77. HasCode bool
  78. HasIncompleteTasks bool
  79. }
  80. type UpdateMemo struct {
  81. ID int32
  82. UID *string
  83. CreatedTs *int64
  84. UpdatedTs *int64
  85. RowStatus *RowStatus
  86. Content *string
  87. Visibility *Visibility
  88. Payload *storepb.MemoPayload
  89. }
  90. type DeleteMemo struct {
  91. ID int32
  92. }
  93. func (s *Store) CreateMemo(ctx context.Context, create *Memo) (*Memo, error) {
  94. if !util.UIDMatcher.MatchString(create.UID) {
  95. return nil, errors.New("invalid uid")
  96. }
  97. return s.driver.CreateMemo(ctx, create)
  98. }
  99. func (s *Store) ListMemos(ctx context.Context, find *FindMemo) ([]*Memo, error) {
  100. return s.driver.ListMemos(ctx, find)
  101. }
  102. func (s *Store) GetMemo(ctx context.Context, find *FindMemo) (*Memo, error) {
  103. list, err := s.ListMemos(ctx, find)
  104. if err != nil {
  105. return nil, err
  106. }
  107. if len(list) == 0 {
  108. return nil, nil
  109. }
  110. memo := list[0]
  111. return memo, nil
  112. }
  113. func (s *Store) UpdateMemo(ctx context.Context, update *UpdateMemo) error {
  114. if update.UID != nil && !util.UIDMatcher.MatchString(*update.UID) {
  115. return errors.New("invalid uid")
  116. }
  117. return s.driver.UpdateMemo(ctx, update)
  118. }
  119. func (s *Store) DeleteMemo(ctx context.Context, delete *DeleteMemo) error {
  120. return s.driver.DeleteMemo(ctx, delete)
  121. }