47 lines
1.3 KiB
Go
47 lines
1.3 KiB
Go
package postgresql
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"gitea.computernetthings.ru/yash/crypto_alert_bot/internal/entities"
|
|
)
|
|
|
|
const saveUserQuery = "insert into users(telegram_id) values ($1) returning id"
|
|
|
|
func (p *Postgresql) SaveUser(ctx context.Context, user *entities.User) (entities.UserID, error) {
|
|
var id entities.UserID
|
|
|
|
err := p.db.QueryRow(ctx, saveUserQuery, user.TelegramID).Scan(&id)
|
|
if err != nil {
|
|
return "", fmt.Errorf("failed to exec saveUserQuery: %w", err)
|
|
}
|
|
|
|
return id, nil
|
|
}
|
|
|
|
const getUserByIDQuery = "select id, telegram_id from users where id = $1"
|
|
|
|
func (p *Postgresql) UserByID(ctx context.Context, id entities.UserID) (*entities.User, error) {
|
|
var user entities.User
|
|
|
|
err := p.db.QueryRow(ctx, getUserByIDQuery, id).Scan(&user.ID, &user.TelegramID)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to exec getUserByIDQuery: %w", err)
|
|
}
|
|
|
|
return &user, nil
|
|
}
|
|
|
|
const getUserByTelegramIDQuery = "select id, telegram_id from users where telegram_id = $1"
|
|
|
|
func (p *Postgresql) UserByTelegramID(ctx context.Context, tgID entities.TelegramID) (*entities.User, error) {
|
|
var user entities.User
|
|
|
|
err := p.db.QueryRow(ctx, getUserByTelegramIDQuery, tgID).Scan(&user.ID, &user.TelegramID)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to exec getUserByTelegramIDQuery: %w", err)
|
|
}
|
|
|
|
return &user, nil
|
|
}
|