forked from mtusi2026/mtusi2026lab4
feat: lab4 - S3 attachments, image validation, failover
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
|
||||
"github.com/lib/pq"
|
||||
|
||||
"mtusi2026lab3/internal/domain"
|
||||
"mtusi2026lab3/internal/ident"
|
||||
)
|
||||
|
||||
// UserRecord is a user together with the password hash used only for login.
|
||||
type UserRecord struct {
|
||||
User domain.User
|
||||
PasswordHash string
|
||||
}
|
||||
|
||||
const userColumns = `id::text, username, display_name, created_at`
|
||||
|
||||
// CreateUser inserts a new user; a duplicate username maps to domain.ErrUsernameTaken.
|
||||
func (s *Store) CreateUser(ctx context.Context, username, displayName, passwordHash string) (domain.User, error) {
|
||||
user := domain.User{ID: ident.NewUUID(), Username: username, DisplayName: displayName}
|
||||
row := s.db.QueryRowContext(ctx,
|
||||
`INSERT INTO users (id, username, display_name, password_hash)
|
||||
VALUES ($1::uuid, $2, $3, $4)
|
||||
RETURNING `+userColumns,
|
||||
user.ID, user.Username, user.DisplayName, passwordHash)
|
||||
if err := scanUser(row, &user); err != nil {
|
||||
if isUniqueViolation(err) {
|
||||
return domain.User{}, domain.ErrUsernameTaken
|
||||
}
|
||||
return domain.User{}, err
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
// UserByUsername returns the credential record used by Basic authentication.
|
||||
func (s *Store) UserByUsername(ctx context.Context, username string) (UserRecord, error) {
|
||||
row := s.db.QueryRowContext(ctx,
|
||||
`SELECT `+userColumns+`, password_hash FROM users WHERE username = $1`, username)
|
||||
var record UserRecord
|
||||
if err := scanUser(row, &record.User); err != nil {
|
||||
return UserRecord{}, err
|
||||
}
|
||||
if err := row.Scan(&record.User.ID, &record.User.Username, &record.User.DisplayName,
|
||||
&record.User.CreatedAt.Time, &record.PasswordHash); err != nil {
|
||||
return UserRecord{}, err
|
||||
}
|
||||
return record, nil
|
||||
}
|
||||
|
||||
// UserByID resolves the owner of a valid session.
|
||||
func (s *Store) UserByID(ctx context.Context, id string) (domain.User, error) {
|
||||
var user domain.User
|
||||
row := s.db.QueryRowContext(ctx, `SELECT `+userColumns+` FROM users WHERE id = $1::uuid`, id)
|
||||
if err := scanUser(row, &user); err != nil {
|
||||
return domain.User{}, err
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func scanUser(row *sql.Row, user *domain.User) error {
|
||||
var created sql.NullTime
|
||||
if err := row.Scan(&user.ID, &user.Username, &user.DisplayName, &created); err != nil {
|
||||
return err
|
||||
}
|
||||
user.CreatedAt = domain.NewTimestamp(created.Time)
|
||||
return nil
|
||||
}
|
||||
|
||||
func isUniqueViolation(err error) bool {
|
||||
var pqErr *pq.Error
|
||||
return errors.As(err, &pqErr) && pqErr.Code == "23505"
|
||||
}
|
||||
Reference in New Issue
Block a user