forked from mtusi2026/mtusi2026lab4
feat: lab4 - S3 attachments, image validation, failover
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
// Package store is the only layer that talks to PostgreSQL.
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"time"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
const (
|
||||
maxOpenConns = 20
|
||||
maxIdleConns = 10
|
||||
connMaxLifetime = 5 * time.Minute
|
||||
pingTimeout = 2 * time.Second
|
||||
)
|
||||
|
||||
// Store wraps the shared PostgreSQL connection pool.
|
||||
type Store struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
// Open creates the pool; it does not require the database to be reachable yet.
|
||||
func Open(databaseURL string) (*Store, error) {
|
||||
db, err := sql.Open("postgres", databaseURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
db.SetMaxOpenConns(maxOpenConns)
|
||||
db.SetMaxIdleConns(maxIdleConns)
|
||||
db.SetConnMaxLifetime(connMaxLifetime)
|
||||
return &Store{db: db}, nil
|
||||
}
|
||||
|
||||
// Close releases the pool.
|
||||
func (s *Store) Close() error {
|
||||
return s.db.Close()
|
||||
}
|
||||
|
||||
// Ping reports whether the database answers within a short budget; readiness uses it.
|
||||
func (s *Store) Ping(ctx context.Context) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, pingTimeout)
|
||||
defer cancel()
|
||||
return s.db.PingContext(ctx)
|
||||
}
|
||||
|
||||
// scanTime normalises timestamps returned by the driver.
|
||||
func utc(t time.Time) time.Time {
|
||||
return t.UTC()
|
||||
}
|
||||
@@ -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