feat(17-02): add gocron dependency, subscribers and scheduled_jobs tables with CRUD

- Add gocron/v2 v2.19.1 as direct dependency
- Append subscribers and scheduled_jobs CREATE TABLE to schema.sql
- Implement full subscriber CRUD (Add/Remove/List/IsSubscribed)
- Implement full scheduled job CRUD (Save/List/Get/Delete/UpdateLastRun/SetEnabled)
This commit is contained in:
salvacybersec
2026-04-06 17:25:43 +03:00
parent 0e87618e32
commit c8f7592b73
5 changed files with 194 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
package storage
import "time"
// Subscriber represents a Telegram chat subscribed to scan notifications.
type Subscriber struct {
ChatID int64
Username string
SubscribedAt time.Time
}
// AddSubscriber inserts or replaces a subscriber in the database.
func (db *DB) AddSubscriber(chatID int64, username string) error {
_, err := db.sql.Exec(
`INSERT OR REPLACE INTO subscribers (chat_id, username, subscribed_at) VALUES (?, ?, CURRENT_TIMESTAMP)`,
chatID, username,
)
return err
}
// RemoveSubscriber deletes a subscriber by chat ID. Returns rows affected.
func (db *DB) RemoveSubscriber(chatID int64) (int64, error) {
res, err := db.sql.Exec(`DELETE FROM subscribers WHERE chat_id = ?`, chatID)
if err != nil {
return 0, err
}
return res.RowsAffected()
}
// ListSubscribers returns all subscribers ordered by subscription time.
func (db *DB) ListSubscribers() ([]Subscriber, error) {
rows, err := db.sql.Query(`SELECT chat_id, username, subscribed_at FROM subscribers ORDER BY subscribed_at`)
if err != nil {
return nil, err
}
defer rows.Close()
var subs []Subscriber
for rows.Next() {
var s Subscriber
if err := rows.Scan(&s.ChatID, &s.Username, &s.SubscribedAt); err != nil {
return nil, err
}
subs = append(subs, s)
}
return subs, rows.Err()
}
// IsSubscribed returns true if the given chat ID is subscribed.
func (db *DB) IsSubscribed(chatID int64) (bool, error) {
var count int
err := db.sql.QueryRow(`SELECT COUNT(*) FROM subscribers WHERE chat_id = ?`, chatID).Scan(&count)
return count > 0, err
}