217 lines
5.9 KiB
Go
217 lines
5.9 KiB
Go
package repository
|
|
|
|
import (
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/daniil/homelab-api/internal/model"
|
|
"github.com/jmoiron/sqlx"
|
|
)
|
|
|
|
var ErrUserNotFound = errors.New("user not found")
|
|
var ErrUserExists = errors.New("user already exists")
|
|
|
|
type UserRepository struct {
|
|
db *sqlx.DB
|
|
}
|
|
|
|
func NewUserRepository(db *sqlx.DB) *UserRepository {
|
|
return &UserRepository{db: db}
|
|
}
|
|
|
|
func (r *UserRepository) Create(user *model.User) error {
|
|
query := `
|
|
INSERT INTO users (email, username, password_hash, email_verified)
|
|
VALUES ($1, $2, $3, $4)
|
|
RETURNING id, created_at, updated_at`
|
|
|
|
err := r.db.QueryRow(query, user.Email, user.Username, user.PasswordHash, user.EmailVerified).
|
|
Scan(&user.ID, &user.CreatedAt, &user.UpdatedAt)
|
|
|
|
if err != nil {
|
|
if isUniqueViolation(err) {
|
|
return ErrUserExists
|
|
}
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (r *UserRepository) GetByID(id 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 id = $1`
|
|
|
|
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
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
return &user, nil
|
|
}
|
|
|
|
func (r *UserRepository) GetByEmail(email string) (*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 email = $1`
|
|
|
|
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
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
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
|
|
SET username = $2, updated_at = CURRENT_TIMESTAMP
|
|
WHERE id = $1
|
|
RETURNING updated_at`
|
|
|
|
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++
|
|
}
|
|
if req.MorningReminderTime != nil {
|
|
query += fmt.Sprintf(", morning_reminder_time = $%d", argIdx)
|
|
args = append(args, *req.MorningReminderTime)
|
|
argIdx++
|
|
}
|
|
if req.EveningReminderTime != nil {
|
|
query += fmt.Sprintf(", evening_reminder_time = $%d", argIdx)
|
|
args = append(args, *req.EveningReminderTime)
|
|
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)
|
|
return err
|
|
}
|
|
|
|
func (r *UserRepository) SetEmailVerified(id int64) error {
|
|
query := `UPDATE users SET email_verified = TRUE, updated_at = CURRENT_TIMESTAMP WHERE id = $1`
|
|
_, err := r.db.Exec(query, id)
|
|
return err
|
|
}
|
|
|
|
func isUniqueViolation(err error) bool {
|
|
return err != nil && (err.Error() == "pq: duplicate key value violates unique constraint \"users_email_key\"" ||
|
|
err.Error() == "UNIQUE constraint failed: users.email")
|
|
}
|