fix today memory after restart
This commit is contained in:
@@ -2,6 +2,8 @@ package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -65,6 +67,21 @@ func (s *Storage) GetShowCounts(ctx context.Context, memoNames []string) (map[st
|
||||
return result, rows.Err()
|
||||
}
|
||||
|
||||
// GetLastShownToday returns the memo_name and tier of the most recently shown memo
|
||||
// within the given time range [from, to). Returns empty string if nothing was shown.
|
||||
func (s *Storage) GetLastShownToday(ctx context.Context, from, to int64) (memoName string, tier int, err error) {
|
||||
err = s.db.QueryRowContext(ctx,
|
||||
`SELECT memo_name, tier FROM show_history WHERE shown_at >= ? AND shown_at < ? ORDER BY shown_at DESC LIMIT 1`,
|
||||
from, to).Scan(&memoName, &tier)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return "", 0, nil
|
||||
}
|
||||
return "", 0, fmt.Errorf("get last shown today: %w", err)
|
||||
}
|
||||
return memoName, tier, nil
|
||||
}
|
||||
|
||||
// RecordShow records that a memo was shown.
|
||||
func (s *Storage) RecordShow(ctx context.Context, memoName string, tier int) error {
|
||||
_, err := s.db.ExecContext(ctx,
|
||||
|
||||
Reference in New Issue
Block a user