feat: add /api/user/me endpoint for session restoration

This commit is contained in:
2026-01-04 14:31:34 +03:00
parent 58aa72f9bb
commit e98d10ae1d
4 changed files with 58 additions and 0 deletions

View File

@@ -227,3 +227,24 @@ func (r *UserRepository) UpdateUserRole(ctx context.Context, userID int, newRole
}
return nil
}
// GetUserByID находит пользователя по его ID.
func (r *UserRepository) GetUserByID(ctx context.Context, userID int) (*models.User, error) {
user := &models.User{}
var userUUID string
query := "SELECT id, uuid, username, email, password_hash, role, created_at, updated_at FROM users WHERE id = $1"
err := r.DB.QueryRow(ctx, query, userID).Scan(
&user.ID, &userUUID, &user.Username, &user.Email, &user.PasswordHash, &user.Role, &user.CreatedAt, &user.UpdatedAt,
)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return nil, ErrUserNotFound
}
return nil, err
}
user.UUID, _ = uuid.Parse(userUUID)
return user, nil
}