memos/store/shortcut.go

252 lines
5.2 KiB
Go
Raw Normal View History

2022-02-03 10:32:03 +03:00
package store
import (
2022-05-21 19:59:22 +03:00
"database/sql"
2022-02-03 10:32:03 +03:00
"fmt"
"memos/api"
"memos/common"
"strings"
)
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-05-16 02:37:23 +03:00
func (s *Store) CreateShortcut(create *api.ShortcutCreate) (*api.Shortcut, error) {
2022-05-19 13:32:04 +03:00
shortcutRaw, err := createShortcut(s.db, create)
2022-02-03 10:32:03 +03:00
if err != nil {
return nil, err
}
2022-05-19 13:32:04 +03:00
shortcut := shortcutRaw.toShortcut()
2022-02-03 10:32:03 +03:00
return shortcut, nil
}
2022-05-16 02:37:23 +03:00
func (s *Store) PatchShortcut(patch *api.ShortcutPatch) (*api.Shortcut, error) {
2022-05-19 13:32:04 +03:00
shortcutRaw, err := patchShortcut(s.db, patch)
2022-02-03 10:32:03 +03:00
if err != nil {
return nil, err
}
2022-05-19 13:32:04 +03:00
shortcut := shortcutRaw.toShortcut()
2022-02-03 10:32:03 +03:00
return shortcut, nil
}
2022-05-16 02:37:23 +03:00
func (s *Store) FindShortcutList(find *api.ShortcutFind) ([]*api.Shortcut, error) {
2022-05-19 13:32:04 +03:00
shortcutRawList, err := findShortcutList(s.db, 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-05-16 02:37:23 +03:00
func (s *Store) FindShortcut(find *api.ShortcutFind) (*api.Shortcut, error) {
2022-02-03 10:32:03 +03:00
list, err := findShortcutList(s.db, find)
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()
return shortcut, nil
2022-02-03 10:32:03 +03:00
}
2022-05-16 02:37:23 +03:00
func (s *Store) DeleteShortcut(delete *api.ShortcutDelete) error {
2022-02-03 10:32:03 +03:00
err := deleteShortcut(s.db, delete)
if err != nil {
return FormatError(err)
}
return nil
}
2022-05-21 19:59:22 +03:00
func createShortcut(db *sql.DB, create *api.ShortcutCreate) (*shortcutRaw, error) {
row, err := db.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-02-03 10:32:03 +03:00
`,
create.Title,
create.Payload,
2022-05-02 21:05:43 +03:00
create.CreatorID,
2022-02-03 10:32:03 +03:00
)
if err != nil {
return nil, FormatError(err)
}
defer row.Close()
row.Next()
2022-05-19 13:32:04 +03:00
var shortcutRaw shortcutRaw
2022-02-03 10:32:03 +03:00
if err := row.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-05-21 19:59:22 +03:00
func patchShortcut(db *sql.DB, 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-05-21 19:59:22 +03:00
row, err := db.Query(`
2022-02-03 10:32:03 +03:00
UPDATE shortcut
SET `+strings.Join(set, ", ")+`
WHERE id = ?
RETURNING id, title, payload, created_ts, updated_ts, row_status
2022-02-03 10:32:03 +03: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 13:32:04 +03:00
var shortcutRaw shortcutRaw
2022-02-03 10:32:03 +03:00
if err := row.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-05-21 19:59:22 +03:00
func findShortcutList(db *sql.DB, 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-05-21 19:59:22 +03:00
rows, err := db.Query(`
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-05-21 19:59:22 +03:00
func deleteShortcut(db *sql.DB, delete *api.ShortcutDelete) error {
result, err := db.Exec(`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 {
2022-05-02 21:05:43 +03:00
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
}