memo.go 2.5 KB

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