Complete lab2 grade 4 implementation and report
Application checks / application (pull_request) Canceled after 0s
Skeleton contracts / contracts (pull_request) Canceled after 0s

This commit is contained in:
Daria0w0
2026-10-10 00:28:43 +03:00
parent 83e3810d33
commit c0ca56f208
24 changed files with 1107 additions and 137 deletions
+35 -1
View File
@@ -12,12 +12,46 @@ import (
var ErrUnavailable = errors.New("storage unavailable")
type PostgresStore struct{ pool *pgxpool.Pool }
type ProfileCache interface {
Get(context.Context, string) (model.User, bool)
Set(context.Context, model.User)
}
type PostgresStore struct {
pool *pgxpool.Pool
profiles ProfileCache
}
func NewPostgresStore(pool *pgxpool.Pool) *PostgresStore {
return &PostgresStore{pool: pool}
}
// Configure once before the HTTP server starts.
func (s *PostgresStore) WithProfiles(profiles ProfileCache) *PostgresStore {
s.profiles = profiles
return s
}
func (s *PostgresStore) profile(ctx context.Context, id string) (model.User, error) {
if s.profiles != nil {
if user, ok := s.profiles.Get(ctx, id); ok {
return user, nil
}
}
var user model.User
err := s.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 {
return model.User{}, ErrUnavailable
}
user.CreatedAt = user.CreatedAt.UTC()
if s.profiles != nil {
s.profiles.Set(ctx, user)
}
return user, nil
}
func (s *PostgresStore) CreateUser(ctx context.Context, username, displayName, passwordHash string) (model.User, error) {
id, err := newUUID()
if err != nil {