This commit is contained in:
parent
21e4c434fd
commit
72c6dd6982
12 changed files with 695 additions and 48 deletions
|
@ -5,6 +5,7 @@ import (
|
|||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
_ "modernc.org/sqlite"
|
||||
|
@ -591,6 +592,120 @@ func (d *Database) UpdateUserPassword(userID int64, newPassword string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
// CreateReminder creates a new reminder
|
||||
func (d *Database) CreateReminder(platform, channelID, messageID, replyToID, userID, username, content string, triggerAt time.Time) (*model.Reminder, error) {
|
||||
query := `
|
||||
INSERT INTO reminders (
|
||||
platform, channel_id, message_id, reply_to_id,
|
||||
user_id, username, created_at, trigger_at,
|
||||
content, processed
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 0)
|
||||
`
|
||||
|
||||
createdAt := time.Now()
|
||||
result, err := d.db.Exec(
|
||||
query,
|
||||
platform, channelID, messageID, replyToID,
|
||||
userID, username, createdAt, triggerAt,
|
||||
content,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
id, err := result.LastInsertId()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &model.Reminder{
|
||||
ID: id,
|
||||
Platform: platform,
|
||||
ChannelID: channelID,
|
||||
MessageID: messageID,
|
||||
ReplyToID: replyToID,
|
||||
UserID: userID,
|
||||
Username: username,
|
||||
CreatedAt: createdAt,
|
||||
TriggerAt: triggerAt,
|
||||
Content: content,
|
||||
Processed: false,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetPendingReminders gets all pending reminders that need to be processed
|
||||
func (d *Database) GetPendingReminders() ([]*model.Reminder, error) {
|
||||
query := `
|
||||
SELECT id, platform, channel_id, message_id, reply_to_id,
|
||||
user_id, username, created_at, trigger_at, content, processed
|
||||
FROM reminders
|
||||
WHERE processed = 0 AND trigger_at <= ?
|
||||
`
|
||||
|
||||
rows, err := d.db.Query(query, time.Now())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var reminders []*model.Reminder
|
||||
|
||||
for rows.Next() {
|
||||
var (
|
||||
id int64
|
||||
platform, channelID, messageID, replyToID string
|
||||
userID, username, content string
|
||||
createdAt, triggerAt time.Time
|
||||
processed bool
|
||||
)
|
||||
|
||||
if err := rows.Scan(
|
||||
&id, &platform, &channelID, &messageID, &replyToID,
|
||||
&userID, &username, &createdAt, &triggerAt, &content, &processed,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
reminder := &model.Reminder{
|
||||
ID: id,
|
||||
Platform: platform,
|
||||
ChannelID: channelID,
|
||||
MessageID: messageID,
|
||||
ReplyToID: replyToID,
|
||||
UserID: userID,
|
||||
Username: username,
|
||||
CreatedAt: createdAt,
|
||||
TriggerAt: triggerAt,
|
||||
Content: content,
|
||||
Processed: processed,
|
||||
}
|
||||
|
||||
reminders = append(reminders, reminder)
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(reminders) == 0 {
|
||||
return make([]*model.Reminder, 0), nil
|
||||
}
|
||||
|
||||
return reminders, nil
|
||||
}
|
||||
|
||||
// MarkReminderAsProcessed marks a reminder as processed
|
||||
func (d *Database) MarkReminderAsProcessed(id int64) error {
|
||||
query := `
|
||||
UPDATE reminders
|
||||
SET processed = 1
|
||||
WHERE id = ?
|
||||
`
|
||||
|
||||
_, err := d.db.Exec(query, id)
|
||||
return err
|
||||
}
|
||||
|
||||
// Helper function to hash password
|
||||
func hashPassword(password string) (string, error) {
|
||||
// Use bcrypt for secure password hashing
|
||||
|
@ -609,25 +724,25 @@ func initDatabase(db *sql.DB) error {
|
|||
if err := migration.EnsureMigrationTable(db); err != nil {
|
||||
return fmt.Errorf("failed to create migration table: %w", err)
|
||||
}
|
||||
|
||||
|
||||
// Get applied migrations
|
||||
applied, err := migration.GetAppliedMigrations(db)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get applied migrations: %w", err)
|
||||
}
|
||||
|
||||
|
||||
// Get all migration versions
|
||||
allMigrations := make([]int, 0, len(migration.Migrations))
|
||||
for version := range migration.Migrations {
|
||||
allMigrations = append(allMigrations, version)
|
||||
}
|
||||
|
||||
|
||||
// Create a map of applied migrations for quick lookup
|
||||
appliedMap := make(map[int]bool)
|
||||
for _, version := range applied {
|
||||
appliedMap[version] = true
|
||||
}
|
||||
|
||||
|
||||
// Count pending migrations
|
||||
pendingCount := 0
|
||||
for _, version := range allMigrations {
|
||||
|
@ -635,7 +750,7 @@ func initDatabase(db *sql.DB) error {
|
|||
pendingCount++
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Run migrations if needed
|
||||
if pendingCount > 0 {
|
||||
fmt.Printf("Running %d pending database migrations...\n", pendingCount)
|
||||
|
@ -646,6 +761,6 @@ func initDatabase(db *sql.DB) error {
|
|||
} else {
|
||||
fmt.Println("Database schema is up to date.")
|
||||
}
|
||||
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue