9d845c8899
- backend moved to backend directory - added and initialized frontend with vue - moved infrastructure files to infra directory
159 lines
3.0 KiB
Go
159 lines
3.0 KiB
Go
package repositories
|
|
|
|
import (
|
|
"FamilyHub/src/domain"
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
)
|
|
|
|
type UsersRepository interface {
|
|
Create(ctx context.Context, user *domain.UserModel) error
|
|
GetByID(ctx context.Context, id int64) (*domain.UserModel, error)
|
|
GetByTelegramID(ctx context.Context, telegramID int64) (*domain.UserModel, error)
|
|
Update(ctx context.Context, user *domain.UserModel) error
|
|
Delete(ctx context.Context, id int64) error
|
|
}
|
|
|
|
type UsersSQLRepository struct {
|
|
db *sql.DB
|
|
}
|
|
|
|
func NewUsersSQLRepository(db *sql.DB) *UsersSQLRepository {
|
|
return &UsersSQLRepository{db: db}
|
|
}
|
|
|
|
func (r *UsersSQLRepository) Create(ctx context.Context, user *domain.UserModel) error {
|
|
query := `
|
|
INSERT INTO users
|
|
(telegram_id, username, first_name, last_name, language_code)
|
|
VALUES ($1, $2, $3, $4, $5)
|
|
RETURNING id, created_at, updated_at
|
|
`
|
|
|
|
return r.db.QueryRowContext(
|
|
ctx,
|
|
query,
|
|
user.TelegramID,
|
|
user.Username,
|
|
user.FirstName,
|
|
user.LastName,
|
|
user.LanguageCode,
|
|
).Scan(&user.ID, &user.CreatedAt, &user.UpdatedAt)
|
|
}
|
|
func (r *UsersSQLRepository) GetByID(ctx context.Context, id int64) (*domain.UserModel, error) {
|
|
query := `
|
|
SELECT
|
|
id,
|
|
telegram_id,
|
|
username,
|
|
first_name,
|
|
last_name,
|
|
language_code,
|
|
created_at,
|
|
updated_at
|
|
FROM users
|
|
WHERE id = $1
|
|
`
|
|
|
|
var user domain.UserModel
|
|
|
|
err := r.db.QueryRowContext(ctx, query, id).Scan(
|
|
&user.ID,
|
|
&user.TelegramID,
|
|
&user.Username,
|
|
&user.FirstName,
|
|
&user.LastName,
|
|
&user.LanguageCode,
|
|
&user.CreatedAt,
|
|
&user.UpdatedAt,
|
|
)
|
|
|
|
if err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return nil, nil // или кастомную ErrNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
return &user, nil
|
|
}
|
|
func (r *UsersSQLRepository) GetByTelegramID(ctx context.Context, telegramID int64) (*domain.UserModel, error) {
|
|
query := `
|
|
SELECT
|
|
id,
|
|
telegram_id,
|
|
username,
|
|
first_name,
|
|
last_name,
|
|
language_code,
|
|
created_at,
|
|
updated_at
|
|
FROM users
|
|
WHERE telegram_id = $1
|
|
`
|
|
|
|
var user domain.UserModel
|
|
|
|
err := r.db.QueryRowContext(ctx, query, telegramID).Scan(
|
|
&user.ID,
|
|
&user.TelegramID,
|
|
&user.Username,
|
|
&user.FirstName,
|
|
&user.LastName,
|
|
&user.LanguageCode,
|
|
&user.CreatedAt,
|
|
&user.UpdatedAt,
|
|
)
|
|
|
|
if err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return nil, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
return &user, nil
|
|
}
|
|
func (r *UsersSQLRepository) Update(ctx context.Context, user *domain.UserModel) error {
|
|
query := `
|
|
UPDATE users SET
|
|
username = $1,
|
|
first_name = $2,
|
|
last_name = $3,
|
|
language_code = $4,
|
|
updated_at = now()
|
|
WHERE id = $5
|
|
RETURNING updated_at
|
|
`
|
|
|
|
return r.db.QueryRowContext(
|
|
ctx,
|
|
query,
|
|
user.Username,
|
|
user.FirstName,
|
|
user.LastName,
|
|
user.LanguageCode,
|
|
user.ID,
|
|
).Scan(&user.UpdatedAt)
|
|
}
|
|
func (r *UsersSQLRepository) Delete(ctx context.Context, id int64) error {
|
|
query := `DELETE FROM users WHERE id = $1`
|
|
|
|
result, err := r.db.ExecContext(ctx, query, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
rows, err := result.RowsAffected()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if rows == 0 {
|
|
return sql.ErrNoRows
|
|
}
|
|
|
|
return nil
|
|
}
|