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"
|
|
|
|
)
|
|
|
|
|
2023-07-02 13:56:25 +03:00
|
|
|
type Shortcut struct {
|
2022-05-19 13:32:04 +03:00
|
|
|
ID int
|
|
|
|
|
|
|
|
// Standard fields
|
2023-07-02 13:56:25 +03:00
|
|
|
RowStatus RowStatus
|
2022-05-19 13:32:04 +03:00
|
|
|
CreatorID int
|
|
|
|
CreatedTs int64
|
|
|
|
UpdatedTs int64
|
|
|
|
|
|
|
|
// Domain specific fields
|
|
|
|
Title string
|
|
|
|
Payload string
|
|
|
|
}
|
|
|
|
|
2023-07-02 13:56:25 +03:00
|
|
|
type UpdateShortcut struct {
|
|
|
|
ID int
|
2022-05-19 13:32:04 +03:00
|
|
|
|
2023-07-02 13:56:25 +03:00
|
|
|
UpdatedTs *int64
|
|
|
|
RowStatus *RowStatus
|
|
|
|
Title *string
|
|
|
|
Payload *string
|
2022-05-19 13:32:04 +03:00
|
|
|
}
|
|
|
|
|
2023-07-02 13:56:25 +03:00
|
|
|
type FindShortcut struct {
|
|
|
|
ID *int
|
|
|
|
CreatorID *int
|
|
|
|
Title *string
|
|
|
|
}
|
2022-10-01 05:37:02 +03:00
|
|
|
|
2023-07-02 13:56:25 +03:00
|
|
|
type DeleteShortcut struct {
|
|
|
|
ID *int
|
|
|
|
CreatorID *int
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
|
|
|
|
2023-07-02 13:56:25 +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 {
|
2023-07-02 13:56:25 +03:00
|
|
|
return nil, err
|
2022-08-07 05:17:12 +03:00
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2023-07-02 13:56:25 +03:00
|
|
|
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 {
|
2023-07-02 13:56:25 +03:00
|
|
|
return nil, err
|
2022-08-07 05:17:12 +03:00
|
|
|
}
|
|
|
|
|
2023-07-02 13:56:25 +03:00
|
|
|
shortcut := create
|
2022-02-03 10:32:03 +03:00
|
|
|
return shortcut, nil
|
|
|
|
}
|
|
|
|
|
2023-07-02 13:56:25 +03:00
|
|
|
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 {
|
2023-07-02 13:56:25 +03:00
|
|
|
return nil, err
|
2022-08-07 05:17:12 +03:00
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2023-07-02 13:56:25 +03:00
|
|
|
list, err := listShortcuts(ctx, tx, find)
|
2022-02-03 10:32:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-06 16:56:42 +03:00
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
2023-07-02 13:56:25 +03:00
|
|
|
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 {
|
2023-07-02 13:56:25 +03:00
|
|
|
return nil, err
|
2022-08-07 05:17:12 +03:00
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2023-07-02 13:56:25 +03:00
|
|
|
list, err := listShortcuts(ctx, tx, find)
|
2022-02-03 10:32:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(list) == 0 {
|
2023-07-02 13:56:25 +03:00
|
|
|
return nil, nil
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
|
|
|
|
2023-07-06 16:56:42 +03:00
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-02 13:56:25 +03:00
|
|
|
shortcut := list[0]
|
2022-05-19 13:32:04 +03:00
|
|
|
return shortcut, nil
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
|
|
|
|
2023-07-02 13:56:25 +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 {
|
2023-07-02 13:56:25 +03:00
|
|
|
return nil, err
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
2022-08-07 05:17:12 +03:00
|
|
|
defer tx.Rollback()
|
|
|
|
|
2023-03-18 17:34:22 +03:00
|
|
|
set, args := []string{}, []any{}
|
2023-07-02 13:56:25 +03:00
|
|
|
if v := update.UpdatedTs; v != nil {
|
2022-10-26 18:00:09 +03:00
|
|
|
set, args = append(set, "updated_ts = ?"), append(args, *v)
|
|
|
|
}
|
2023-07-02 13:56:25 +03:00
|
|
|
if v := update.Title; v != nil {
|
2022-02-03 10:32:03 +03:00
|
|
|
set, args = append(set, "title = ?"), append(args, *v)
|
|
|
|
}
|
2023-07-02 13:56:25 +03:00
|
|
|
if v := update.Payload; v != nil {
|
2022-02-03 10:32:03 +03:00
|
|
|
set, args = append(set, "payload = ?"), append(args, *v)
|
|
|
|
}
|
2023-07-02 13:56:25 +03:00
|
|
|
if v := update.RowStatus; v != nil {
|
2022-02-04 11:51:48 +03:00
|
|
|
set, args = append(set, "row_status = ?"), append(args, *v)
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
2023-07-02 13:56:25 +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 = ?
|
2023-01-07 07:45:55 +03:00
|
|
|
RETURNING id, title, payload, creator_id, created_ts, updated_ts, row_status
|
2022-08-07 05:17:12 +03:00
|
|
|
`
|
2023-07-02 13:56:25 +03:00
|
|
|
shortcut := &Shortcut{}
|
2022-08-07 05:17:12 +03:00
|
|
|
if err := tx.QueryRowContext(ctx, query, args...).Scan(
|
2023-07-02 13:56:25 +03:00
|
|
|
&shortcut.ID,
|
|
|
|
&shortcut.Title,
|
|
|
|
&shortcut.Payload,
|
|
|
|
&shortcut.CreatorID,
|
|
|
|
&shortcut.CreatedTs,
|
|
|
|
&shortcut.UpdatedTs,
|
|
|
|
&shortcut.RowStatus,
|
2022-02-03 10:32:03 +03:00
|
|
|
); err != nil {
|
2023-07-02 13:56:25 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-07-02 13:56:25 +03:00
|
|
|
s.shortcutCache.Delete(*delete.ID)
|
|
|
|
return nil
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
|
|
|
|
2023-07-02 13:56:25 +03:00
|
|
|
func listShortcuts(ctx context.Context, tx *sql.Tx, find *FindShortcut) ([]*Shortcut, error) {
|
2023-03-18 17:34:22 +03:00
|
|
|
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,
|
2022-02-04 11:51:48 +03:00
|
|
|
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 {
|
2023-07-04 05:05:57 +03:00
|
|
|
return nil, err
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2023-07-02 13:56:25 +03:00
|
|
|
list := make([]*Shortcut, 0)
|
2022-02-03 10:32:03 +03:00
|
|
|
for rows.Next() {
|
2023-07-02 13:56:25 +03:00
|
|
|
var shortcut Shortcut
|
2022-02-03 10:32:03 +03:00
|
|
|
if err := rows.Scan(
|
2023-07-02 13:56:25 +03:00
|
|
|
&shortcut.ID,
|
|
|
|
&shortcut.Title,
|
|
|
|
&shortcut.Payload,
|
|
|
|
&shortcut.CreatorID,
|
|
|
|
&shortcut.CreatedTs,
|
|
|
|
&shortcut.UpdatedTs,
|
|
|
|
&shortcut.RowStatus,
|
2022-02-03 10:32:03 +03:00
|
|
|
); err != nil {
|
2023-07-04 05:05:57 +03:00
|
|
|
return nil, err
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
2023-07-02 13:56:25 +03:00
|
|
|
list = append(list, &shortcut)
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := rows.Err(); err != nil {
|
2023-07-04 05:05:57 +03:00
|
|
|
return nil, err
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
|
|
|
|
2023-07-02 13:56:25 +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 {
|
2023-07-04 05:05:57 +03:00
|
|
|
return err
|
2022-09-03 13:54:22 +03:00
|
|
|
}
|
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
return nil
|
|
|
|
}
|