memos/store/memo.go

139 lines
2.7 KiB
Go
Raw Normal View History

2021-12-08 23:43:14 +08:00
package store
2021-12-09 22:02:57 +08:00
import (
2022-08-07 10:17:12 +08:00
"context"
2024-01-20 23:48:35 +08:00
"errors"
"github.com/usememos/memos/internal/util"
2024-05-13 20:24:11 +08:00
storepb "github.com/usememos/memos/proto/gen/store"
2021-12-09 22:02:57 +08:00
)
2021-12-08 23:43:14 +08:00
2023-05-25 21:50:37 +08:00
// Visibility is the type of a visibility.
type Visibility string
const (
// Public is the PUBLIC visibility.
Public Visibility = "PUBLIC"
// Protected is the PROTECTED visibility.
Protected Visibility = "PROTECTED"
// Private is the PRIVATE visibility.
Private Visibility = "PRIVATE"
)
func (v Visibility) String() string {
switch v {
case Public:
return "PUBLIC"
case Protected:
return "PROTECTED"
case Private:
return "PRIVATE"
}
return "PRIVATE"
}
type Memo struct {
2024-03-20 20:39:16 +08:00
// ID is the system generated unique identifier for the memo.
ID int32
// UID is the user defined unique identifier for the memo.
UID string
2022-05-19 18:32:04 +08:00
// Standard fields
2023-05-25 21:50:37 +08:00
RowStatus RowStatus
2023-08-04 21:55:07 +08:00
CreatorID int32
2022-05-19 18:32:04 +08:00
CreatedTs int64
UpdatedTs int64
// Domain specific fields
Content string
2023-05-25 21:50:37 +08:00
Visibility Visibility
2024-05-13 20:24:11 +08:00
Payload *storepb.MemoPayload
2022-05-19 18:32:04 +08:00
2023-05-25 21:50:37 +08:00
// Composed fields
Pinned bool
ParentID *int32
2022-05-19 18:32:04 +08:00
}
type FindMemo struct {
2024-03-20 20:39:16 +08:00
ID *int32
UID *string
2022-08-07 10:17:12 +08:00
2023-05-25 21:50:37 +08:00
// Standard fields
2023-09-13 20:42:44 +08:00
RowStatus *RowStatus
CreatorID *int32
CreatedTsAfter *int64
CreatedTsBefore *int64
2024-01-18 14:30:20 +08:00
UpdatedTsAfter *int64
UpdatedTsBefore *int64
2022-08-07 10:17:12 +08:00
2023-05-25 21:50:37 +08:00
// Domain specific fields
ContentSearch []string
VisibilityList []Visibility
2024-05-13 22:04:37 +08:00
PayloadFind *FindMemoPayload
ExcludeContent bool
ExcludeComments bool
2024-03-30 13:50:18 +08:00
Random bool
2022-05-19 18:32:04 +08:00
2023-05-25 21:50:37 +08:00
// Pagination
Limit *int
Offset *int
OrderByUpdatedTs bool
2023-11-19 09:42:59 +08:00
OrderByPinned bool
2023-05-25 21:50:37 +08:00
}
2022-08-07 10:17:12 +08:00
2024-05-13 22:04:37 +08:00
type FindMemoPayload struct {
Raw *string
Tag *string
}
type UpdateMemo struct {
2024-03-20 20:39:16 +08:00
ID int32
UID *string
CreatedTs *int64
UpdatedTs *int64
RowStatus *RowStatus
Content *string
Visibility *Visibility
2024-05-13 20:24:11 +08:00
Payload *storepb.MemoPayload
2023-05-25 21:50:37 +08:00
}
2022-08-07 08:09:43 +08:00
type DeleteMemo struct {
2023-08-04 21:55:07 +08:00
ID int32
2021-12-08 23:43:14 +08:00
}
func (s *Store) CreateMemo(ctx context.Context, create *Memo) (*Memo, error) {
2024-03-20 20:39:16 +08:00
if !util.UIDMatcher.MatchString(create.UID) {
return nil, errors.New("invalid uid")
2024-01-20 23:48:35 +08:00
}
return s.driver.CreateMemo(ctx, create)
2021-12-09 22:02:57 +08:00
}
func (s *Store) ListMemos(ctx context.Context, find *FindMemo) ([]*Memo, error) {
return s.driver.ListMemos(ctx, find)
2022-11-06 12:21:58 +08:00
}
func (s *Store) GetMemo(ctx context.Context, find *FindMemo) (*Memo, error) {
list, err := s.ListMemos(ctx, find)
if err != nil {
return nil, err
}
if len(list) == 0 {
return nil, nil
}
memo := list[0]
return memo, nil
}
func (s *Store) UpdateMemo(ctx context.Context, update *UpdateMemo) error {
2024-03-20 20:39:16 +08:00
if update.UID != nil && !util.UIDMatcher.MatchString(*update.UID) {
return errors.New("invalid uid")
2024-01-20 23:48:35 +08:00
}
return s.driver.UpdateMemo(ctx, update)
}
func (s *Store) DeleteMemo(ctx context.Context, delete *DeleteMemo) error {
return s.driver.DeleteMemo(ctx, delete)
}