package controllers import ( "GoTesting/models" "encoding/json" "fmt" "html/template" "log" "net/http" "strings" ) var ( templates *template.Template ) func init() { // Parse templates with custom functions if needed templates = template.Must(template.New("").Funcs(template.FuncMap{ "formatScore": formatScore, }).ParseGlob("views/*.tmpl")) } // MatchData represents all data needed for the match template type MatchData struct { MatchID string MatchStatus string Team1 TeamData Team2 TeamData Innings []Inning UserTeam UserTeamInfo } type TeamData struct { Name string ShortName string Score string Overs string FlagImage string } type Inning struct { Team TeamData Batters []Player Bowlers []Player DidNotBat []Player FallOfWickets []Wicket Extras string Total string } type Player struct { Name string Image string Runs int Balls int Fours int Sixes int StrikeRate float64 Dismissal string IsNotOut bool IsTopScorer bool } type Wicket struct { PlayerName string Dismissal string Score string Over string } type UserTeamInfo struct { Name string Points int PlayerIDs []string } func formatScore(score int) string { return fmt.Sprintf("%d", score) } var ScorecardModel *models.ScorecardDataModel func GoTesting(w http.ResponseWriter, r *http.Request) { matchID := strings.TrimPrefix(r.URL.Path, "/match/") if matchID == "" { http.Error(w, "Missing matchkey", http.StatusBadRequest) return } // Fetch single result matches, err := models.GetMatchData(matchID) if err != nil { log.Println("Error fetching match:", err) http.Error(w, "Server error", http.StatusInternalServerError) return } if len(matches) == 0 { http.Error(w, "No match found", http.StatusNotFound) return } firstMatch := matches[0] var decoded map[string]interface{} // Decode JSON string from DB err = json.Unmarshal([]byte(firstMatch.FullResponse), &decoded) if err != nil { log.Println("Error decoding FullResponse:", err) http.Error(w, "Invalid JSON in FullResponse", http.StatusInternalServerError) return } // Debug print // Extract "response" object responseData, ok := decoded["response"].(map[string]interface{}) if !ok { http.Error(w, "Invalid JSON format in response field", http.StatusInternalServerError) return } fmt.Printf("DEBUG: competition = %#v\n", responseData["competition"]) fmt.Printf("DEBUG: format_str = %#v\n", responseData["format_str"]) fmt.Printf("DEBUG: innings = %#v\n", responseData["innings"]) // Render template err = templates.ExecuteTemplate(w, "match.tmpl", responseData) if err != nil { log.Println("Error executing template:", err) http.Error(w, err.Error(), http.StatusInternalServerError) return } }