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"
|
2022-05-22 00:59:22 +08:00
|
|
|
"database/sql"
|
2022-10-21 19:57:57 +08:00
|
|
|
"encoding/json"
|
2022-02-03 15:32:03 +08:00
|
|
|
"fmt"
|
2021-12-09 22:02:57 +08:00
|
|
|
"strings"
|
2022-06-27 22:09:06 +08:00
|
|
|
|
|
|
|
"github.com/usememos/memos/api"
|
|
|
|
"github.com/usememos/memos/common"
|
2021-12-09 22:02:57 +08:00
|
|
|
)
|
2021-12-08 23:43:14 +08:00
|
|
|
|
2022-05-19 18:32:04 +08:00
|
|
|
// memoRaw is the store model for an Memo.
|
|
|
|
// Fields have exactly the same meanings as Memo.
|
|
|
|
type memoRaw struct {
|
|
|
|
ID int
|
|
|
|
|
|
|
|
// Standard fields
|
|
|
|
RowStatus api.RowStatus
|
|
|
|
CreatorID int
|
|
|
|
CreatedTs int64
|
|
|
|
UpdatedTs int64
|
|
|
|
|
|
|
|
// Domain specific fields
|
2022-07-08 22:23:27 +08:00
|
|
|
Content string
|
|
|
|
Visibility api.Visibility
|
2022-05-19 18:32:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// toMemo creates an instance of Memo based on the memoRaw.
|
|
|
|
// This is intended to be called when we need to compose an Memo relationship.
|
|
|
|
func (raw *memoRaw) toMemo() *api.Memo {
|
|
|
|
return &api.Memo{
|
|
|
|
ID: raw.ID,
|
|
|
|
|
|
|
|
// Standard fields
|
|
|
|
RowStatus: raw.RowStatus,
|
|
|
|
CreatorID: raw.CreatorID,
|
|
|
|
CreatedTs: raw.CreatedTs,
|
|
|
|
UpdatedTs: raw.UpdatedTs,
|
|
|
|
|
|
|
|
// Domain specific fields
|
2022-07-08 22:23:27 +08:00
|
|
|
Content: raw.Content,
|
|
|
|
Visibility: raw.Visibility,
|
2022-10-21 19:57:57 +08:00
|
|
|
DisplayTs: raw.CreatedTs,
|
2022-05-19 18:32:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-01 10:57:14 +08:00
|
|
|
func (s *Store) ComposeMemo(ctx context.Context, memo *api.Memo) (*api.Memo, error) {
|
2022-08-07 10:17:12 +08:00
|
|
|
memoOrganizer, err := s.FindMemoOrganizer(ctx, &api.MemoOrganizerFind{
|
|
|
|
MemoID: memo.ID,
|
|
|
|
UserID: memo.CreatorID,
|
|
|
|
})
|
|
|
|
if err != nil && common.ErrorCode(err) != common.NotFound {
|
|
|
|
return nil, err
|
|
|
|
} else if memoOrganizer != nil {
|
|
|
|
memo.Pinned = memoOrganizer.Pinned
|
|
|
|
}
|
|
|
|
|
2022-10-01 09:49:59 +08:00
|
|
|
if err = s.ComposeMemoCreator(ctx, memo); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err = s.ComposeMemoResourceList(ctx, memo); err != nil {
|
2022-09-05 21:14:17 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-21 20:26:00 +08:00
|
|
|
memoDisplayTsOptionKey := api.UserSettingMemoDisplayTsOptionKey
|
|
|
|
memoDisplayTsOptionSetting, err := s.FindUserSetting(ctx, &api.UserSettingFind{
|
2022-10-21 19:57:57 +08:00
|
|
|
UserID: memo.CreatorID,
|
2022-10-21 20:26:00 +08:00
|
|
|
Key: &memoDisplayTsOptionKey,
|
2022-10-21 19:57:57 +08:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-10-21 20:26:00 +08:00
|
|
|
memoDisplayTsOptionValue := "created_ts"
|
|
|
|
if memoDisplayTsOptionSetting != nil {
|
|
|
|
err = json.Unmarshal([]byte(memoDisplayTsOptionSetting.Value), &memoDisplayTsOptionValue)
|
2022-10-21 19:57:57 +08:00
|
|
|
if err != nil {
|
2022-10-21 20:26:00 +08:00
|
|
|
return nil, fmt.Errorf("failed to unmarshal user setting memo display ts option value")
|
2022-10-21 19:57:57 +08:00
|
|
|
}
|
|
|
|
}
|
2022-10-21 20:26:00 +08:00
|
|
|
if memoDisplayTsOptionValue == "updated_ts" {
|
2022-10-21 19:57:57 +08:00
|
|
|
memo.DisplayTs = memo.UpdatedTs
|
|
|
|
}
|
|
|
|
|
2022-08-07 10:17:12 +08:00
|
|
|
return memo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) CreateMemo(ctx context.Context, create *api.MemoCreate) (*api.Memo, error) {
|
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
memoRaw, err := createMemoRaw(ctx, tx, create)
|
2022-05-19 18:32:04 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-08-07 10:17:12 +08:00
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
|
2022-10-01 10:37:02 +08:00
|
|
|
if err := s.cache.UpsertCache(api.MemoCache, memoRaw.ID, memoRaw); err != nil {
|
2022-02-03 15:32:03 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-12-08 23:43:14 +08:00
|
|
|
|
2022-10-01 10:57:14 +08:00
|
|
|
memo, err := s.ComposeMemo(ctx, memoRaw.toMemo())
|
2022-10-01 10:37:02 +08:00
|
|
|
if err != nil {
|
2022-08-07 08:09:43 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
return memo, nil
|
2021-12-08 23:43:14 +08:00
|
|
|
}
|
|
|
|
|
2022-08-07 10:17:12 +08:00
|
|
|
func (s *Store) PatchMemo(ctx context.Context, patch *api.MemoPatch) (*api.Memo, error) {
|
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
memoRaw, err := patchMemoRaw(ctx, tx, patch)
|
2022-05-19 18:32:04 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-08-07 10:17:12 +08:00
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
|
2022-10-01 10:37:02 +08:00
|
|
|
if err := s.cache.UpsertCache(api.MemoCache, memoRaw.ID, memoRaw); err != nil {
|
2022-02-03 15:32:03 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-01 10:57:14 +08:00
|
|
|
memo, err := s.ComposeMemo(ctx, memoRaw.toMemo())
|
2022-10-01 10:37:02 +08:00
|
|
|
if err != nil {
|
2022-08-07 08:09:43 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
return memo, nil
|
2021-12-09 22:02:57 +08:00
|
|
|
}
|
|
|
|
|
2022-08-07 10:17:12 +08:00
|
|
|
func (s *Store) FindMemoList(ctx context.Context, find *api.MemoFind) ([]*api.Memo, error) {
|
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
memoRawList, err := findMemoRawList(ctx, tx, find)
|
2022-02-03 15:32:03 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-19 18:32:04 +08:00
|
|
|
list := []*api.Memo{}
|
|
|
|
for _, raw := range memoRawList {
|
2022-10-01 10:57:14 +08:00
|
|
|
memo, err := s.ComposeMemo(ctx, raw.toMemo())
|
2022-05-19 18:32:04 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
list = append(list, memo)
|
|
|
|
}
|
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
return list, nil
|
|
|
|
}
|
2021-12-08 23:43:14 +08:00
|
|
|
|
2022-08-07 10:17:12 +08:00
|
|
|
func (s *Store) FindMemo(ctx context.Context, find *api.MemoFind) (*api.Memo, error) {
|
2022-08-07 08:09:43 +08:00
|
|
|
if find.ID != nil {
|
2022-10-01 10:37:02 +08:00
|
|
|
memoRaw := &memoRaw{}
|
|
|
|
has, err := s.cache.FindCache(api.MemoCache, *find.ID, memoRaw)
|
2022-08-07 08:09:43 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if has {
|
2022-10-01 10:57:14 +08:00
|
|
|
memo, err := s.ComposeMemo(ctx, memoRaw.toMemo())
|
2022-10-01 10:37:02 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-08-07 08:09:43 +08:00
|
|
|
return memo, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-07 10:17:12 +08:00
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
list, err := findMemoRawList(ctx, tx, find)
|
2022-02-03 15:32:03 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-12-08 23:43:14 +08:00
|
|
|
}
|
2022-02-03 15:32:03 +08:00
|
|
|
|
|
|
|
if len(list) == 0 {
|
|
|
|
return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("not found")}
|
2021-12-08 23:43:14 +08:00
|
|
|
}
|
|
|
|
|
2022-10-01 10:37:02 +08:00
|
|
|
memoRaw := list[0]
|
|
|
|
if err := s.cache.UpsertCache(api.MemoCache, memoRaw.ID, memoRaw); err != nil {
|
2022-05-19 18:32:04 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-01 10:57:14 +08:00
|
|
|
memo, err := s.ComposeMemo(ctx, memoRaw.toMemo())
|
2022-10-01 10:37:02 +08:00
|
|
|
if err != nil {
|
2022-08-07 08:09:43 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-19 18:32:04 +08:00
|
|
|
return memo, nil
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
2021-12-08 23:43:14 +08:00
|
|
|
|
2022-08-07 10:17:12 +08:00
|
|
|
func (s *Store) DeleteMemo(ctx context.Context, delete *api.MemoDelete) error {
|
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
2022-02-03 15:32:03 +08:00
|
|
|
if err != nil {
|
|
|
|
return FormatError(err)
|
|
|
|
}
|
2022-08-07 10:17:12 +08:00
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
if err := deleteMemo(ctx, tx, delete); err != nil {
|
|
|
|
return FormatError(err)
|
|
|
|
}
|
2022-11-06 12:21:58 +08:00
|
|
|
if err := vacuum(ctx, tx); err != nil {
|
|
|
|
return err
|
2022-11-05 20:21:13 +08:00
|
|
|
}
|
|
|
|
|
2022-08-07 10:17:12 +08:00
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return FormatError(err)
|
|
|
|
}
|
2022-02-03 15:32:03 +08:00
|
|
|
|
2022-08-07 08:09:43 +08:00
|
|
|
s.cache.DeleteCache(api.MemoCache, delete.ID)
|
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
return nil
|
2021-12-08 23:43:14 +08:00
|
|
|
}
|
|
|
|
|
2022-08-07 10:17:12 +08:00
|
|
|
func createMemoRaw(ctx context.Context, tx *sql.Tx, create *api.MemoCreate) (*memoRaw, error) {
|
2022-08-20 21:22:36 +08:00
|
|
|
set := []string{"creator_id", "content", "visibility"}
|
|
|
|
args := []interface{}{create.CreatorID, create.Content, create.Visibility}
|
2022-08-25 22:02:32 +08:00
|
|
|
placeholder := []string{"?", "?", "?"}
|
2022-05-15 22:21:13 +08:00
|
|
|
|
2022-07-08 22:23:27 +08:00
|
|
|
query := `
|
2022-08-07 10:17:12 +08:00
|
|
|
INSERT INTO memo (
|
|
|
|
` + strings.Join(set, ", ") + `
|
|
|
|
)
|
|
|
|
VALUES (` + strings.Join(placeholder, ",") + `)
|
|
|
|
RETURNING id, creator_id, created_ts, updated_ts, row_status, content, visibility
|
|
|
|
`
|
2022-05-19 18:32:04 +08:00
|
|
|
var memoRaw memoRaw
|
2022-08-07 10:17:12 +08:00
|
|
|
if err := tx.QueryRowContext(ctx, query, args...).Scan(
|
2022-05-19 18:32:04 +08:00
|
|
|
&memoRaw.ID,
|
|
|
|
&memoRaw.CreatorID,
|
|
|
|
&memoRaw.CreatedTs,
|
|
|
|
&memoRaw.UpdatedTs,
|
|
|
|
&memoRaw.RowStatus,
|
2022-07-08 22:23:27 +08:00
|
|
|
&memoRaw.Content,
|
|
|
|
&memoRaw.Visibility,
|
2022-02-03 15:32:03 +08:00
|
|
|
); err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
|
2022-05-19 18:32:04 +08:00
|
|
|
return &memoRaw, nil
|
2021-12-09 22:02:57 +08:00
|
|
|
}
|
|
|
|
|
2022-08-07 10:17:12 +08:00
|
|
|
func patchMemoRaw(ctx context.Context, tx *sql.Tx, patch *api.MemoPatch) (*memoRaw, error) {
|
2022-02-03 15:32:03 +08:00
|
|
|
set, args := []string{}, []interface{}{}
|
|
|
|
|
2022-08-25 22:02:32 +08:00
|
|
|
if v := patch.CreatedTs; v != nil {
|
|
|
|
set, args = append(set, "created_ts = ?"), append(args, *v)
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
2022-10-21 19:57:57 +08:00
|
|
|
if v := patch.UpdatedTs; v != nil {
|
|
|
|
set, args = append(set, "updated_ts = ?"), append(args, *v)
|
|
|
|
}
|
2022-02-03 15:32:03 +08:00
|
|
|
if v := patch.RowStatus; v != nil {
|
2022-02-04 21:24:21 +08:00
|
|
|
set, args = append(set, "row_status = ?"), append(args, *v)
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
2022-08-25 22:02:32 +08:00
|
|
|
if v := patch.Content; v != nil {
|
|
|
|
set, args = append(set, "content = ?"), append(args, *v)
|
|
|
|
}
|
2022-07-08 22:23:27 +08:00
|
|
|
if v := patch.Visibility; v != nil {
|
|
|
|
set, args = append(set, "visibility = ?"), append(args, *v)
|
|
|
|
}
|
2022-02-03 15:32:03 +08:00
|
|
|
|
2022-05-03 02:05:43 +08:00
|
|
|
args = append(args, patch.ID)
|
2022-02-03 15:32:03 +08:00
|
|
|
|
2022-08-07 10:17:12 +08:00
|
|
|
query := `
|
2022-02-03 15:32:03 +08:00
|
|
|
UPDATE memo
|
2022-08-07 10:17:12 +08:00
|
|
|
SET ` + strings.Join(set, ", ") + `
|
2022-02-03 15:32:03 +08:00
|
|
|
WHERE id = ?
|
2022-07-08 22:23:27 +08:00
|
|
|
RETURNING id, creator_id, created_ts, updated_ts, row_status, content, visibility
|
2022-08-07 10:17:12 +08:00
|
|
|
`
|
2022-05-19 18:32:04 +08:00
|
|
|
var memoRaw memoRaw
|
2022-08-07 10:17:12 +08:00
|
|
|
if err := tx.QueryRowContext(ctx, query, args...).Scan(
|
2022-05-19 18:32:04 +08:00
|
|
|
&memoRaw.ID,
|
2022-07-04 21:27:44 +08:00
|
|
|
&memoRaw.CreatorID,
|
2022-05-19 18:32:04 +08:00
|
|
|
&memoRaw.CreatedTs,
|
|
|
|
&memoRaw.UpdatedTs,
|
|
|
|
&memoRaw.RowStatus,
|
2022-07-08 22:23:27 +08:00
|
|
|
&memoRaw.Content,
|
|
|
|
&memoRaw.Visibility,
|
2022-02-03 15:32:03 +08:00
|
|
|
); err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
|
2022-05-19 18:32:04 +08:00
|
|
|
return &memoRaw, nil
|
2021-12-08 23:43:14 +08:00
|
|
|
}
|
|
|
|
|
2022-08-07 10:17:12 +08:00
|
|
|
func findMemoRawList(ctx context.Context, tx *sql.Tx, find *api.MemoFind) ([]*memoRaw, error) {
|
2022-02-03 15:32:03 +08:00
|
|
|
where, args := []string{"1 = 1"}, []interface{}{}
|
2021-12-08 23:43:14 +08:00
|
|
|
|
2022-05-03 02:05:43 +08:00
|
|
|
if v := find.ID; v != nil {
|
2022-02-03 15:32:03 +08:00
|
|
|
where, args = append(where, "id = ?"), append(args, *v)
|
|
|
|
}
|
2022-05-03 02:05:43 +08:00
|
|
|
if v := find.CreatorID; v != nil {
|
2022-02-03 15:32:03 +08:00
|
|
|
where, args = append(where, "creator_id = ?"), append(args, *v)
|
2021-12-09 22:02:57 +08:00
|
|
|
}
|
2022-02-04 21:24:21 +08:00
|
|
|
if v := find.RowStatus; v != nil {
|
|
|
|
where, args = append(where, "row_status = ?"), append(args, *v)
|
|
|
|
}
|
2022-05-19 18:32:04 +08:00
|
|
|
if v := find.Pinned; v != nil {
|
2022-11-06 12:21:58 +08:00
|
|
|
where = append(where, "id IN (SELECT memo_id FROM memo_organizer WHERE pinned = 1 AND user_id = memo.creator_id)")
|
2022-05-19 18:32:04 +08:00
|
|
|
}
|
2022-06-21 21:58:33 +08:00
|
|
|
if v := find.ContentSearch; v != nil {
|
|
|
|
where, args = append(where, "content LIKE ?"), append(args, "%"+*v+"%")
|
2022-06-14 23:09:03 +08:00
|
|
|
}
|
2022-07-26 21:12:20 +08:00
|
|
|
if v := find.VisibilityList; len(v) != 0 {
|
|
|
|
list := []string{}
|
|
|
|
for _, visibility := range v {
|
|
|
|
list = append(list, fmt.Sprintf("$%d", len(args)+1))
|
|
|
|
args = append(args, visibility)
|
|
|
|
}
|
|
|
|
where = append(where, fmt.Sprintf("visibility in (%s)", strings.Join(list, ",")))
|
2022-07-08 22:23:27 +08:00
|
|
|
}
|
2021-12-08 23:43:14 +08:00
|
|
|
|
2022-08-07 10:17:12 +08:00
|
|
|
query := `
|
2022-02-03 15:32:03 +08:00
|
|
|
SELECT
|
|
|
|
id,
|
|
|
|
creator_id,
|
|
|
|
created_ts,
|
|
|
|
updated_ts,
|
2022-07-08 22:23:27 +08:00
|
|
|
row_status,
|
2022-02-03 15:32:03 +08:00
|
|
|
content,
|
2022-07-08 22:23:27 +08:00
|
|
|
visibility
|
2022-02-03 15:32:03 +08:00
|
|
|
FROM memo
|
2022-08-07 10:17:12 +08:00
|
|
|
WHERE ` + strings.Join(where, " AND ") + `
|
|
|
|
ORDER BY created_ts DESC
|
2022-10-27 22:02:42 +08:00
|
|
|
`
|
2022-08-07 10:17:12 +08:00
|
|
|
rows, err := tx.QueryContext(ctx, query, args...)
|
2022-02-03 15:32:03 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
2021-12-09 22:02:57 +08:00
|
|
|
defer rows.Close()
|
|
|
|
|
2022-05-19 18:32:04 +08:00
|
|
|
memoRawList := make([]*memoRaw, 0)
|
2021-12-08 23:43:14 +08:00
|
|
|
for rows.Next() {
|
2022-05-19 18:32:04 +08:00
|
|
|
var memoRaw memoRaw
|
2022-02-03 15:32:03 +08:00
|
|
|
if err := rows.Scan(
|
2022-05-19 18:32:04 +08:00
|
|
|
&memoRaw.ID,
|
|
|
|
&memoRaw.CreatorID,
|
|
|
|
&memoRaw.CreatedTs,
|
|
|
|
&memoRaw.UpdatedTs,
|
|
|
|
&memoRaw.RowStatus,
|
2022-07-08 22:23:27 +08:00
|
|
|
&memoRaw.Content,
|
|
|
|
&memoRaw.Visibility,
|
2022-02-03 15:32:03 +08:00
|
|
|
); err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
|
2022-05-19 18:32:04 +08:00
|
|
|
memoRawList = append(memoRawList, &memoRaw)
|
2021-12-08 23:43:14 +08:00
|
|
|
}
|
|
|
|
|
2021-12-10 13:41:17 +08:00
|
|
|
if err := rows.Err(); err != nil {
|
2022-02-03 15:32:03 +08:00
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
|
2022-05-19 18:32:04 +08:00
|
|
|
return memoRawList, nil
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
|
|
|
|
2022-08-07 10:17:12 +08:00
|
|
|
func deleteMemo(ctx context.Context, tx *sql.Tx, delete *api.MemoDelete) error {
|
2022-11-06 12:21:58 +08:00
|
|
|
where, args := []string{"id = ?"}, []interface{}{delete.ID}
|
|
|
|
|
|
|
|
stmt := `DELETE FROM memo WHERE ` + strings.Join(where, " AND ")
|
|
|
|
result, err := tx.ExecContext(ctx, stmt, args...)
|
2022-02-03 15:32:03 +08:00
|
|
|
if err != nil {
|
|
|
|
return FormatError(err)
|
|
|
|
}
|
|
|
|
|
2022-09-03 18:54:22 +08:00
|
|
|
rows, _ := result.RowsAffected()
|
|
|
|
if rows == 0 {
|
2022-11-06 12:21:58 +08:00
|
|
|
return &common.Error{Code: common.NotFound, Err: fmt.Errorf("memo not found")}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func vacuumMemo(ctx context.Context, tx *sql.Tx) error {
|
|
|
|
stmt := `
|
|
|
|
DELETE FROM
|
|
|
|
memo
|
|
|
|
WHERE
|
|
|
|
creator_id NOT IN (
|
|
|
|
SELECT
|
|
|
|
id
|
|
|
|
FROM
|
|
|
|
user
|
|
|
|
)`
|
|
|
|
_, err := tx.ExecContext(ctx, stmt)
|
|
|
|
if err != nil {
|
|
|
|
return FormatError(err)
|
2022-09-03 18:54:22 +08:00
|
|
|
}
|
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
return nil
|
2021-12-08 23:43:14 +08:00
|
|
|
}
|