memos/store/shortcut.go

269 lines
5.0 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
"strings"
)
type Shortcut struct {
2022-05-19 13:32:04 +03:00
ID int
// Standard fields
RowStatus RowStatus
2022-05-19 13:32:04 +03:00
CreatorID int
CreatedTs int64
UpdatedTs int64
// Domain specific fields
Title string
Payload string
}
type UpdateShortcut struct {
ID int
2022-05-19 13:32:04 +03:00
UpdatedTs *int64
RowStatus *RowStatus
Title *string
Payload *string
2022-05-19 13:32:04 +03:00
}
type FindShortcut struct {
ID *int
CreatorID *int
Title *string
}
2022-10-01 05:37:02 +03:00
type DeleteShortcut struct {
ID *int
CreatorID *int
2022-02-03 10:32:03 +03:00
}
func (s *Store) CreateShortcut(ctx context.Context, create *Shortcut) (*Shortcut, error) {
2022-08-07 05:17:12 +03:00
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, err
2022-08-07 05:17:12 +03:00
}
defer tx.Rollback()
query := `
INSERT INTO shortcut (
title,
payload,
creator_id
)
VALUES (?, ?, ?)
RETURNING id, created_ts, updated_ts, row_status
`
if err := tx.QueryRowContext(ctx, query, create.Title, create.Payload, create.CreatorID).Scan(
&create.ID,
&create.CreatedTs,
&create.UpdatedTs,
&create.RowStatus,
); err != nil {
2022-02-03 10:32:03 +03:00
return nil, err
}
2022-08-07 05:17:12 +03:00
if err := tx.Commit(); err != nil {
return nil, err
2022-08-07 05:17:12 +03:00
}
shortcut := create
2022-02-03 10:32:03 +03:00
return shortcut, nil
}
func (s *Store) ListShortcuts(ctx context.Context, find *FindShortcut) ([]*Shortcut, error) {
2022-08-07 05:17:12 +03:00
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, err
2022-08-07 05:17:12 +03:00
}
defer tx.Rollback()
list, err := listShortcuts(ctx, tx, find)
2022-02-03 10:32:03 +03:00
if err != nil {
return nil, err
}
if err := tx.Commit(); err != nil {
return nil, err
}
2022-02-03 10:32:03 +03:00
return list, nil
}
func (s *Store) GetShortcut(ctx context.Context, find *FindShortcut) (*Shortcut, error) {
2022-08-07 05:17:12 +03:00
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, err
2022-08-07 05:17:12 +03:00
}
defer tx.Rollback()
list, err := listShortcuts(ctx, tx, find)
2022-02-03 10:32:03 +03:00
if err != nil {
return nil, err
}
if len(list) == 0 {
return nil, nil
2022-02-03 10:32:03 +03:00
}
if err := tx.Commit(); err != nil {
return nil, err
}
shortcut := list[0]
2022-05-19 13:32:04 +03:00
return shortcut, nil
2022-02-03 10:32:03 +03:00
}
func (s *Store) UpdateShortcut(ctx context.Context, update *UpdateShortcut) (*Shortcut, error) {
2022-08-07 05:17:12 +03:00
tx, err := s.db.BeginTx(ctx, nil)
2022-02-03 10:32:03 +03:00
if err != nil {
return nil, err
2022-02-03 10:32:03 +03:00
}
2022-08-07 05:17:12 +03:00
defer tx.Rollback()
set, args := []string{}, []any{}
if v := update.UpdatedTs; v != nil {
set, args = append(set, "updated_ts = ?"), append(args, *v)
}
if v := update.Title; v != nil {
2022-02-03 10:32:03 +03:00
set, args = append(set, "title = ?"), append(args, *v)
}
if v := update.Payload; v != nil {
2022-02-03 10:32:03 +03:00
set, args = append(set, "payload = ?"), append(args, *v)
}
if v := update.RowStatus; v != nil {
set, args = append(set, "row_status = ?"), append(args, *v)
2022-02-03 10:32:03 +03:00
}
args = append(args, update.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, creator_id, created_ts, updated_ts, row_status
2022-08-07 05:17:12 +03:00
`
shortcut := &Shortcut{}
2022-08-07 05:17:12 +03:00
if err := tx.QueryRowContext(ctx, query, args...).Scan(
&shortcut.ID,
&shortcut.Title,
&shortcut.Payload,
&shortcut.CreatorID,
&shortcut.CreatedTs,
&shortcut.UpdatedTs,
&shortcut.RowStatus,
2022-02-03 10:32:03 +03:00
); err != nil {
return nil, err
}
if err := tx.Commit(); err != nil {
return nil, err
}
return shortcut, nil
}
func (s *Store) DeleteShortcut(ctx context.Context, delete *DeleteShortcut) error {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
where, args := []string{}, []any{}
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 ")
if _, err := tx.ExecContext(ctx, stmt, args...); err != nil {
return err
}
if err := tx.Commit(); err != nil {
return err
2022-02-03 10:32:03 +03:00
}
s.shortcutCache.Delete(*delete.ID)
return nil
2022-02-03 10:32:03 +03:00
}
func listShortcuts(ctx context.Context, tx *sql.Tx, find *FindShortcut) ([]*Shortcut, error) {
where, args := []string{"1 = 1"}, []any{}
2022-02-03 10:32:03 +03:00
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, err
2022-02-03 10:32:03 +03:00
}
defer rows.Close()
list := make([]*Shortcut, 0)
2022-02-03 10:32:03 +03:00
for rows.Next() {
var shortcut Shortcut
2022-02-03 10:32:03 +03:00
if err := rows.Scan(
&shortcut.ID,
&shortcut.Title,
&shortcut.Payload,
&shortcut.CreatorID,
&shortcut.CreatedTs,
&shortcut.UpdatedTs,
&shortcut.RowStatus,
2022-02-03 10:32:03 +03:00
); err != nil {
return nil, err
2022-02-03 10:32:03 +03:00
}
list = append(list, &shortcut)
2022-02-03 10:32:03 +03:00
}
if err := rows.Err(); err != nil {
return nil, err
2022-02-03 10:32:03 +03:00
}
return list, nil
2022-11-06 07:21:58 +03:00
}
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 err
}
2022-02-03 10:32:03 +03:00
return nil
}