2021-12-08 23:43:14 +08:00
|
|
|
package store
|
|
|
|
|
2021-12-09 22:02:57 +08:00
|
|
|
import (
|
2022-05-22 00:59:22 +08:00
|
|
|
"database/sql"
|
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
|
|
|
|
Content string
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
Content: raw.Content,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-16 07:37:23 +08:00
|
|
|
func (s *Store) CreateMemo(create *api.MemoCreate) (*api.Memo, error) {
|
2022-05-19 18:32:04 +08:00
|
|
|
memoRaw, err := createMemoRaw(s.db, create)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
memo, err := s.composeMemo(memoRaw)
|
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
|
|
|
return memo, nil
|
2021-12-08 23:43:14 +08:00
|
|
|
}
|
|
|
|
|
2022-05-16 07:37:23 +08:00
|
|
|
func (s *Store) PatchMemo(patch *api.MemoPatch) (*api.Memo, error) {
|
2022-05-19 18:32:04 +08:00
|
|
|
memoRaw, err := patchMemoRaw(s.db, patch)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
memo, err := s.composeMemo(memoRaw)
|
2022-02-03 15:32:03 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return memo, nil
|
2021-12-09 22:02:57 +08:00
|
|
|
}
|
|
|
|
|
2022-05-16 07:37:23 +08:00
|
|
|
func (s *Store) FindMemoList(find *api.MemoFind) ([]*api.Memo, error) {
|
2022-05-19 18:32:04 +08:00
|
|
|
memoRawList, err := findMemoRawList(s.db, 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 {
|
|
|
|
memo, err := s.composeMemo(raw)
|
|
|
|
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-05-16 07:37:23 +08:00
|
|
|
func (s *Store) FindMemo(find *api.MemoFind) (*api.Memo, error) {
|
2022-05-19 18:32:04 +08:00
|
|
|
list, err := findMemoRawList(s.db, 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-05-19 18:32:04 +08:00
|
|
|
memo, err := s.composeMemo(list[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return memo, nil
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
2021-12-08 23:43:14 +08:00
|
|
|
|
2022-05-16 07:37:23 +08:00
|
|
|
func (s *Store) DeleteMemo(delete *api.MemoDelete) error {
|
2022-02-03 15:32:03 +08:00
|
|
|
err := deleteMemo(s.db, delete)
|
|
|
|
if err != nil {
|
|
|
|
return FormatError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-12-08 23:43:14 +08:00
|
|
|
}
|
|
|
|
|
2022-05-22 00:59:22 +08:00
|
|
|
func createMemoRaw(db *sql.DB, create *api.MemoCreate) (*memoRaw, error) {
|
2022-05-15 22:21:13 +08:00
|
|
|
set := []string{"creator_id", "content"}
|
|
|
|
placeholder := []string{"?", "?"}
|
|
|
|
args := []interface{}{create.CreatorID, create.Content}
|
|
|
|
|
|
|
|
if v := create.CreatedTs; v != nil {
|
|
|
|
set, placeholder, args = append(set, "created_ts"), append(placeholder, "?"), append(args, *v)
|
|
|
|
}
|
|
|
|
|
2022-05-22 00:59:22 +08:00
|
|
|
row, err := db.Query(`
|
2022-02-03 15:32:03 +08:00
|
|
|
INSERT INTO memo (
|
2022-05-15 22:21:13 +08:00
|
|
|
`+strings.Join(set, ", ")+`
|
2022-02-03 15:32:03 +08:00
|
|
|
)
|
2022-05-15 22:21:13 +08:00
|
|
|
VALUES (`+strings.Join(placeholder, ",")+`)
|
2022-02-03 15:32:03 +08:00
|
|
|
RETURNING id, creator_id, created_ts, updated_ts, content, row_status
|
|
|
|
`,
|
2022-05-15 22:21:13 +08:00
|
|
|
args...,
|
2022-02-03 15:32:03 +08:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
defer row.Close()
|
|
|
|
|
2022-05-19 18:32:04 +08:00
|
|
|
row.Next()
|
|
|
|
var memoRaw memoRaw
|
2022-02-03 15:32:03 +08:00
|
|
|
if err := row.Scan(
|
2022-05-19 18:32:04 +08:00
|
|
|
&memoRaw.ID,
|
|
|
|
&memoRaw.CreatorID,
|
|
|
|
&memoRaw.CreatedTs,
|
|
|
|
&memoRaw.UpdatedTs,
|
|
|
|
&memoRaw.Content,
|
|
|
|
&memoRaw.RowStatus,
|
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-05-22 00:59:22 +08:00
|
|
|
func patchMemoRaw(db *sql.DB, patch *api.MemoPatch) (*memoRaw, error) {
|
2022-02-03 15:32:03 +08:00
|
|
|
set, args := []string{}, []interface{}{}
|
|
|
|
|
|
|
|
if v := patch.Content; v != nil {
|
2022-02-04 21:24:21 +08:00
|
|
|
set, args = append(set, "content = ?"), 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-05-03 02:05:43 +08:00
|
|
|
args = append(args, patch.ID)
|
2022-02-03 15:32:03 +08:00
|
|
|
|
2022-05-22 00:59:22 +08:00
|
|
|
row, err := db.Query(`
|
2022-02-03 15:32:03 +08:00
|
|
|
UPDATE memo
|
|
|
|
SET `+strings.Join(set, ", ")+`
|
|
|
|
WHERE id = ?
|
2022-07-04 21:27:44 +08:00
|
|
|
RETURNING id, creator_id, created_ts, updated_ts, content, row_status
|
2022-02-03 15:32:03 +08:00
|
|
|
`, args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
defer row.Close()
|
|
|
|
|
|
|
|
if !row.Next() {
|
|
|
|
return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("not found")}
|
|
|
|
}
|
|
|
|
|
2022-05-19 18:32:04 +08:00
|
|
|
var memoRaw memoRaw
|
2022-02-03 15:32:03 +08:00
|
|
|
if err := row.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.Content,
|
|
|
|
&memoRaw.RowStatus,
|
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-05-22 00:59:22 +08:00
|
|
|
func findMemoRawList(db *sql.DB, 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 {
|
|
|
|
where = append(where, "id in (SELECT memo_id FROM memo_organizer WHERE pinned = 1 AND user_id = memo.creator_id )")
|
|
|
|
}
|
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
|
|
|
}
|
2021-12-08 23:43:14 +08:00
|
|
|
|
2022-06-21 21:58:33 +08:00
|
|
|
pagination := ""
|
|
|
|
if find.Limit > 0 {
|
|
|
|
pagination = fmt.Sprintf("%s LIMIT %d", pagination, find.Limit)
|
|
|
|
if find.Offset > 0 {
|
|
|
|
pagination = fmt.Sprintf("%s OFFSET %d", pagination, find.Offset)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-22 00:59:22 +08:00
|
|
|
rows, err := db.Query(`
|
2022-02-03 15:32:03 +08:00
|
|
|
SELECT
|
|
|
|
id,
|
|
|
|
creator_id,
|
|
|
|
created_ts,
|
|
|
|
updated_ts,
|
|
|
|
content,
|
|
|
|
row_status
|
|
|
|
FROM memo
|
2022-05-22 12:35:57 +08:00
|
|
|
WHERE `+strings.Join(where, " AND ")+`
|
2022-06-21 21:58:33 +08:00
|
|
|
ORDER BY created_ts DESC`+pagination,
|
2022-02-03 15:32:03 +08:00
|
|
|
args...,
|
|
|
|
)
|
|
|
|
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.Content,
|
|
|
|
&memoRaw.RowStatus,
|
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-05-22 00:59:22 +08:00
|
|
|
func deleteMemo(db *sql.DB, delete *api.MemoDelete) error {
|
|
|
|
result, err := db.Exec(`DELETE FROM memo WHERE id = ?`, delete.ID)
|
2022-02-03 15:32:03 +08:00
|
|
|
if err != nil {
|
|
|
|
return FormatError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rows, _ := result.RowsAffected()
|
|
|
|
if rows == 0 {
|
2022-05-03 02:05:43 +08:00
|
|
|
return &common.Error{Code: common.NotFound, Err: fmt.Errorf("memo ID not found: %d", delete.ID)}
|
2021-12-10 13:41:17 +08:00
|
|
|
}
|
2021-12-09 22:02:57 +08:00
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
return nil
|
2021-12-08 23:43:14 +08:00
|
|
|
}
|
2022-05-19 18:32:04 +08:00
|
|
|
|
|
|
|
func (s *Store) composeMemo(raw *memoRaw) (*api.Memo, error) {
|
|
|
|
memo := raw.toMemo()
|
|
|
|
|
|
|
|
memoOrganizer, err := s.FindMemoOrganizer(&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
|
|
|
|
}
|
|
|
|
|
|
|
|
return memo, nil
|
|
|
|
}
|