memos/store/shortcut.go

304 lines
6.5 KiB
Go
Raw Normal View History

2022-02-03 10:32:03 +03:00
package store
import (
2022-08-07 05:17:12 +03:00
"context"
2022-05-21 19:59:22 +03:00
"database/sql"
2022-02-03 10:32:03 +03:00
"fmt"
"strings"
2022-06-27 17:09:06 +03:00
"github.com/usememos/memos/api"
"github.com/usememos/memos/common"
2022-02-03 10:32:03 +03:00
)
2022-05-19 13:32:04 +03: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 05:17:12 +03: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 10:32:03 +03:00
if err != nil {
return nil, err
}
2022-08-07 05:17:12 +03:00
if err := tx.Commit(); err != nil {
return nil, FormatError(err)
}
2022-05-19 13:32:04 +03:00
shortcut := shortcutRaw.toShortcut()
2022-08-07 03:09:43 +03:00
if err := s.cache.UpsertCache(api.ShortcutCache, shortcut.ID, shortcut); err != nil {
return nil, err
}
2022-02-03 10:32:03 +03:00
return shortcut, nil
}
2022-08-07 05:17:12 +03: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 10:32:03 +03:00
if err != nil {
return nil, err
}
2022-08-07 05:17:12 +03:00
if err := tx.Commit(); err != nil {
return nil, FormatError(err)
}
2022-05-19 13:32:04 +03:00
shortcut := shortcutRaw.toShortcut()
2022-08-07 03:09:43 +03:00
if err := s.cache.UpsertCache(api.ShortcutCache, shortcut.ID, shortcut); err != nil {
return nil, err
}
2022-02-03 10:32:03 +03:00
return shortcut, nil
}
2022-08-07 05:17:12 +03: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 10:32:03 +03:00
if err != nil {
return nil, err
}
2022-05-19 13:32:04 +03:00
list := []*api.Shortcut{}
for _, raw := range shortcutRawList {
list = append(list, raw.toShortcut())
}
2022-02-03 10:32:03 +03:00
return list, nil
}
2022-08-07 05:17:12 +03:00
func (s *Store) FindShortcut(ctx context.Context, find *api.ShortcutFind) (*api.Shortcut, error) {
2022-08-07 03:09:43 +03: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 05:17:12 +03: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 10:32:03 +03: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 13:32:04 +03:00
shortcut := list[0].toShortcut()
2022-08-07 03:09:43 +03:00
if err := s.cache.UpsertCache(api.ShortcutCache, shortcut.ID, shortcut); err != nil {
return nil, err
}
2022-05-19 13:32:04 +03:00
return shortcut, nil
2022-02-03 10:32:03 +03:00
}
2022-08-07 05:17:12 +03:00
func (s *Store) DeleteShortcut(ctx context.Context, delete *api.ShortcutDelete) error {
tx, err := s.db.BeginTx(ctx, nil)
2022-02-03 10:32:03 +03:00
if err != nil {
return FormatError(err)
}
2022-08-07 05:17:12 +03: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 10:32:03 +03:00
2022-08-07 03:09:43 +03:00
s.cache.DeleteCache(api.ShortcutCache, delete.ID)
2022-02-03 10:32:03 +03:00
return nil
}
2022-08-07 05:17:12 +03:00
func createShortcut(ctx context.Context, tx *sql.Tx, create *api.ShortcutCreate) (*shortcutRaw, error) {
query := `
2022-02-03 10:32:03 +03:00
INSERT INTO shortcut (
title,
payload,
2022-02-04 16:24:21 +03:00
creator_id
2022-02-03 10:32:03 +03:00
)
VALUES (?, ?, ?)
RETURNING id, title, payload, creator_id, created_ts, updated_ts, row_status
2022-08-07 05:17:12 +03:00
`
2022-05-19 13:32:04 +03:00
var shortcutRaw shortcutRaw
2022-08-07 05:17:12 +03:00
if err := tx.QueryRowContext(ctx, query, create.Title, create.Payload, create.CreatorID).Scan(
2022-05-19 13:32:04 +03:00
&shortcutRaw.ID,
&shortcutRaw.Title,
&shortcutRaw.Payload,
&shortcutRaw.CreatorID,
&shortcutRaw.CreatedTs,
&shortcutRaw.UpdatedTs,
&shortcutRaw.RowStatus,
2022-02-03 10:32:03 +03:00
); err != nil {
return nil, FormatError(err)
}
2022-05-19 13:32:04 +03:00
return &shortcutRaw, nil
2022-02-03 10:32:03 +03:00
}
2022-08-07 05:17:12 +03:00
func patchShortcut(ctx context.Context, tx *sql.Tx, patch *api.ShortcutPatch) (*shortcutRaw, error) {
2022-02-03 10:32:03 +03:00
set, args := []string{}, []interface{}{}
2022-02-04 16:24:21 +03:00
2022-02-03 10:32:03 +03: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)
}
if v := patch.RowStatus; v != nil {
set, args = append(set, "row_status = ?"), append(args, *v)
2022-02-03 10:32:03 +03:00
}
2022-05-02 21:05:43 +03:00
args = append(args, patch.ID)
2022-02-03 10:32:03 +03:00
2022-08-07 05:17:12 +03:00
query := `
2022-02-03 10:32:03 +03:00
UPDATE shortcut
2022-08-07 05:17:12 +03:00
SET ` + strings.Join(set, ", ") + `
2022-02-03 10:32:03 +03:00
WHERE id = ?
RETURNING id, title, payload, created_ts, updated_ts, row_status
2022-08-07 05:17:12 +03:00
`
2022-05-19 13:32:04 +03:00
var shortcutRaw shortcutRaw
2022-08-07 05:17:12 +03:00
if err := tx.QueryRowContext(ctx, query, args...).Scan(
2022-05-19 13:32:04 +03:00
&shortcutRaw.ID,
&shortcutRaw.Title,
&shortcutRaw.Payload,
&shortcutRaw.CreatedTs,
&shortcutRaw.UpdatedTs,
&shortcutRaw.RowStatus,
2022-02-03 10:32:03 +03:00
); err != nil {
return nil, FormatError(err)
}
2022-05-19 13:32:04 +03:00
return &shortcutRaw, nil
2022-02-03 10:32:03 +03:00
}
2022-08-07 05:17:12 +03:00
func findShortcutList(ctx context.Context, tx *sql.Tx, find *api.ShortcutFind) ([]*shortcutRaw, error) {
2022-02-03 10:32:03 +03:00
where, args := []string{"1 = 1"}, []interface{}{}
2022-05-02 21:05:43 +03:00
if v := find.ID; v != nil {
2022-02-03 10:32:03 +03:00
where, args = append(where, "id = ?"), append(args, *v)
}
2022-05-02 21:05:43 +03:00
if v := find.CreatorID; v != nil {
2022-02-03 10:32:03 +03: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 05:17:12 +03:00
rows, err := tx.QueryContext(ctx, `
2022-02-03 10:32:03 +03:00
SELECT
id,
title,
payload,
creator_id,
created_ts,
updated_ts,
row_status
2022-02-03 10:32:03 +03:00
FROM shortcut
2022-05-22 07:35:57 +03:00
WHERE `+strings.Join(where, " AND ")+`
ORDER BY created_ts DESC`,
2022-02-03 10:32:03 +03:00
args...,
)
if err != nil {
return nil, FormatError(err)
}
defer rows.Close()
2022-05-19 13:32:04 +03:00
shortcutRawList := make([]*shortcutRaw, 0)
2022-02-03 10:32:03 +03:00
for rows.Next() {
2022-05-19 13:32:04 +03:00
var shortcutRaw shortcutRaw
2022-02-03 10:32:03 +03:00
if err := rows.Scan(
2022-05-19 13:32:04 +03:00
&shortcutRaw.ID,
&shortcutRaw.Title,
&shortcutRaw.Payload,
&shortcutRaw.CreatorID,
&shortcutRaw.CreatedTs,
&shortcutRaw.UpdatedTs,
&shortcutRaw.RowStatus,
2022-02-03 10:32:03 +03:00
); err != nil {
return nil, FormatError(err)
}
2022-05-19 13:32:04 +03:00
shortcutRawList = append(shortcutRawList, &shortcutRaw)
2022-02-03 10:32:03 +03:00
}
if err := rows.Err(); err != nil {
return nil, FormatError(err)
}
2022-05-19 13:32:04 +03:00
return shortcutRawList, nil
2022-02-03 10:32:03 +03:00
}
2022-08-07 05:17:12 +03:00
func deleteShortcut(ctx context.Context, tx *sql.Tx, delete *api.ShortcutDelete) error {
result, err := tx.ExecContext(ctx, `
2022-07-26 17:32:26 +03:00
DELETE FROM shortcut WHERE id = ?
`, delete.ID)
2022-02-03 10:32:03 +03:00
if err != nil {
return FormatError(err)
}
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 10:32:03 +03:00
return nil
}