memo.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. OrderByUpdatedTs bool
  68. OrderByPinned bool
  69. }
  70. type FindMemoPayload struct {
  71. Raw *string
  72. Tag *string
  73. HasLink bool
  74. HasTaskList bool
  75. HasCode bool
  76. HasIncompleteTasks bool
  77. }
  78. type UpdateMemo struct {
  79. ID int32
  80. UID *string
  81. CreatedTs *int64
  82. UpdatedTs *int64
  83. RowStatus *RowStatus
  84. Content *string
  85. Visibility *Visibility
  86. Payload *storepb.MemoPayload
  87. }
  88. type DeleteMemo struct {
  89. ID int32
  90. }
  91. func (s *Store) CreateMemo(ctx context.Context, create *Memo) (*Memo, error) {
  92. if !util.UIDMatcher.MatchString(create.UID) {
  93. return nil, errors.New("invalid uid")
  94. }
  95. return s.driver.CreateMemo(ctx, create)
  96. }
  97. func (s *Store) ListMemos(ctx context.Context, find *FindMemo) ([]*Memo, error) {
  98. return s.driver.ListMemos(ctx, find)
  99. }
  100. func (s *Store) GetMemo(ctx context.Context, find *FindMemo) (*Memo, error) {
  101. list, err := s.ListMemos(ctx, find)
  102. if err != nil {
  103. return nil, err
  104. }
  105. if len(list) == 0 {
  106. return nil, nil
  107. }
  108. memo := list[0]
  109. return memo, nil
  110. }
  111. func (s *Store) UpdateMemo(ctx context.Context, update *UpdateMemo) error {
  112. if update.UID != nil && !util.UIDMatcher.MatchString(*update.UID) {
  113. return errors.New("invalid uid")
  114. }
  115. return s.driver.UpdateMemo(ctx, update)
  116. }
  117. func (s *Store) DeleteMemo(ctx context.Context, delete *DeleteMemo) error {
  118. return s.driver.DeleteMemo(ctx, delete)
  119. }