feat: add Telegram bot with notifications and scheduler
This commit is contained in:
@@ -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