2022-02-03 15:32:03 +08:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
2022-08-07 10:17:12 +08:00
|
|
|
"context"
|
2022-05-22 00:59:22 +08:00
|
|
|
"database/sql"
|
2022-02-03 15:32:03 +08:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2022-06-27 22:09:06 +08:00
|
|
|
|
|
|
|
"github.com/usememos/memos/api"
|
|
|
|
"github.com/usememos/memos/common"
|
2022-02-03 15:32:03 +08:00
|
|
|
)
|
|
|
|
|
2022-05-19 18:32:04 +08:00
|
|
|
// shortcutRaw is the store model for an Shortcut.
|
|
|
|
// Fields have exactly the same meanings as Shortcut.
|
|
|
|
type shortcutRaw struct {
|
|
|
|
ID int
|
|
|
|
|
|
|
|
// Standard fields
|
|
|
|
RowStatus api.RowStatus
|
|
|
|
CreatorID int
|
|
|
|
CreatedTs int64
|
|
|
|
UpdatedTs int64
|
|
|
|
|
|
|
|
// Domain specific fields
|
|
|
|
Title string
|
|
|
|
Payload string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (raw *shortcutRaw) toShortcut() *api.Shortcut {
|
|
|
|
return &api.Shortcut{
|
|
|
|
ID: raw.ID,
|
|
|
|
|
|
|
|
RowStatus: raw.RowStatus,
|
|
|
|
CreatorID: raw.CreatorID,
|
|
|
|
CreatedTs: raw.CreatedTs,
|
|
|
|
UpdatedTs: raw.UpdatedTs,
|
|
|
|
|
|
|
|
Title: raw.Title,
|
|
|
|
Payload: raw.Payload,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-07 10:17:12 +08:00
|
|
|
func (s *Store) CreateShortcut(ctx context.Context, create *api.ShortcutCreate) (*api.Shortcut, error) {
|
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
shortcutRaw, err := createShortcut(ctx, tx, create)
|
2022-02-03 15:32:03 +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-05-19 18:32:04 +08:00
|
|
|
shortcut := shortcutRaw.toShortcut()
|
|
|
|
|
2022-08-07 08:09:43 +08:00
|
|
|
if err := s.cache.UpsertCache(api.ShortcutCache, shortcut.ID, shortcut); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
return shortcut, nil
|
|
|
|
}
|
|
|
|
|
2022-08-07 10:17:12 +08:00
|
|
|
func (s *Store) PatchShortcut(ctx context.Context, patch *api.ShortcutPatch) (*api.Shortcut, error) {
|
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
shortcutRaw, err := patchShortcut(ctx, tx, patch)
|
2022-02-03 15:32:03 +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-05-19 18:32:04 +08:00
|
|
|
shortcut := shortcutRaw.toShortcut()
|
|
|
|
|
2022-08-07 08:09:43 +08:00
|
|
|
if err := s.cache.UpsertCache(api.ShortcutCache, shortcut.ID, shortcut); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
return shortcut, nil
|
|
|
|
}
|
|
|
|
|
2022-08-07 10:17:12 +08:00
|
|
|
func (s *Store) FindShortcutList(ctx context.Context, find *api.ShortcutFind) ([]*api.Shortcut, error) {
|
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
shortcutRawList, err := findShortcutList(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.Shortcut{}
|
|
|
|
for _, raw := range shortcutRawList {
|
|
|
|
list = append(list, raw.toShortcut())
|
|
|
|
}
|
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
2022-08-07 10:17:12 +08:00
|
|
|
func (s *Store) FindShortcut(ctx context.Context, find *api.ShortcutFind) (*api.Shortcut, error) {
|
2022-08-07 08:09:43 +08:00
|
|
|
if find.ID != nil {
|
|
|
|
shortcut := &api.Shortcut{}
|
|
|
|
has, err := s.cache.FindCache(api.ShortcutCache, *find.ID, shortcut)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if has {
|
|
|
|
return shortcut, 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 := findShortcutList(ctx, tx, find)
|
2022-02-03 15:32:03 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(list) == 0 {
|
|
|
|
return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("not found")}
|
|
|
|
}
|
|
|
|
|
2022-05-19 18:32:04 +08:00
|
|
|
shortcut := list[0].toShortcut()
|
|
|
|
|
2022-08-07 08:09:43 +08:00
|
|
|
if err := s.cache.UpsertCache(api.ShortcutCache, shortcut.ID, shortcut); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-19 18:32:04 +08:00
|
|
|
return shortcut, nil
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
|
|
|
|
2022-08-07 10:17:12 +08:00
|
|
|
func (s *Store) DeleteShortcut(ctx context.Context, delete *api.ShortcutDelete) 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()
|
|
|
|
|
|
|
|
err = deleteShortcut(ctx, tx, delete)
|
|
|
|
if err != nil {
|
|
|
|
return FormatError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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.ShortcutCache, delete.ID)
|
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-07 10:17:12 +08:00
|
|
|
func createShortcut(ctx context.Context, tx *sql.Tx, create *api.ShortcutCreate) (*shortcutRaw, error) {
|
|
|
|
query := `
|
2022-02-03 15:32:03 +08:00
|
|
|
INSERT INTO shortcut (
|
|
|
|
title,
|
|
|
|
payload,
|
2022-02-04 21:24:21 +08:00
|
|
|
creator_id
|
2022-02-03 15:32:03 +08:00
|
|
|
)
|
|
|
|
VALUES (?, ?, ?)
|
2022-02-04 16:51:48 +08:00
|
|
|
RETURNING id, title, payload, creator_id, created_ts, updated_ts, row_status
|
2022-08-07 10:17:12 +08:00
|
|
|
`
|
2022-05-19 18:32:04 +08:00
|
|
|
var shortcutRaw shortcutRaw
|
2022-08-07 10:17:12 +08:00
|
|
|
if err := tx.QueryRowContext(ctx, query, create.Title, create.Payload, create.CreatorID).Scan(
|
2022-05-19 18:32:04 +08:00
|
|
|
&shortcutRaw.ID,
|
|
|
|
&shortcutRaw.Title,
|
|
|
|
&shortcutRaw.Payload,
|
|
|
|
&shortcutRaw.CreatorID,
|
|
|
|
&shortcutRaw.CreatedTs,
|
|
|
|
&shortcutRaw.UpdatedTs,
|
|
|
|
&shortcutRaw.RowStatus,
|
2022-02-03 15:32:03 +08:00
|
|
|
); err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
|
2022-05-19 18:32:04 +08:00
|
|
|
return &shortcutRaw, nil
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
|
|
|
|
2022-08-07 10:17:12 +08:00
|
|
|
func patchShortcut(ctx context.Context, tx *sql.Tx, patch *api.ShortcutPatch) (*shortcutRaw, error) {
|
2022-02-03 15:32:03 +08:00
|
|
|
set, args := []string{}, []interface{}{}
|
2022-02-04 21:24:21 +08:00
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
if v := patch.Title; v != nil {
|
|
|
|
set, args = append(set, "title = ?"), append(args, *v)
|
|
|
|
}
|
|
|
|
if v := patch.Payload; v != nil {
|
|
|
|
set, args = append(set, "payload = ?"), append(args, *v)
|
|
|
|
}
|
2022-02-04 16:51:48 +08:00
|
|
|
if v := patch.RowStatus; v != nil {
|
|
|
|
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-08-07 10:17:12 +08:00
|
|
|
query := `
|
2022-02-03 15:32:03 +08:00
|
|
|
UPDATE shortcut
|
2022-08-07 10:17:12 +08:00
|
|
|
SET ` + strings.Join(set, ", ") + `
|
2022-02-03 15:32:03 +08:00
|
|
|
WHERE id = ?
|
2022-02-04 16:51:48 +08:00
|
|
|
RETURNING id, title, payload, created_ts, updated_ts, row_status
|
2022-08-07 10:17:12 +08:00
|
|
|
`
|
2022-05-19 18:32:04 +08:00
|
|
|
var shortcutRaw shortcutRaw
|
2022-08-07 10:17:12 +08:00
|
|
|
if err := tx.QueryRowContext(ctx, query, args...).Scan(
|
2022-05-19 18:32:04 +08:00
|
|
|
&shortcutRaw.ID,
|
|
|
|
&shortcutRaw.Title,
|
|
|
|
&shortcutRaw.Payload,
|
|
|
|
&shortcutRaw.CreatedTs,
|
|
|
|
&shortcutRaw.UpdatedTs,
|
|
|
|
&shortcutRaw.RowStatus,
|
2022-02-03 15:32:03 +08:00
|
|
|
); err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
|
2022-05-19 18:32:04 +08:00
|
|
|
return &shortcutRaw, nil
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
|
|
|
|
2022-08-07 10:17:12 +08:00
|
|
|
func findShortcutList(ctx context.Context, tx *sql.Tx, find *api.ShortcutFind) ([]*shortcutRaw, error) {
|
2022-02-03 15:32:03 +08:00
|
|
|
where, args := []string{"1 = 1"}, []interface{}{}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
if v := find.Title; v != nil {
|
|
|
|
where, args = append(where, "title = ?"), append(args, *v)
|
|
|
|
}
|
|
|
|
|
2022-08-07 10:17:12 +08:00
|
|
|
rows, err := tx.QueryContext(ctx, `
|
2022-02-03 15:32:03 +08:00
|
|
|
SELECT
|
|
|
|
id,
|
|
|
|
title,
|
|
|
|
payload,
|
|
|
|
creator_id,
|
|
|
|
created_ts,
|
|
|
|
updated_ts,
|
2022-02-04 16:51:48 +08:00
|
|
|
row_status
|
2022-02-03 15:32:03 +08:00
|
|
|
FROM shortcut
|
2022-05-22 12:35:57 +08:00
|
|
|
WHERE `+strings.Join(where, " AND ")+`
|
|
|
|
ORDER BY created_ts DESC`,
|
2022-02-03 15:32:03 +08:00
|
|
|
args...,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2022-05-19 18:32:04 +08:00
|
|
|
shortcutRawList := make([]*shortcutRaw, 0)
|
2022-02-03 15:32:03 +08:00
|
|
|
for rows.Next() {
|
2022-05-19 18:32:04 +08:00
|
|
|
var shortcutRaw shortcutRaw
|
2022-02-03 15:32:03 +08:00
|
|
|
if err := rows.Scan(
|
2022-05-19 18:32:04 +08:00
|
|
|
&shortcutRaw.ID,
|
|
|
|
&shortcutRaw.Title,
|
|
|
|
&shortcutRaw.Payload,
|
|
|
|
&shortcutRaw.CreatorID,
|
|
|
|
&shortcutRaw.CreatedTs,
|
|
|
|
&shortcutRaw.UpdatedTs,
|
|
|
|
&shortcutRaw.RowStatus,
|
2022-02-03 15:32:03 +08:00
|
|
|
); err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
|
2022-05-19 18:32:04 +08:00
|
|
|
shortcutRawList = append(shortcutRawList, &shortcutRaw)
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
|
2022-05-19 18:32:04 +08:00
|
|
|
return shortcutRawList, nil
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
|
|
|
|
2022-08-07 10:17:12 +08:00
|
|
|
func deleteShortcut(ctx context.Context, tx *sql.Tx, delete *api.ShortcutDelete) error {
|
2022-09-03 18:54:22 +08:00
|
|
|
result, err := tx.ExecContext(ctx, `
|
2022-07-26 22:32:26 +08:00
|
|
|
DELETE FROM shortcut WHERE id = ?
|
|
|
|
`, delete.ID)
|
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 {
|
|
|
|
return &common.Error{Code: common.NotFound, Err: fmt.Errorf("shortcut ID not found: %d", delete.ID)}
|
|
|
|
}
|
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
return nil
|
|
|
|
}
|