forked from mtusi2026/mtusi2026lab6
ЛР 6 — БПИ2302 — Нурлыгаянова — на 3
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/domain"
|
||||
)
|
||||
|
||||
type Users struct {
|
||||
conn
|
||||
}
|
||||
|
||||
func (u *Users) CreateUser(ctx context.Context, username, displayName, passwordHash string) (domain.User, error) {
|
||||
if strings.TrimSpace(username) == "" || strings.TrimSpace(displayName) == "" || passwordHash == "" {
|
||||
return domain.User{}, domain.ErrInvalidArgument
|
||||
}
|
||||
|
||||
ctx, cancel := u.withTimeout(ctx)
|
||||
defer cancel()
|
||||
|
||||
user := domain.User{Username: username, DisplayName: displayName}
|
||||
err := u.pool.QueryRow(ctx, `
|
||||
INSERT INTO users (username, display_name, password_hash)
|
||||
VALUES ($1, $2, $3)
|
||||
RETURNING id::text, created_at`,
|
||||
username, encodeText(displayName), passwordHash,
|
||||
).Scan(&user.ID, &user.CreatedAt)
|
||||
if err != nil {
|
||||
return domain.User{}, mapErr(err)
|
||||
}
|
||||
user.CreatedAt = user.CreatedAt.UTC()
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (u *Users) UserByID(ctx context.Context, id string) (domain.User, error) {
|
||||
ctx, cancel := u.withTimeout(ctx)
|
||||
defer cancel()
|
||||
|
||||
user := domain.User{}
|
||||
err := u.pool.QueryRow(ctx, `
|
||||
SELECT id::text, username, display_name, created_at
|
||||
FROM users
|
||||
WHERE id = $1`,
|
||||
id,
|
||||
).Scan(&user.ID, &user.Username, &user.DisplayName, &user.CreatedAt)
|
||||
if err != nil {
|
||||
err = mapErr(err)
|
||||
if err == domain.ErrInvalidArgument {
|
||||
return domain.User{}, domain.ErrNotFound
|
||||
}
|
||||
return domain.User{}, err
|
||||
}
|
||||
user.DisplayName = decodeText(user.DisplayName)
|
||||
user.CreatedAt = user.CreatedAt.UTC()
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (u *Users) CredentialsByUsername(ctx context.Context, username string) (domain.User, string, error) {
|
||||
ctx, cancel := u.withTimeout(ctx)
|
||||
defer cancel()
|
||||
|
||||
user := domain.User{}
|
||||
var hash string
|
||||
err := u.pool.QueryRow(ctx, `
|
||||
SELECT id::text, username, display_name, created_at, password_hash
|
||||
FROM users
|
||||
WHERE username = $1`,
|
||||
encodeText(username),
|
||||
).Scan(&user.ID, &user.Username, &user.DisplayName, &user.CreatedAt, &hash)
|
||||
if err != nil {
|
||||
return domain.User{}, "", mapErr(err)
|
||||
}
|
||||
user.DisplayName = decodeText(user.DisplayName)
|
||||
user.CreatedAt = user.CreatedAt.UTC()
|
||||
return user, hash, nil
|
||||
}
|
||||
|
||||
func (u *Users) UsernameExists(ctx context.Context, username string) bool {
|
||||
ctx, cancel := u.withTimeout(ctx)
|
||||
defer cancel()
|
||||
|
||||
var exists bool
|
||||
err := u.pool.QueryRow(ctx,
|
||||
`SELECT EXISTS (SELECT 1 FROM users WHERE username = $1)`,
|
||||
encodeText(username),
|
||||
).Scan(&exists)
|
||||
return err == nil && exists
|
||||
}
|
||||
Reference in New Issue
Block a user