Reapply "Adminbereich mit Rollen und Nutzerauswertung ergänzen"
This reverts commit 2447537652.
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"strings"
|
||||
|
||||
"speech-learning-api/internal/domain"
|
||||
)
|
||||
|
||||
func (s *PostgresStore) AdminOverview(ctx context.Context) (domain.AdminOverview, error) {
|
||||
var overview domain.AdminOverview
|
||||
err := s.db.QueryRowContext(ctx, `
|
||||
SELECT
|
||||
(SELECT COUNT(*) FROM users),
|
||||
(SELECT COUNT(DISTINCT user_id) FROM attempts WHERE created_at >= now() - interval '7 days'),
|
||||
(SELECT COUNT(*) FROM attempts),
|
||||
(SELECT COUNT(*) FROM exercises WHERE source = 'flowise'),
|
||||
(SELECT COALESCE(AVG(overall_score), 0) FROM attempts)
|
||||
`).Scan(&overview.UsersTotal, &overview.UsersActive7Days, &overview.AttemptsTotal,
|
||||
&overview.ExercisesGenerated, &overview.AverageScore)
|
||||
return overview, err
|
||||
}
|
||||
|
||||
func (s *PostgresStore) AdminUsers(ctx context.Context, search string, page int) (domain.AdminUserPage, error) {
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
search = strings.TrimSpace(search)
|
||||
result := domain.AdminUserPage{Users: []domain.AdminUser{}, Page: page}
|
||||
if err := s.db.QueryRowContext(ctx, `
|
||||
SELECT COUNT(*) FROM users WHERE ($1 = '' OR email ILIKE '%' || $1 || '%')
|
||||
`, search).Scan(&result.Total); err != nil {
|
||||
return domain.AdminUserPage{}, err
|
||||
}
|
||||
rows, err := s.db.QueryContext(ctx, `
|
||||
WITH selected AS (
|
||||
SELECT id, email, role, created_at
|
||||
FROM users
|
||||
WHERE ($1 = '' OR email ILIKE '%' || $1 || '%')
|
||||
ORDER BY created_at DESC, id DESC
|
||||
LIMIT 25 OFFSET $2
|
||||
)
|
||||
SELECT u.id, u.email, u.role, u.created_at,
|
||||
a.attempts, a.average_score, a.last_attempt
|
||||
FROM selected u
|
||||
LEFT JOIN LATERAL (
|
||||
SELECT COUNT(*) AS attempts, COALESCE(AVG(overall_score), 0) AS average_score,
|
||||
MAX(created_at) AS last_attempt
|
||||
FROM attempts WHERE user_id = u.id
|
||||
) a ON true
|
||||
ORDER BY u.created_at DESC, u.id DESC
|
||||
`, search, (page-1)*25)
|
||||
if err != nil {
|
||||
return domain.AdminUserPage{}, err
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var user domain.AdminUser
|
||||
var last sql.NullTime
|
||||
if err := rows.Scan(&user.ID, &user.Email, &user.Role, &user.CreatedAt,
|
||||
&user.Attempts, &user.AverageScore, &last); err != nil {
|
||||
return domain.AdminUserPage{}, err
|
||||
}
|
||||
if last.Valid {
|
||||
user.LastAttempt = &last.Time
|
||||
}
|
||||
result.Users = append(result.Users, user)
|
||||
}
|
||||
return result, rows.Err()
|
||||
}
|
||||
@@ -142,9 +142,9 @@ func (s *PostgresStore) CreateUser(ctx context.Context, email, passwordHash, uiL
|
||||
row := s.db.QueryRowContext(ctx, `
|
||||
INSERT INTO users (id, email, password_hash, ui_language)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING id, email, ui_language, created_at
|
||||
RETURNING id, email, ui_language, role, created_at
|
||||
`, user.ID, user.Email, passwordHash, uiLanguage)
|
||||
if err := row.Scan(&user.ID, &user.Email, &user.UILanguage, &user.CreatedAt); err != nil {
|
||||
if err := row.Scan(&user.ID, &user.Email, &user.UILanguage, &user.Role, &user.CreatedAt); err != nil {
|
||||
return domain.User{}, err
|
||||
}
|
||||
return user, nil
|
||||
@@ -154,11 +154,11 @@ func (s *PostgresStore) GetPasswordHashByEmail(ctx context.Context, email string
|
||||
var user domain.User
|
||||
var hash string
|
||||
row := s.db.QueryRowContext(ctx, `
|
||||
SELECT id, email, password_hash, ui_language, created_at
|
||||
SELECT id, email, password_hash, ui_language, role, created_at
|
||||
FROM users
|
||||
WHERE email = $1
|
||||
`, strings.ToLower(strings.TrimSpace(email)))
|
||||
if err := row.Scan(&user.ID, &user.Email, &hash, &user.UILanguage, &user.CreatedAt); err != nil {
|
||||
if err := row.Scan(&user.ID, &user.Email, &hash, &user.UILanguage, &user.Role, &user.CreatedAt); err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return domain.User{}, "", ErrNotFound
|
||||
}
|
||||
@@ -170,11 +170,11 @@ func (s *PostgresStore) GetPasswordHashByEmail(ctx context.Context, email string
|
||||
func (s *PostgresStore) GetUser(ctx context.Context, id string) (domain.User, error) {
|
||||
var user domain.User
|
||||
row := s.db.QueryRowContext(ctx, `
|
||||
SELECT id, email, ui_language, created_at
|
||||
SELECT id, email, ui_language, role, created_at
|
||||
FROM users
|
||||
WHERE id = $1
|
||||
`, id)
|
||||
if err := row.Scan(&user.ID, &user.Email, &user.UILanguage, &user.CreatedAt); err != nil {
|
||||
if err := row.Scan(&user.ID, &user.Email, &user.UILanguage, &user.Role, &user.CreatedAt); err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return domain.User{}, ErrNotFound
|
||||
}
|
||||
@@ -558,6 +558,11 @@ CREATE TABLE IF NOT EXISTS users (
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
-- Existing installations receive the role on startup; public registration
|
||||
-- continues to create only learner accounts.
|
||||
ALTER TABLE users ADD COLUMN IF NOT EXISTS role TEXT NOT NULL DEFAULT 'learner'
|
||||
CHECK (role IN ('learner', 'admin'));
|
||||
|
||||
CREATE TABLE IF NOT EXISTS learner_profiles (
|
||||
id TEXT PRIMARY KEY,
|
||||
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
|
||||
Reference in New Issue
Block a user