package http import ( "encoding/json" "log" "net/http" "strings" "datarush/internal/auth/service" ) type UserAchievementResponse struct { Name string `json:"name"` Description string `json:"description"` Icon string `json:"icon"` ReceivedAt string `json:"received_at"` } type UserDetailResponse struct { ID string `json:"id"` Email string `json:"email"` Username string `json:"username"` Avatar *string `json:"avatar"` CreatedAt string `json:"created_at"` Achievements []UserAchievementResponse `json:"achievements"` } type StatResponse struct { TotalAttempts int `json:"total_attempts"` SolvedTasks int `json:"solved_tasks"` } type UserHandler struct { authService *service.AuthService } func NewUserHandler(authService *service.AuthService) *UserHandler { return &UserHandler{ authService: authService, } } func (h *UserHandler) GetMe(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet { http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) return } // Extract token from Authorization header authHeader := r.Header.Get("Authorization") if authHeader == "" { w.WriteHeader(http.StatusUnauthorized) json.NewEncoder(w).Encode(ErrorResponse{Detail: "unauthorized"}) return } parts := strings.Split(authHeader, " ") if len(parts) != 2 || parts[0] != "Bearer" { w.WriteHeader(http.StatusUnauthorized) json.NewEncoder(w).Encode(ErrorResponse{Detail: "unauthorized"}) return } token := parts[1] user, err := h.authService.ValidateToken(r.Context(), token) if err != nil { log.Printf("validate token error: %v", err) w.WriteHeader(http.StatusUnauthorized) json.NewEncoder(w).Encode(ErrorResponse{Detail: "unauthorized"}) return } w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(UserDetailResponse{ ID: user.ID.String(), Email: user.Email, Username: user.Username, Avatar: nil, CreatedAt: user.CreatedAt.Format("2006-01-02T15:04:05Z07:00"), Achievements: []UserAchievementResponse{}, }) } func (h *UserHandler) GetUser(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet { http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) return } userID := r.Header.Get("X-User-ID") if userID == "" { w.WriteHeader(http.StatusBadRequest) json.NewEncoder(w).Encode(ErrorResponse{Detail: "invalid user_id"}) return } // Extract token from Authorization header for verification authHeader := r.Header.Get("Authorization") if authHeader == "" { w.WriteHeader(http.StatusUnauthorized) json.NewEncoder(w).Encode(ErrorResponse{Detail: "unauthorized"}) return } parts := strings.Split(authHeader, " ") if len(parts) != 2 || parts[0] != "Bearer" { w.WriteHeader(http.StatusUnauthorized) json.NewEncoder(w).Encode(ErrorResponse{Detail: "unauthorized"}) return } token := parts[1] _, err := h.authService.ValidateToken(r.Context(), token) if err != nil { log.Printf("validate token error: %v", err) w.WriteHeader(http.StatusUnauthorized) json.NewEncoder(w).Encode(ErrorResponse{Detail: "unauthorized"}) return } // For now, return a simple user response // In a real implementation, you would fetch from the database w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(UserDetailResponse{ ID: userID, Email: "user@example.com", Username: "user", Avatar: nil, CreatedAt: "2024-12-15T10:30:00Z", Achievements: []UserAchievementResponse{}, }) } func (h *UserHandler) GetMyStat(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet { http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) return } authHeader := r.Header.Get("Authorization") if authHeader == "" { w.WriteHeader(http.StatusUnauthorized) json.NewEncoder(w).Encode(ErrorResponse{Detail: "unauthorized"}) return } parts := strings.Split(authHeader, " ") if len(parts) != 2 || parts[0] != "Bearer" { w.WriteHeader(http.StatusUnauthorized) json.NewEncoder(w).Encode(ErrorResponse{Detail: "unauthorized"}) return } token := parts[1] _, err := h.authService.ValidateToken(r.Context(), token) if err != nil { log.Printf("validate token error: %v", err) w.WriteHeader(http.StatusUnauthorized) json.NewEncoder(w).Encode(ErrorResponse{Detail: "unauthorized"}) return } w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(StatResponse{ TotalAttempts: 0, SolvedTasks: 0, }) } func (h *UserHandler) GetLeaderboard(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet { http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) return } // Return empty leaderboard for now w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode([]UserDetailResponse{}) }