feat: add Telegram bot with notifications and scheduler
This commit is contained in:
@@ -32,7 +32,7 @@ func RunMigrations(db *sqlx.DB) error {
|
||||
id SERIAL PRIMARY KEY,
|
||||
user_id INTEGER REFERENCES users(id) ON DELETE CASCADE,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
description TEXT DEFAULT '''',
|
||||
description TEXT DEFAULT '',
|
||||
color VARCHAR(20) DEFAULT '#6366f1',
|
||||
icon VARCHAR(50) DEFAULT 'check',
|
||||
frequency VARCHAR(20) DEFAULT 'daily',
|
||||
@@ -48,7 +48,7 @@ func RunMigrations(db *sqlx.DB) error {
|
||||
user_id INTEGER REFERENCES users(id) ON DELETE CASCADE,
|
||||
date DATE NOT NULL,
|
||||
count INTEGER DEFAULT 1,
|
||||
note TEXT DEFAULT '''',
|
||||
note TEXT DEFAULT '',
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
UNIQUE(habit_id, date)
|
||||
)`,
|
||||
@@ -65,7 +65,7 @@ func RunMigrations(db *sqlx.DB) error {
|
||||
id SERIAL PRIMARY KEY,
|
||||
user_id INTEGER REFERENCES users(id) ON DELETE CASCADE,
|
||||
title VARCHAR(255) NOT NULL,
|
||||
description TEXT DEFAULT '''',
|
||||
description TEXT DEFAULT '',
|
||||
icon VARCHAR(10) DEFAULT '📋',
|
||||
color VARCHAR(7) DEFAULT '#6B7280',
|
||||
due_date DATE,
|
||||
@@ -83,13 +83,18 @@ func RunMigrations(db *sqlx.DB) error {
|
||||
`CREATE INDEX IF NOT EXISTS idx_tasks_user_id ON tasks(user_id)`,
|
||||
`CREATE INDEX IF NOT EXISTS idx_tasks_due_date ON tasks(due_date)`,
|
||||
`CREATE INDEX IF NOT EXISTS idx_tasks_completed ON tasks(user_id, completed_at)`,
|
||||
// Migration: add email_verified column if not exists
|
||||
`DO $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='users' AND column_name='email_verified') THEN
|
||||
ALTER TABLE users ADD COLUMN email_verified BOOLEAN DEFAULT FALSE;
|
||||
END IF;
|
||||
END $$;`,
|
||||
`ALTER TABLE users ADD COLUMN IF NOT EXISTS telegram_chat_id BIGINT`,
|
||||
`ALTER TABLE users ADD COLUMN IF NOT EXISTS notifications_enabled BOOLEAN DEFAULT true`,
|
||||
`ALTER TABLE users ADD COLUMN IF NOT EXISTS timezone VARCHAR(50) DEFAULT 'Europe/Moscow'`,
|
||||
`ALTER TABLE tasks ADD COLUMN IF NOT EXISTS reminder_time TIME`,
|
||||
`ALTER TABLE habits ADD COLUMN IF NOT EXISTS reminder_time TIME`,
|
||||
`CREATE INDEX IF NOT EXISTS idx_users_telegram_chat_id ON users(telegram_chat_id)`,
|
||||
}
|
||||
|
||||
for _, migration := range migrations {
|
||||
|
||||
@@ -23,8 +23,8 @@ func NewHabitRepository(db *sqlx.DB) *HabitRepository {
|
||||
|
||||
func (r *HabitRepository) Create(habit *model.Habit) error {
|
||||
query := `
|
||||
INSERT INTO habits (user_id, name, description, color, icon, frequency, target_days, target_count)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||
INSERT INTO habits (user_id, name, description, color, icon, frequency, target_days, target_count, reminder_time)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
||||
RETURNING id, created_at, updated_at`
|
||||
|
||||
targetDays := pq.Array(habit.TargetDays)
|
||||
@@ -41,6 +41,7 @@ func (r *HabitRepository) Create(habit *model.Habit) error {
|
||||
habit.Frequency,
|
||||
targetDays,
|
||||
habit.TargetCount,
|
||||
habit.ReminderTime,
|
||||
).Scan(&habit.ID, &habit.CreatedAt, &habit.UpdatedAt)
|
||||
}
|
||||
|
||||
@@ -49,13 +50,13 @@ func (r *HabitRepository) GetByID(id, userID int64) (*model.Habit, error) {
|
||||
var targetDays pq.Int64Array
|
||||
|
||||
query := `
|
||||
SELECT id, user_id, name, description, color, icon, frequency, target_days, target_count, is_archived, created_at, updated_at
|
||||
SELECT id, user_id, name, description, color, icon, frequency, target_days, target_count, reminder_time, is_archived, created_at, updated_at
|
||||
FROM habits WHERE id = $1 AND user_id = $2`
|
||||
|
||||
err := r.db.QueryRow(query, id, userID).Scan(
|
||||
&habit.ID, &habit.UserID, &habit.Name, &habit.Description,
|
||||
&habit.Color, &habit.Icon, &habit.Frequency, &targetDays,
|
||||
&habit.TargetCount, &habit.IsArchived, &habit.CreatedAt, &habit.UpdatedAt,
|
||||
&habit.TargetCount, &habit.ReminderTime, &habit.IsArchived, &habit.CreatedAt, &habit.UpdatedAt,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
@@ -69,13 +70,14 @@ func (r *HabitRepository) GetByID(id, userID int64) (*model.Habit, error) {
|
||||
for i, v := range targetDays {
|
||||
habit.TargetDays[i] = int(v)
|
||||
}
|
||||
habit.ProcessForJSON()
|
||||
|
||||
return &habit, nil
|
||||
}
|
||||
|
||||
func (r *HabitRepository) ListByUser(userID int64, includeArchived bool) ([]model.Habit, error) {
|
||||
query := `
|
||||
SELECT id, user_id, name, description, color, icon, frequency, target_days, target_count, is_archived, created_at, updated_at
|
||||
SELECT id, user_id, name, description, color, icon, frequency, target_days, target_count, reminder_time, is_archived, created_at, updated_at
|
||||
FROM habits WHERE user_id = $1`
|
||||
|
||||
if !includeArchived {
|
||||
@@ -97,7 +99,7 @@ func (r *HabitRepository) ListByUser(userID int64, includeArchived bool) ([]mode
|
||||
if err := rows.Scan(
|
||||
&habit.ID, &habit.UserID, &habit.Name, &habit.Description,
|
||||
&habit.Color, &habit.Icon, &habit.Frequency, &targetDays,
|
||||
&habit.TargetCount, &habit.IsArchived, &habit.CreatedAt, &habit.UpdatedAt,
|
||||
&habit.TargetCount, &habit.ReminderTime, &habit.IsArchived, &habit.CreatedAt, &habit.UpdatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -106,6 +108,49 @@ func (r *HabitRepository) ListByUser(userID int64, includeArchived bool) ([]mode
|
||||
for i, v := range targetDays {
|
||||
habit.TargetDays[i] = int(v)
|
||||
}
|
||||
habit.ProcessForJSON()
|
||||
|
||||
habits = append(habits, habit)
|
||||
}
|
||||
|
||||
return habits, nil
|
||||
}
|
||||
|
||||
func (r *HabitRepository) GetHabitsWithReminder(reminderTime string, weekday int) ([]model.Habit, error) {
|
||||
query := `
|
||||
SELECT h.id, h.user_id, h.name, h.description, h.color, h.icon, h.frequency, h.target_days, h.target_count, h.reminder_time, h.is_archived, h.created_at, h.updated_at
|
||||
FROM habits h
|
||||
JOIN users u ON h.user_id = u.id
|
||||
WHERE h.reminder_time = $1
|
||||
AND h.is_archived = false
|
||||
AND (h.frequency = 'daily' OR $2 = ANY(h.target_days))
|
||||
AND u.telegram_chat_id IS NOT NULL
|
||||
AND u.notifications_enabled = true`
|
||||
|
||||
rows, err := r.db.Query(query, reminderTime, weekday)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var habits []model.Habit
|
||||
for rows.Next() {
|
||||
var habit model.Habit
|
||||
var targetDays pq.Int64Array
|
||||
|
||||
if err := rows.Scan(
|
||||
&habit.ID, &habit.UserID, &habit.Name, &habit.Description,
|
||||
&habit.Color, &habit.Icon, &habit.Frequency, &targetDays,
|
||||
&habit.TargetCount, &habit.ReminderTime, &habit.IsArchived, &habit.CreatedAt, &habit.UpdatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
habit.TargetDays = make([]int, len(targetDays))
|
||||
for i, v := range targetDays {
|
||||
habit.TargetDays[i] = int(v)
|
||||
}
|
||||
habit.ProcessForJSON()
|
||||
|
||||
habits = append(habits, habit)
|
||||
}
|
||||
@@ -117,8 +162,8 @@ func (r *HabitRepository) Update(habit *model.Habit) error {
|
||||
query := `
|
||||
UPDATE habits
|
||||
SET name = $2, description = $3, color = $4, icon = $5, frequency = $6,
|
||||
target_days = $7, target_count = $8, is_archived = $9, updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = $1 AND user_id = $10
|
||||
target_days = $7, target_count = $8, reminder_time = $9, is_archived = $10, updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = $1 AND user_id = $11
|
||||
RETURNING updated_at`
|
||||
|
||||
return r.db.QueryRow(query,
|
||||
@@ -130,6 +175,7 @@ func (r *HabitRepository) Update(habit *model.Habit) error {
|
||||
habit.Frequency,
|
||||
pq.Array(habit.TargetDays),
|
||||
habit.TargetCount,
|
||||
habit.ReminderTime,
|
||||
habit.IsArchived,
|
||||
habit.UserID,
|
||||
).Scan(&habit.UpdatedAt)
|
||||
@@ -208,6 +254,13 @@ func (r *HabitRepository) GetUserLogsForDate(userID int64, date time.Time) ([]mo
|
||||
return logs, nil
|
||||
}
|
||||
|
||||
func (r *HabitRepository) IsHabitCompletedToday(habitID, userID int64) (bool, error) {
|
||||
today := time.Now().Format("2006-01-02")
|
||||
var count int
|
||||
err := r.db.Get(&count, `SELECT COUNT(*) FROM habit_logs WHERE habit_id = $1 AND user_id = $2 AND date = $3`, habitID, userID, today)
|
||||
return count > 0, err
|
||||
}
|
||||
|
||||
func (r *HabitRepository) GetStats(habitID, userID int64) (*model.HabitStats, error) {
|
||||
stats := &model.HabitStats{HabitID: habitID}
|
||||
|
||||
|
||||
@@ -21,8 +21,8 @@ func NewTaskRepository(db *sqlx.DB) *TaskRepository {
|
||||
|
||||
func (r *TaskRepository) Create(task *model.Task) error {
|
||||
query := `
|
||||
INSERT INTO tasks (user_id, title, description, icon, color, due_date, priority)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
INSERT INTO tasks (user_id, title, description, icon, color, due_date, priority, reminder_time)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||
RETURNING id, created_at, updated_at`
|
||||
|
||||
return r.db.QueryRow(query,
|
||||
@@ -33,6 +33,7 @@ func (r *TaskRepository) Create(task *model.Task) error {
|
||||
task.Color,
|
||||
task.DueDate,
|
||||
task.Priority,
|
||||
task.ReminderTime,
|
||||
).Scan(&task.ID, &task.CreatedAt, &task.UpdatedAt)
|
||||
}
|
||||
|
||||
@@ -40,13 +41,13 @@ func (r *TaskRepository) GetByID(id, userID int64) (*model.Task, error) {
|
||||
var task model.Task
|
||||
|
||||
query := `
|
||||
SELECT id, user_id, title, description, icon, color, due_date, priority, completed_at, created_at, updated_at
|
||||
SELECT id, user_id, title, description, icon, color, due_date, priority, reminder_time, completed_at, created_at, updated_at
|
||||
FROM tasks WHERE id = $1 AND user_id = $2`
|
||||
|
||||
err := r.db.QueryRow(query, id, userID).Scan(
|
||||
&task.ID, &task.UserID, &task.Title, &task.Description,
|
||||
&task.Icon, &task.Color, &task.DueDate, &task.Priority,
|
||||
&task.CompletedAt, &task.CreatedAt, &task.UpdatedAt,
|
||||
&task.ReminderTime, &task.CompletedAt, &task.CreatedAt, &task.UpdatedAt,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
@@ -62,7 +63,7 @@ func (r *TaskRepository) GetByID(id, userID int64) (*model.Task, error) {
|
||||
|
||||
func (r *TaskRepository) ListByUser(userID int64, completed *bool) ([]model.Task, error) {
|
||||
query := `
|
||||
SELECT id, user_id, title, description, icon, color, due_date, priority, completed_at, created_at, updated_at
|
||||
SELECT id, user_id, title, description, icon, color, due_date, priority, reminder_time, completed_at, created_at, updated_at
|
||||
FROM tasks WHERE user_id = $1`
|
||||
|
||||
if completed != nil {
|
||||
@@ -87,7 +88,7 @@ func (r *TaskRepository) ListByUser(userID int64, completed *bool) ([]model.Task
|
||||
if err := rows.Scan(
|
||||
&task.ID, &task.UserID, &task.Title, &task.Description,
|
||||
&task.Icon, &task.Color, &task.DueDate, &task.Priority,
|
||||
&task.CompletedAt, &task.CreatedAt, &task.UpdatedAt,
|
||||
&task.ReminderTime, &task.CompletedAt, &task.CreatedAt, &task.UpdatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -103,7 +104,7 @@ func (r *TaskRepository) GetTodayTasks(userID int64) ([]model.Task, error) {
|
||||
today := time.Now().Format("2006-01-02")
|
||||
|
||||
query := `
|
||||
SELECT id, user_id, title, description, icon, color, due_date, priority, completed_at, created_at, updated_at
|
||||
SELECT id, user_id, title, description, icon, color, due_date, priority, reminder_time, completed_at, created_at, updated_at
|
||||
FROM tasks
|
||||
WHERE user_id = $1 AND completed_at IS NULL AND due_date <= $2
|
||||
ORDER BY priority DESC, due_date, created_at`
|
||||
@@ -121,7 +122,7 @@ func (r *TaskRepository) GetTodayTasks(userID int64) ([]model.Task, error) {
|
||||
if err := rows.Scan(
|
||||
&task.ID, &task.UserID, &task.Title, &task.Description,
|
||||
&task.Icon, &task.Color, &task.DueDate, &task.Priority,
|
||||
&task.CompletedAt, &task.CreatedAt, &task.UpdatedAt,
|
||||
&task.ReminderTime, &task.CompletedAt, &task.CreatedAt, &task.UpdatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -133,11 +134,45 @@ func (r *TaskRepository) GetTodayTasks(userID int64) ([]model.Task, error) {
|
||||
return tasks, nil
|
||||
}
|
||||
|
||||
func (r *TaskRepository) GetTasksWithReminder(reminderTime string, date string) ([]model.Task, error) {
|
||||
query := `
|
||||
SELECT t.id, t.user_id, t.title, t.description, t.icon, t.color, t.due_date, t.priority, t.reminder_time, t.completed_at, t.created_at, t.updated_at
|
||||
FROM tasks t
|
||||
JOIN users u ON t.user_id = u.id
|
||||
WHERE t.reminder_time = $1
|
||||
AND t.completed_at IS NULL
|
||||
AND (t.due_date IS NULL OR t.due_date >= $2)
|
||||
AND u.telegram_chat_id IS NOT NULL
|
||||
AND u.notifications_enabled = true`
|
||||
|
||||
rows, err := r.db.Query(query, reminderTime, date)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var tasks []model.Task
|
||||
for rows.Next() {
|
||||
var task model.Task
|
||||
if err := rows.Scan(
|
||||
&task.ID, &task.UserID, &task.Title, &task.Description,
|
||||
&task.Icon, &task.Color, &task.DueDate, &task.Priority,
|
||||
&task.ReminderTime, &task.CompletedAt, &task.CreatedAt, &task.UpdatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
task.ProcessForJSON()
|
||||
tasks = append(tasks, task)
|
||||
}
|
||||
|
||||
return tasks, nil
|
||||
}
|
||||
|
||||
func (r *TaskRepository) Update(task *model.Task) error {
|
||||
query := `
|
||||
UPDATE tasks
|
||||
SET title = $2, description = $3, icon = $4, color = $5, due_date = $6, priority = $7, updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = $1 AND user_id = $8
|
||||
SET title = $2, description = $3, icon = $4, color = $5, due_date = $6, priority = $7, reminder_time = $8, updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = $1 AND user_id = $9
|
||||
RETURNING updated_at`
|
||||
|
||||
return r.db.QueryRow(query,
|
||||
@@ -148,6 +183,7 @@ func (r *TaskRepository) Update(task *model.Task) error {
|
||||
task.Color,
|
||||
task.DueDate,
|
||||
task.Priority,
|
||||
task.ReminderTime,
|
||||
task.UserID,
|
||||
).Scan(&task.UpdatedAt)
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package repository
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/daniil/homelab-api/internal/model"
|
||||
"github.com/jmoiron/sqlx"
|
||||
@@ -40,9 +41,20 @@ func (r *UserRepository) Create(user *model.User) error {
|
||||
|
||||
func (r *UserRepository) GetByID(id int64) (*model.User, error) {
|
||||
var user model.User
|
||||
query := `SELECT id, email, username, password_hash, email_verified, created_at, updated_at FROM users WHERE id = $1`
|
||||
query := `SELECT id, email, username, password_hash, email_verified,
|
||||
telegram_chat_id,
|
||||
COALESCE(notifications_enabled, true) as notifications_enabled,
|
||||
COALESCE(timezone, $2) as timezone,
|
||||
created_at, updated_at
|
||||
FROM users WHERE id = $1`
|
||||
|
||||
if err := r.db.Get(&user, query, id); err != nil {
|
||||
row := r.db.QueryRow(query, id, "Europe/Moscow")
|
||||
err := row.Scan(
|
||||
&user.ID, &user.Email, &user.Username, &user.PasswordHash, &user.EmailVerified,
|
||||
&user.TelegramChatID, &user.NotificationsEnabled, &user.Timezone,
|
||||
&user.CreatedAt, &user.UpdatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, ErrUserNotFound
|
||||
}
|
||||
@@ -54,9 +66,20 @@ func (r *UserRepository) GetByID(id int64) (*model.User, error) {
|
||||
|
||||
func (r *UserRepository) GetByEmail(email string) (*model.User, error) {
|
||||
var user model.User
|
||||
query := `SELECT id, email, username, password_hash, email_verified, created_at, updated_at FROM users WHERE email = $1`
|
||||
query := `SELECT id, email, username, password_hash, email_verified,
|
||||
telegram_chat_id,
|
||||
COALESCE(notifications_enabled, true) as notifications_enabled,
|
||||
COALESCE(timezone, $2) as timezone,
|
||||
created_at, updated_at
|
||||
FROM users WHERE email = $1`
|
||||
|
||||
if err := r.db.Get(&user, query, email); err != nil {
|
||||
row := r.db.QueryRow(query, email, "Europe/Moscow")
|
||||
err := row.Scan(
|
||||
&user.ID, &user.Email, &user.Username, &user.PasswordHash, &user.EmailVerified,
|
||||
&user.TelegramChatID, &user.NotificationsEnabled, &user.Timezone,
|
||||
&user.CreatedAt, &user.UpdatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, ErrUserNotFound
|
||||
}
|
||||
@@ -66,6 +89,64 @@ func (r *UserRepository) GetByEmail(email string) (*model.User, error) {
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
func (r *UserRepository) GetByTelegramChatID(chatID int64) (*model.User, error) {
|
||||
var user model.User
|
||||
query := `SELECT id, email, username, password_hash, email_verified,
|
||||
telegram_chat_id,
|
||||
COALESCE(notifications_enabled, true) as notifications_enabled,
|
||||
COALESCE(timezone, $2) as timezone,
|
||||
created_at, updated_at
|
||||
FROM users WHERE telegram_chat_id = $1`
|
||||
|
||||
row := r.db.QueryRow(query, chatID, "Europe/Moscow")
|
||||
err := row.Scan(
|
||||
&user.ID, &user.Email, &user.Username, &user.PasswordHash, &user.EmailVerified,
|
||||
&user.TelegramChatID, &user.NotificationsEnabled, &user.Timezone,
|
||||
&user.CreatedAt, &user.UpdatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, ErrUserNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
func (r *UserRepository) GetUsersWithNotifications() ([]model.User, error) {
|
||||
query := `SELECT id, email, username, password_hash, email_verified,
|
||||
telegram_chat_id,
|
||||
COALESCE(notifications_enabled, true) as notifications_enabled,
|
||||
COALESCE(timezone, $1) as timezone,
|
||||
created_at, updated_at
|
||||
FROM users
|
||||
WHERE telegram_chat_id IS NOT NULL
|
||||
AND notifications_enabled = true`
|
||||
|
||||
rows, err := r.db.Query(query, "Europe/Moscow")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var users []model.User
|
||||
for rows.Next() {
|
||||
var user model.User
|
||||
err := rows.Scan(
|
||||
&user.ID, &user.Email, &user.Username, &user.PasswordHash, &user.EmailVerified,
|
||||
&user.TelegramChatID, &user.NotificationsEnabled, &user.Timezone,
|
||||
&user.CreatedAt, &user.UpdatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
users = append(users, user)
|
||||
}
|
||||
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func (r *UserRepository) Update(user *model.User) error {
|
||||
query := `
|
||||
UPDATE users
|
||||
@@ -76,6 +157,37 @@ func (r *UserRepository) Update(user *model.User) error {
|
||||
return r.db.QueryRow(query, user.ID, user.Username).Scan(&user.UpdatedAt)
|
||||
}
|
||||
|
||||
func (r *UserRepository) UpdateProfile(id int64, req *model.UpdateProfileRequest) error {
|
||||
query := `UPDATE users SET updated_at = CURRENT_TIMESTAMP`
|
||||
args := []interface{}{id}
|
||||
argIdx := 2
|
||||
|
||||
if req.Username != nil {
|
||||
query += fmt.Sprintf(", username = $%d", argIdx)
|
||||
args = append(args, *req.Username)
|
||||
argIdx++
|
||||
}
|
||||
if req.TelegramChatID != nil {
|
||||
query += fmt.Sprintf(", telegram_chat_id = $%d", argIdx)
|
||||
args = append(args, *req.TelegramChatID)
|
||||
argIdx++
|
||||
}
|
||||
if req.NotificationsEnabled != nil {
|
||||
query += fmt.Sprintf(", notifications_enabled = $%d", argIdx)
|
||||
args = append(args, *req.NotificationsEnabled)
|
||||
argIdx++
|
||||
}
|
||||
if req.Timezone != nil {
|
||||
query += fmt.Sprintf(", timezone = $%d", argIdx)
|
||||
args = append(args, *req.Timezone)
|
||||
argIdx++
|
||||
}
|
||||
|
||||
query += " WHERE id = $1"
|
||||
_, err := r.db.Exec(query, args...)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *UserRepository) UpdatePassword(id int64, passwordHash string) error {
|
||||
query := `UPDATE users SET password_hash = $2, updated_at = CURRENT_TIMESTAMP WHERE id = $1`
|
||||
_, err := r.db.Exec(query, id, passwordHash)
|
||||
|
||||
Reference in New Issue
Block a user