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)
|
|
|
|
}
|
|
|
|
|
2023-02-11 14:19:26 +08:00
|
|
|
s.shortcutCache.Store(shortcutRaw.ID, shortcutRaw)
|
2022-10-01 10:37:02 +08:00
|
|
|
shortcut := shortcutRaw.toShortcut()
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-02-11 14:19:26 +08:00
|
|
|
s.shortcutCache.Store(shortcutRaw.ID, shortcutRaw)
|
2022-10-01 10:37:02 +08:00
|
|
|
shortcut := shortcutRaw.toShortcut()
|
|
|
|
|
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 {
|
2023-02-11 14:19:26 +08:00
|
|
|
if shortcut, ok := s.shortcutCache.Load(*find.ID); ok {
|
|
|
|
return shortcut.(*shortcutRaw).toShortcut(), nil
|
2022-08-07 08:09:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-10-01 10:37:02 +08:00
|
|
|
shortcutRaw := list[0]
|
2023-02-11 14:19:26 +08:00
|
|
|
s.shortcutCache.Store(shortcutRaw.ID, shortcutRaw)
|
2022-10-01 10:37:02 +08:00
|
|
|
shortcut := shortcutRaw.toShortcut()
|
|
|
|
|
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
|
|
|
|
2023-02-11 14:19:26 +08:00
|
|
|
s.shortcutCache.Delete(*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-10-26 23:00:09 +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.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 = ?
|
2023-01-07 12:45:55 +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, args...).Scan(
|
2022-05-19 18:32:04 +08:00
|
|
|
&shortcutRaw.ID,
|
|
|
|
&shortcutRaw.Title,
|
|
|
|
&shortcutRaw.Payload,
|
2023-01-07 12:45:55 +08:00
|
|
|
&shortcutRaw.CreatorID,
|
2022-05-19 18:32:04 +08:00
|
|
|
&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-11-06 12:21:58 +08:00
|
|
|
where, args := []string{}, []interface{}{}
|
|
|
|
|
|
|
|
if v := delete.ID; v != nil {
|
|
|
|
where, args = append(where, "id = ?"), append(args, *v)
|
|
|
|
}
|
|
|
|
if v := delete.CreatorID; v != nil {
|
|
|
|
where, args = append(where, "creator_id = ?"), append(args, *v)
|
|
|
|
}
|
|
|
|
|
|
|
|
stmt := `DELETE FROM shortcut 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("shortcut not found")}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func vacuumShortcut(ctx context.Context, tx *sql.Tx) error {
|
|
|
|
stmt := `
|
|
|
|
DELETE FROM
|
|
|
|
shortcut
|
|
|
|
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
|
|
|
|
}
|