Some checks failed
Deploy to NAS / deploy (push) Failing after 26s
- Public feed (/) with infinite scroll via Intersection Observer - Self-registration (/register) - Admin area (/admin/entries, /admin/users) with user management - journal_entries: visibility (public/private) + hashtags fields - users: is_admin flag - DB schema updated (recreate DB to apply) - CI: run go test via docker run (golang:1.25-alpine) — fixes 'go not found' Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
44 lines
997 B
Go
44 lines
997 B
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"github.com/jacek/pamietnik/backend/internal/domain"
|
|
)
|
|
|
|
type UserStore struct {
|
|
pool *pgxpool.Pool
|
|
}
|
|
|
|
func NewUserStore(pool *pgxpool.Pool) *UserStore {
|
|
return &UserStore{pool: pool}
|
|
}
|
|
|
|
// ListUsers returns all users ordered by created_at.
|
|
func (s *UserStore) ListUsers(ctx context.Context) ([]domain.User, error) {
|
|
rows, err := s.pool.Query(ctx,
|
|
`SELECT user_id, username, is_admin, created_at FROM users ORDER BY created_at`,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var users []domain.User
|
|
for rows.Next() {
|
|
var u domain.User
|
|
if err := rows.Scan(&u.UserID, &u.Username, &u.IsAdmin, &u.CreatedAt); err != nil {
|
|
return nil, err
|
|
}
|
|
users = append(users, u)
|
|
}
|
|
return users, rows.Err()
|
|
}
|
|
|
|
// DeleteUser removes a user by ID.
|
|
func (s *UserStore) DeleteUser(ctx context.Context, userID string) error {
|
|
_, err := s.pool.Exec(ctx, `DELETE FROM users WHERE user_id = $1`, userID)
|
|
return err
|
|
}
|