user usecases & repository methods
This commit is contained in:
parent
7f6cd8e557
commit
39b89fc404
5 changed files with 125 additions and 0 deletions
|
|
@ -1 +1,47 @@
|
|||
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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue