user usecases & repository methods
This commit is contained in:
parent
7f6cd8e557
commit
39b89fc404
5 changed files with 125 additions and 0 deletions
9
internal/entities/user.go
Normal file
9
internal/entities/user.go
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
package entities
|
||||||
|
|
||||||
|
type UserID string
|
||||||
|
type TelegramID int64
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
ID UserID
|
||||||
|
TelegramID TelegramID
|
||||||
|
}
|
||||||
|
|
@ -1 +1,47 @@
|
||||||
package postgresql
|
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
|
||||||
|
}
|
||||||
|
|
|
||||||
13
internal/repository/repository.go
Normal file
13
internal/repository/repository.go
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
package repository
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"gitea.computernetthings.ru/yash/crypto_alert_bot/internal/entities"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Storage interface {
|
||||||
|
SaveUser(ctx context.Context, user *entities.User) (entities.UserID, error)
|
||||||
|
UserByID(ctx context.Context, id entities.UserID) (*entities.User, error)
|
||||||
|
UserByTelegramID(ctx context.Context, tgID entities.TelegramID) (*entities.User, error)
|
||||||
|
}
|
||||||
19
internal/usecase/usecase.go
Normal file
19
internal/usecase/usecase.go
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
package usecase
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log/slog"
|
||||||
|
|
||||||
|
"gitea.computernetthings.ru/yash/crypto_alert_bot/internal/repository"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Usecase struct {
|
||||||
|
log *slog.Logger
|
||||||
|
storage repository.Storage
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(log *slog.Logger, storage repository.Storage) *Usecase {
|
||||||
|
return &Usecase{
|
||||||
|
log: log,
|
||||||
|
storage: storage,
|
||||||
|
}
|
||||||
|
}
|
||||||
38
internal/usecase/user.go
Normal file
38
internal/usecase/user.go
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
package usecase
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"gitea.computernetthings.ru/yash/crypto_alert_bot/internal/entities"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (uc *Usecase) RegisterNewUser(ctx context.Context, user *entities.User) error {
|
||||||
|
_, err := uc.storage.SaveUser(ctx, user)
|
||||||
|
if err != nil {
|
||||||
|
uc.log.Error("failed to register new user", "user", user, "err", err)
|
||||||
|
return fmt.Errorf("failed to register new user: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (uc *Usecase) UserByID(ctx context.Context, userID entities.UserID) (*entities.User, error) {
|
||||||
|
user, err := uc.storage.UserByID(ctx, userID)
|
||||||
|
if err != nil {
|
||||||
|
uc.log.Error("failed to get user by id", "user_id", userID, "err", err)
|
||||||
|
return nil, fmt.Errorf("failed to get user by id: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return user, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (uc *Usecase) UserByTgID(ctx context.Context, telegramID entities.TelegramID) (*entities.User, error) {
|
||||||
|
user, err := uc.storage.UserByTelegramID(ctx, telegramID)
|
||||||
|
if err != nil {
|
||||||
|
uc.log.Error("failed to get user by telegram id", "tg_id", telegramID, "err", err)
|
||||||
|
return nil, fmt.Errorf("failed to get user by telegram id: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return user, nil
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue