From 8084c4194b705e0b1c1eb287a80c4aa30b49c7f7 Mon Sep 17 00:00:00 2001 From: Flowing Date: Wed, 8 Jan 2020 00:11:12 +0900 Subject: [PATCH 1/8] backup --- server/src/api/api.go | 543 +++++++-------------------------------- server/src/api/models.go | 6 - 2 files changed, 91 insertions(+), 458 deletions(-) diff --git a/server/src/api/api.go b/server/src/api/api.go index 1e672ed0..13ac440e 100644 --- a/server/src/api/api.go +++ b/server/src/api/api.go @@ -40,7 +40,7 @@ func GetVersion(w http.ResponseWriter, r *http.Request) { } jsonbyte, err := json.Marshal(response) if err != nil { - w.WriteHeader(http.StatusInternalServerError) + http.Error(w, "JSON Format invalid", http.StatusBadRequest) return } w.WriteHeader(http.StatusOK) @@ -63,13 +63,15 @@ func CheckLoggedIn(w http.ResponseWriter, r *http.Request) { } jsonbyte, err := json.Marshal(response) if err != nil { - w.WriteHeader(http.StatusInternalServerError) + http.Error(w, "JSON Format invalid", http.StatusBadRequest) return } + w.WriteHeader(http.StatusOK) w.Header().Set("Content-Type", "application/json") w.Write(jsonbyte) } +// GetMatchInfo Gets match info by ID func GetMatchInfo(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) fmt.Printf("GetMatchInfo\n") @@ -137,10 +139,10 @@ func GetMatchInfo(w http.ResponseWriter, r *http.Request) { jsonbyte, err := json.Marshal(response) if err != nil { - w.WriteHeader(http.StatusInternalServerError) + http.Error(w, "JSON Format invalid", http.StatusBadRequest) return } - fmt.Println(string(jsonbyte)) + w.WriteHeader(http.StatusOK) w.Header().Set("Content-Type", "application/json") w.Write(jsonbyte) } @@ -163,10 +165,10 @@ func GetPlayerStatInfo(w http.ResponseWriter, r *http.Request) { jsonbyte, err := json.Marshal(response) if err != nil { - w.WriteHeader(http.StatusInternalServerError) + http.Error(w, "JSON Format invalid", http.StatusInternalServerError) return } - fmt.Println(string(jsonbyte)) + w.WriteHeader(http.StatusOK) w.Header().Set("Content-Type", "application/json") w.Write(jsonbyte) } @@ -249,10 +251,10 @@ func GetUserInfo(w http.ResponseWriter, r *http.Request) { jsonbyte, err := json.Marshal(response) if err != nil { - w.WriteHeader(http.StatusInternalServerError) + http.Error(w, "JSON Format invalid", http.StatusInternalServerError) return } - fmt.Println(string(jsonbyte)) + w.WriteHeader(http.StatusOK) w.Header().Set("Content-Type", "application/json") w.Write(jsonbyte) } @@ -265,10 +267,10 @@ func GetServerInfo(w http.ResponseWriter, r *http.Request) { db.SQLAccess.Gorm.Where("id = ?", serverID).First(&response) jsonbyte, err := json.Marshal(response) if err != nil { - w.WriteHeader(http.StatusInternalServerError) + http.Error(w, "JSON Format invalid", http.StatusInternalServerError) return } - fmt.Println(string(jsonbyte)) + w.WriteHeader(http.StatusOK) w.Header().Set("Content-Type", "application/json") w.Write(jsonbyte) } @@ -279,12 +281,13 @@ func GetStatusString(w http.ResponseWriter, r *http.Request) { matchid := vars["matchID"] response := db.MatchData{} db.SQLAccess.Gorm.Where("id = ?", matchid).First(&response) - w.Header().Set("Content-Type", "application/json") status, err := response.GetStatusString(true) if err != nil { - w.WriteHeader(http.StatusInternalServerError) + http.Error(w, "Failed to get status", http.StatusInternalServerError) return } + w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", "text/plain") w.Write([]byte(status)) } @@ -304,10 +307,10 @@ func GetMatches(w http.ResponseWriter, r *http.Request) { } jsonbyte, err := json.Marshal(response) if err != nil { - w.WriteHeader(http.StatusInternalServerError) + http.Error(w, "JSON Format invalid", http.StatusInternalServerError) return } - fmt.Println(string(jsonbyte)) + w.WriteHeader(http.StatusOK) w.Header().Set("Content-Type", "application/json") w.Write(jsonbyte) } @@ -318,9 +321,10 @@ func GetTeamInfo(w http.ResponseWriter, r *http.Request) { teamid := vars["teamID"] response := APITeamData{} db.SQLAccess.Gorm.Where("id = ?", teamid).First(&response) + var steamids = make([]string, 5) steamids, err := response.GetPlayers() if err != nil { - w.WriteHeader(http.StatusInternalServerError) + http.Error(w, "Failed to get players.", http.StatusInternalServerError) return } for i := 0; i < len(steamids); i++ { @@ -328,9 +332,10 @@ func GetTeamInfo(w http.ResponseWriter, r *http.Request) { } jsonbyte, err := json.Marshal(response) if err != nil { - w.WriteHeader(http.StatusInternalServerError) + http.Error(w, "JSON Format invalid", http.StatusInternalServerError) return } + w.WriteHeader(http.StatusOK) w.Header().Set("Content-Type", "application/json") w.Write(jsonbyte) } @@ -406,10 +411,10 @@ func GetRecentMatches(w http.ResponseWriter, r *http.Request) { jsonbyte, err := json.Marshal(response) if err != nil { - w.WriteHeader(http.StatusInternalServerError) + http.Error(w, "JSON Format invalid", http.StatusInternalServerError) return } - fmt.Println(string(jsonbyte)) + w.WriteHeader(http.StatusOK) w.Header().Set("Content-Type", "application/json") w.Write(jsonbyte) } @@ -421,21 +426,15 @@ func CheckUserCanEdit(w http.ResponseWriter, r *http.Request) { teamid := vars["teamID"] useridstr := q.Get("userID") team := db.TeamData{} - res := SimpleJSONResponse{} db.SQLAccess.Gorm.Where("id = ?", teamid).First(&team) userid, err := strconv.Atoi(useridstr) if err != nil { - w.WriteHeader(http.StatusInternalServerError) - return - } - res.Response = strconv.FormatBool(team.CanEdit(userid)) - jsonbyte, err := json.Marshal(res) - if err != nil { - w.WriteHeader(http.StatusInternalServerError) + http.Error(w, "userid should be int", http.StatusBadRequest) return } + w.WriteHeader(http.StatusOK) w.Header().Set("Content-Type", "application/json") - w.Write(jsonbyte) + w.Write([]byte(strconv.FormatBool(team.CanEdit(userid)))) } func GetMetrics(w http.ResponseWriter, r *http.Request) { @@ -443,28 +442,31 @@ func GetMetrics(w http.ResponseWriter, r *http.Request) { Metrics := db.GetMetrics() jsonbyte, err := json.Marshal(Metrics) if err != nil { - w.WriteHeader(http.StatusInternalServerError) + http.Error(w, "JSON Format Invalid", http.StatusInternalServerError) return } - fmt.Println(string(jsonbyte)) + w.WriteHeader(http.StatusOK) w.Header().Set("Content-Type", "application/json") w.Write(jsonbyte) } +// GetSteamName Get Steam Profile name by SteamWebAPI func GetSteamName(w http.ResponseWriter, r *http.Request) { fmt.Printf("GetSteamName\n") q := r.URL.Query() steamid := q.Get("steamID") steamid64, err := strconv.Atoi(steamid) if err != nil { - w.WriteHeader(http.StatusInternalServerError) + http.Error(w, "steamID should be int", http.StatusBadRequest) return } steamname, err := db.GetSteamName(uint64(steamid64)) if err != nil { - w.WriteHeader(http.StatusInternalServerError) + http.Error(w, "failed to get steamname", http.StatusInternalServerError) return } + w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", "text/plain") w.Write([]byte(steamname)) } @@ -488,9 +490,10 @@ func GetTeamList(w http.ResponseWriter, r *http.Request) { } jsonbyte, err := json.Marshal(Teams) if err != nil { - w.WriteHeader(http.StatusInternalServerError) + http.Error(w, "JSON Format invalid", http.StatusInternalServerError) return } + w.WriteHeader(http.StatusOK) w.Header().Set("Content-Type", "application/json") w.Write(jsonbyte) } @@ -512,9 +515,10 @@ func GetServerList(w http.ResponseWriter, r *http.Request) { } jsonbyte, err := json.Marshal(Servers) if err != nil { - w.WriteHeader(http.StatusInternalServerError) + http.Error(w, "JSON Format invalid", http.StatusBadRequest) return } + w.WriteHeader(http.StatusOK) w.Header().Set("Content-Type", "application/json") w.Write(jsonbyte) } @@ -533,40 +537,19 @@ func CreateTeam(w http.ResponseWriter, r *http.Request) { TeamTemp := db.TeamData{} err := json.NewDecoder(r.Body).Decode(&TeamTemp) if err != nil { - fmt.Println("Failed to decode JSON") http.Error(w, "JSON Format invalid", http.StatusBadRequest) return } _, err = Team.Create(userid, TeamTemp.Name, TeamTemp.Tag, TeamTemp.Flag, TeamTemp.Logo, TeamTemp.Auths, TeamTemp.PublicTeam) if err != nil { - fmt.Println("Failed to create team") http.Error(w, "Failed to create team", http.StatusInternalServerError) return } - res := SimpleJSONResponse{ - Response: "OK", - } - jsonbyte, err := json.Marshal(res) - if err != nil { - http.Error(w, "Internal ERROR", http.StatusInternalServerError) - return - } w.WriteHeader(http.StatusOK) - w.Header().Set("Content-Type", "application/json") - w.Write(jsonbyte) + w.Header().Set("Content-Type", "text/plain") + w.Write([]byte("OK")) } else { - res := SimpleJSONResponse{ - Errorcode: http.StatusUnauthorized, - Errormessage: "Forbidden", - } - jsonbyte, err := json.Marshal(res) - if err != nil { - http.Error(w, "Internal ERROR", http.StatusInternalServerError) - return - } - w.WriteHeader(http.StatusUnauthorized) - w.Header().Set("Content-Type", "application/json") - w.Write(jsonbyte) + http.Error(w, "Forbidden", http.StatusForbidden) } } @@ -583,58 +566,18 @@ func EditTeam(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) teamid, err := strconv.Atoi(vars["teamID"]) if err != nil { - fmt.Println(err) - fmt.Println("teamid should be int") - res := SimpleJSONResponse{ - Response: "error", - Errorcode: http.StatusBadRequest, - Errormessage: "teamid should be int.", - } - jsonbyte, err := json.Marshal(res) - if err != nil { - http.Error(w, "Internal ERROR", http.StatusInternalServerError) - return - } - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusBadRequest) - w.Write(jsonbyte) + http.Error(w, "teamid should be int.", http.StatusBadRequest) return } Team := db.TeamData{ID: teamid} db.SQLAccess.Gorm.First(&Team) if !Team.CanEdit(userid) { - fmt.Println("You do not have permission to edit this team.") - res := SimpleJSONResponse{ - Response: "error", - Errorcode: http.StatusUnauthorized, - Errormessage: "You dont have permission to edit this team.", - } - jsonbyte, err := json.Marshal(res) - if err != nil { - http.Error(w, "Internal ERROR", http.StatusInternalServerError) - return - } - w.WriteHeader(http.StatusUnauthorized) - w.Header().Set("Content-Type", "application/json") - w.Write(jsonbyte) + http.Error(w, "You do not have permission to edit this team.", http.StatusUnauthorized) return } err = json.NewDecoder(r.Body).Decode(&Team) if err != nil { - fmt.Println("Failed to decode JSON") - res := SimpleJSONResponse{ - Response: "error", - Errorcode: http.StatusBadRequest, - Errormessage: "JSON Format invalid", - } - jsonbyte, err := json.Marshal(res) - if err != nil { - http.Error(w, "Internal ERROR", http.StatusInternalServerError) - return - } - w.WriteHeader(http.StatusBadRequest) - w.Header().Set("Content-Type", "application/json") - w.Write(jsonbyte) + http.Error(w, "JSON Format Invalid", http.StatusBadRequest) return } Team.ID = teamid @@ -642,48 +585,14 @@ func EditTeam(w http.ResponseWriter, r *http.Request) { _, err = Team.Edit() if err != nil { - fmt.Printf("Failed to edit team %v\n", teamid) - http.Error(w, "", http.StatusInternalServerError) - res := SimpleJSONResponse{ - Response: "error", - Errorcode: http.StatusInternalServerError, - Errormessage: "Failed to edit team", - } - jsonbyte, err := json.Marshal(res) - if err != nil { - http.Error(w, "Internal ERROR", http.StatusInternalServerError) - return - } - w.WriteHeader(http.StatusInternalServerError) - w.Header().Set("Content-Type", "application/json") - w.Write(jsonbyte) + http.Error(w, "Failed to edit team", http.StatusInternalServerError) return } w.WriteHeader(http.StatusOK) - res := SimpleJSONResponse{ - Response: "OK", - } - jsonbyte, err := json.Marshal(res) - if err != nil { - http.Error(w, "Internal ERROR", http.StatusInternalServerError) - return - } - w.Header().Set("Content-Type", "application/json") - w.Write(jsonbyte) + w.Header().Set("Content-Type", "text/plain") + w.Write([]byte("OK")) } else { - w.WriteHeader(http.StatusUnauthorized) - res := SimpleJSONResponse{ - Errorcode: http.StatusUnauthorized, - Errormessage: "Forbidden", - } - jsonbyte, err := json.Marshal(res) - if err != nil { - http.Error(w, "Internal ERROR", http.StatusInternalServerError) - return - } - w.WriteHeader(http.StatusUnauthorized) - w.Header().Set("Content-Type", "application/json") - w.Write(jsonbyte) + http.Error(w, "Forbidden", http.StatusUnauthorized) } } @@ -700,87 +609,26 @@ func DeleteTeam(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) teamID, err := strconv.Atoi(vars["teamID"]) if err != nil { - fmt.Println("teamID should be int") - res := SimpleJSONResponse{ - Response: "error", - Errorcode: http.StatusBadRequest, - Errormessage: "teamID should be int.", - } - jsonbyte, err := json.Marshal(res) - if err != nil { - http.Error(w, "Internal ERROR", http.StatusInternalServerError) - return - } - w.WriteHeader(http.StatusBadRequest) - w.Header().Set("Content-Type", "application/json") - w.Write(jsonbyte) + http.Error(w, "teamID should be int.", http.StatusBadRequest) return } Team := db.TeamData{ID: teamID} if !Team.CanDelete(userid) { - fmt.Println("You dont have permission to delete this team.") - res := SimpleJSONResponse{ - Response: "error", - Errorcode: http.StatusUnauthorized, - Errormessage: "You dont have permission to delete this team.", - } - jsonbyte, err := json.Marshal(res) - if err != nil { - http.Error(w, "Internal ERROR", http.StatusInternalServerError) - return - } - w.WriteHeader(http.StatusUnauthorized) - w.Header().Set("Content-Type", "application/json") - w.Write(jsonbyte) + http.Error(w, "You dont have permission to delete this team.", http.StatusUnauthorized) return } err = Team.Delete() if err != nil { - fmt.Printf("Failed to delete team %v\n", teamID) - http.Error(w, "", http.StatusInternalServerError) - res := SimpleJSONResponse{ - Response: "error", - Errorcode: http.StatusInternalServerError, - Errormessage: "Failed to delete team", - } - jsonbyte, err := json.Marshal(res) - if err != nil { - http.Error(w, "Internal ERROR", http.StatusInternalServerError) - return - } - w.WriteHeader(http.StatusInternalServerError) - w.Header().Set("Content-Type", "application/json") - w.Write(jsonbyte) + http.Error(w, "Failed to delete team", http.StatusInternalServerError) return } w.WriteHeader(http.StatusOK) - res := SimpleJSONResponse{ - Response: "OK", - } - jsonbyte, err := json.Marshal(res) - if err != nil { - http.Error(w, "Internal ERROR", http.StatusInternalServerError) - return - } - w.WriteHeader(http.StatusInternalServerError) - w.Header().Set("Content-Type", "application/json") - w.Write(jsonbyte) + w.Header().Set("Content-Type", "text/plain") + w.Write([]byte("OK")) } else { - w.WriteHeader(http.StatusUnauthorized) - res := SimpleJSONResponse{ - Errorcode: http.StatusUnauthorized, - Errormessage: "Forbidden", - } - jsonbyte, err := json.Marshal(res) - if err != nil { - http.Error(w, "Internal ERROR", http.StatusInternalServerError) - return - } - w.WriteHeader(http.StatusUnauthorized) - w.Header().Set("Content-Type", "application/json") - w.Write(jsonbyte) + http.Error(w, "Forbidden", http.StatusUnauthorized) } } @@ -798,7 +646,6 @@ func CreateServer(w http.ResponseWriter, r *http.Request) { ServerTemp := db.GameServerData{} err := json.NewDecoder(r.Body).Decode(&ServerTemp) if err != nil { - fmt.Println("Failed to decode JSON") http.Error(w, "JSON Format invalid", http.StatusBadRequest) return } @@ -808,30 +655,11 @@ func CreateServer(w http.ResponseWriter, r *http.Request) { return } w.WriteHeader(http.StatusOK) - res := SimpleJSONResponse{ - Response: "OK", - } - jsonbyte, err := json.Marshal(res) - if err != nil { - http.Error(w, "Internal ERROR", http.StatusInternalServerError) - return - } - w.Header().Set("Content-Type", "application/json") - w.Write(jsonbyte) + w.Header().Set("Content-Type", "text/plain") + w.Write([]byte("OK")) } else { - w.WriteHeader(http.StatusUnauthorized) - res := SimpleJSONResponse{ - Errorcode: http.StatusUnauthorized, - Errormessage: "Forbidden", - } - jsonbyte, err := json.Marshal(res) - if err != nil { - http.Error(w, "Internal ERROR", http.StatusInternalServerError) - return - } - w.Header().Set("Content-Type", "application/json") - w.Write(jsonbyte) + http.Error(w, "Forbidden", http.StatusUnauthorized) } } @@ -849,56 +677,17 @@ func EditServer(w http.ResponseWriter, r *http.Request) { serverID, err := strconv.Atoi(vars["serverID"]) if err != nil { fmt.Println(err) - fmt.Println("serverID should be int") - res := SimpleJSONResponse{ - Response: "error", - Errorcode: http.StatusBadRequest, - Errormessage: "serverID should be int.", - } - jsonbyte, err := json.Marshal(res) - if err != nil { - http.Error(w, "Internal ERROR", http.StatusInternalServerError) - return - } - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusBadRequest) - w.Write(jsonbyte) + http.Error(w, "serverID should be int", http.StatusBadRequest) return } Server := db.GameServerData{ID: serverID} if !Server.CanEdit(userid) { - fmt.Println("You do not have permission to edit this server.") - res := SimpleJSONResponse{ - Response: "error", - Errorcode: http.StatusUnauthorized, - Errormessage: "You dont have permission to edit this server.", - } - jsonbyte, err := json.Marshal(res) - if err != nil { - http.Error(w, "Internal ERROR", http.StatusInternalServerError) - return - } - w.WriteHeader(http.StatusUnauthorized) - w.Header().Set("Content-Type", "application/json") - w.Write(jsonbyte) + http.Error(w, "You do not have permission to edit this server.", http.StatusForbidden) return } err = json.NewDecoder(r.Body).Decode(&Server) if err != nil { - fmt.Println("Failed to decode JSON") - res := SimpleJSONResponse{ - Response: "error", - Errorcode: http.StatusBadRequest, - Errormessage: "JSON Format invalid", - } - jsonbyte, err := json.Marshal(res) - if err != nil { - http.Error(w, "Internal ERROR", http.StatusInternalServerError) - return - } - w.WriteHeader(http.StatusBadRequest) - w.Header().Set("Content-Type", "application/json") - w.Write(jsonbyte) + http.Error(w, "JSON Format invalid", http.StatusBadRequest) return } Server.ID = serverID @@ -906,48 +695,14 @@ func EditServer(w http.ResponseWriter, r *http.Request) { _, err = Server.Edit() if err != nil { - fmt.Printf("Failed to edit server %v\n", serverID) - http.Error(w, "", http.StatusInternalServerError) - res := SimpleJSONResponse{ - Response: "error", - Errorcode: http.StatusInternalServerError, - Errormessage: "Failed to edit server", - } - jsonbyte, err := json.Marshal(res) - if err != nil { - http.Error(w, "Internal ERROR", http.StatusInternalServerError) - return - } - w.WriteHeader(http.StatusInternalServerError) - w.Header().Set("Content-Type", "application/json") - w.Write(jsonbyte) + http.Error(w, "Failed to edit server", http.StatusInternalServerError) return } w.WriteHeader(http.StatusOK) - res := SimpleJSONResponse{ - Response: "OK", - } - jsonbyte, err := json.Marshal(res) - if err != nil { - http.Error(w, "Internal ERROR", http.StatusInternalServerError) - return - } - w.Header().Set("Content-Type", "application/json") - w.Write(jsonbyte) + w.Header().Set("Content-Type", "text/plain") + w.Write([]byte("OK")) } else { - w.WriteHeader(http.StatusUnauthorized) - res := SimpleJSONResponse{ - Errorcode: http.StatusUnauthorized, - Errormessage: "Forbidden", - } - jsonbyte, err := json.Marshal(res) - if err != nil { - http.Error(w, "Internal ERROR", http.StatusInternalServerError) - return - } - w.WriteHeader(http.StatusUnauthorized) - w.Header().Set("Content-Type", "application/json") - w.Write(jsonbyte) + http.Error(w, "Forbidden", http.StatusUnauthorized) } } @@ -964,87 +719,26 @@ func DeleteServer(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) serverID, err := strconv.Atoi(vars["serverID"]) if err != nil { - fmt.Println("serverID should be int") - res := SimpleJSONResponse{ - Response: "error", - Errorcode: http.StatusBadRequest, - Errormessage: "serverID should be int.", - } - jsonbyte, err := json.Marshal(res) - if err != nil { - http.Error(w, "Internal ERROR", http.StatusInternalServerError) - return - } - w.WriteHeader(http.StatusBadRequest) - w.Header().Set("Content-Type", "application/json") - w.Write(jsonbyte) + http.Error(w, "serverID should be int", http.StatusBadRequest) return } Server := db.GameServerData{ID: serverID} if !Server.CanDelete(userid) { - fmt.Println("You dont have permission to delete this server.") - res := SimpleJSONResponse{ - Response: "error", - Errorcode: http.StatusUnauthorized, - Errormessage: "You dont have permission to delete this server.", - } - jsonbyte, err := json.Marshal(res) - if err != nil { - http.Error(w, "Internal ERROR", http.StatusInternalServerError) - return - } - w.WriteHeader(http.StatusUnauthorized) - w.Header().Set("Content-Type", "application/json") - w.Write(jsonbyte) + http.Error(w, "You dont have permission to delete this server.", http.StatusUnauthorized) return } err = Server.Delete() if err != nil { - fmt.Printf("Failed to delete server %v\n", serverID) - http.Error(w, "", http.StatusInternalServerError) - res := SimpleJSONResponse{ - Response: "error", - Errorcode: http.StatusInternalServerError, - Errormessage: "Failed to delete server", - } - jsonbyte, err := json.Marshal(res) - if err != nil { - http.Error(w, "Internal ERROR", http.StatusInternalServerError) - return - } - w.WriteHeader(http.StatusInternalServerError) - w.Header().Set("Content-Type", "application/json") - w.Write(jsonbyte) + http.Error(w, "Failed to delete server", http.StatusInternalServerError) return } w.WriteHeader(http.StatusOK) - res := SimpleJSONResponse{ - Response: "OK", - } - jsonbyte, err := json.Marshal(res) - if err != nil { - http.Error(w, "Internal ERROR", http.StatusInternalServerError) - return - } - w.WriteHeader(http.StatusInternalServerError) - w.Header().Set("Content-Type", "application/json") - w.Write(jsonbyte) + w.Header().Set("Content-Type", "text/plain") + w.Write([]byte("OK")) } else { - w.WriteHeader(http.StatusUnauthorized) - res := SimpleJSONResponse{ - Errorcode: http.StatusUnauthorized, - Errormessage: "Forbidden", - } - jsonbyte, err := json.Marshal(res) - if err != nil { - http.Error(w, "Internal ERROR", http.StatusInternalServerError) - return - } - w.WriteHeader(http.StatusUnauthorized) - w.Header().Set("Content-Type", "application/json") - w.Write(jsonbyte) + http.Error(w, "Forbidden", http.StatusUnauthorized) } } @@ -1060,53 +754,22 @@ func CreateMatch(w http.ResponseWriter, r *http.Request) { userid := s.Get("UserID").(int) var MatchTemp = db.MatchData{} var Match = db.MatchData{} - // Returns error if JSON is not valid... err := json.NewDecoder(r.Body).Decode(&MatchTemp) if err != nil { - fmt.Println("failed to decode Match, Format incorrect") - res := SimpleJSONResponse{ - Response: "error", - Errorcode: http.StatusBadRequest, - Errormessage: "JSON Format invalid", - } - jsonbyte, err := json.Marshal(res) - if err != nil { - http.Error(w, "Internal ERROR", http.StatusInternalServerError) - } - w.WriteHeader(http.StatusInternalServerError) - w.Header().Set("Content-Type", "application/json") - w.Write(jsonbyte) + http.Error(w, "JSON Format invalid", http.StatusBadRequest) return } _, err = Match.Create(userid, MatchTemp.Team1ID, MatchTemp.Team2ID, MatchTemp.Team1String, MatchTemp.Team2String, MatchTemp.MaxMaps, MatchTemp.SkipVeto, MatchTemp.Title, MatchTemp.VetoMapPoolJSON, MatchTemp.ServerID) if err != nil { - http.Error(w, "Internal ERROR", http.StatusInternalServerError) - return - } - res := SimpleJSONResponse{ - Response: "ok", - } - jsonbyte, err := json.Marshal(res) - if err != nil { - http.Error(w, "Internal ERROR", http.StatusInternalServerError) + http.Error(w, "Failed to create match", http.StatusInternalServerError) return } w.WriteHeader(http.StatusOK) - w.Header().Set("Content-Type", "application/json") - w.Write(jsonbyte) + w.Header().Set("Content-Type", "text/plain") + w.Write([]byte("OK")) } else { - w.WriteHeader(http.StatusUnauthorized) - res := SimpleJSONResponse{ - Errorcode: http.StatusUnauthorized, - Errormessage: "Forbidden", - } - jsonbyte, err := json.Marshal(res) - if err != nil { - http.Error(w, "Internal ERROR", http.StatusInternalServerError) - } - w.Header().Set("Content-Type", "application/json") - w.Write(jsonbyte) + http.Error(w, "Forbidden", http.StatusUnauthorized) } } @@ -1151,7 +814,7 @@ func MatchCancelHandler(w http.ResponseWriter, r *http.Request) { http.Error(w, fmt.Sprintf("Failed to cancel match: %v", err), http.StatusNotFound) } } else { - http.Error(w, "Please log in", http.StatusUnauthorized) + http.Error(w, "Forbidden", http.StatusUnauthorized) } } @@ -1196,19 +859,11 @@ func MatchRconHandler(w http.ResponseWriter, r *http.Request) { if res != "" { RconRes = res } - JSONres := SimpleJSONResponse{ - Response: RconRes, - } - jsonbyte, err := json.Marshal(JSONres) - if err != nil { - http.Error(w, "Internal ERROR", http.StatusInternalServerError) - return - } w.WriteHeader(http.StatusOK) - w.Header().Set("Content-Type", "application/json") - w.Write(jsonbyte) + w.Header().Set("Content-Type", "text/plain") + w.Write([]byte(RconRes)) } else { - http.Error(w, "Please log in", http.StatusUnauthorized) + http.Error(w, "Forbidden", http.StatusUnauthorized) return } } @@ -1245,19 +900,11 @@ func MatchPauseHandler(w http.ResponseWriter, r *http.Request) { if err != nil { http.Error(w, fmt.Sprintf("Failed to send pause command : %s", err), http.StatusInternalServerError) } - JSONres := SimpleJSONResponse{ - Response: "ok", - } - jsonbyte, err := json.Marshal(JSONres) - if err != nil { - http.Error(w, "Internal ERROR", http.StatusInternalServerError) - return - } w.WriteHeader(http.StatusOK) - w.Header().Set("Content-Type", "application/json") - w.Write(jsonbyte) + w.Header().Set("Content-Type", "text/plain") + w.Write([]byte("OK")) } else { - http.Error(w, "Please log in", http.StatusUnauthorized) + http.Error(w, "Forbidden", http.StatusUnauthorized) } } @@ -1293,19 +940,11 @@ func MatchUnpauseHandler(w http.ResponseWriter, r *http.Request) { if err != nil { http.Error(w, fmt.Sprintf("Failed to send unpause command : %s", err), http.StatusInternalServerError) } - JSONres := SimpleJSONResponse{ - Response: "ok", - } - jsonbyte, err := json.Marshal(JSONres) - if err != nil { - http.Error(w, "Internal ERROR", http.StatusInternalServerError) - return - } w.WriteHeader(http.StatusOK) - w.Header().Set("Content-Type", "application/json") - w.Write(jsonbyte) + w.Header().Set("Content-Type", "text/plain") + w.Write([]byte("OK")) } else { - http.Error(w, "Please log in", http.StatusUnauthorized) + http.Error(w, "Forbidden", http.StatusUnauthorized) } } @@ -1358,10 +997,10 @@ func MatchAddUserHandler(w http.ResponseWriter, r *http.Request) { return } w.WriteHeader(http.StatusOK) - w.Header().Set("Content-Type", "application/json") - fmt.Fprintf(w, res) + w.Header().Set("Content-Type", "text/plain") + w.Write([]byte(res)) } else { - http.Error(w, "Please log in", http.StatusUnauthorized) + http.Error(w, "Forbidden", http.StatusUnauthorized) } } @@ -1418,7 +1057,7 @@ func MatchListBackupsHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") w.Write(jsonbyte) } else { - http.Error(w, "Please log in", http.StatusUnauthorized) + http.Error(w, "Forbidden", http.StatusUnauthorized) } } @@ -1464,9 +1103,9 @@ func MatchLoadBackupsHandler(w http.ResponseWriter, r *http.Request) { return } w.WriteHeader(http.StatusOK) - w.Header().Set("Content-Type", "application/json") - fmt.Fprintf(w, res) + w.Header().Set("Content-Type", "text/plain") + w.Write([]byte(res)) } else { - http.Error(w, "Please log in", http.StatusUnauthorized) + http.Error(w, "Forbidden", http.StatusUnauthorized) } } diff --git a/server/src/api/models.go b/server/src/api/models.go index dda8c3c6..a3111263 100644 --- a/server/src/api/models.go +++ b/server/src/api/models.go @@ -214,9 +214,3 @@ func (p *APIPlayerStatsData) GetFPR() float64 { } return util.Round(float64(p.Kills)/float64(p.Roundsplayed), 2) } - -type SimpleJSONResponse struct { - Response string `json:"response"` - Errorcode int `json:"errorcode"` - Errormessage string `json:"errormessage"` -} From 4183f356b50570e04231ac68c6fb4ce571b0765f Mon Sep 17 00:00:00 2001 From: Flowing Date: Wed, 8 Jan 2020 00:23:01 +0900 Subject: [PATCH 2/8] /myteams route is now works --- web/src/components/Match.vue | 2 +- web/src/components/Teams.vue | 19 ++++++++++++------- web/src/router/index.ts | 5 +++++ 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/web/src/components/Match.vue b/web/src/components/Match.vue index 411a7574..6429352c 100644 --- a/web/src/components/Match.vue +++ b/web/src/components/Match.vue @@ -26,7 +26,7 @@ - + Date: Wed, 8 Jan 2020 00:32:20 +0900 Subject: [PATCH 3/8] suppress logs --- server/main.go | 2 +- server/src/api/get5.go | 1 - server/src/db/db.go | 8 +++----- server/src/db/models.go | 7 +------ server/src/util/util.go | 2 -- 5 files changed, 5 insertions(+), 15 deletions(-) diff --git a/server/main.go b/server/main.go index 621b8d71..3d9d8c88 100644 --- a/server/main.go +++ b/server/main.go @@ -56,7 +56,7 @@ func init() { SQLDBName: c.Section("sql").Key("database").MustString(""), } HOST = Cnf.HOST - fmt.Println(db.SQLAccess) + // fmt.Println(db.SQLAccess) } func main() { diff --git a/server/src/api/get5.go b/server/src/api/get5.go index 27a05639..bd29191b 100644 --- a/server/src/api/get5.go +++ b/server/src/api/get5.go @@ -128,7 +128,6 @@ func MatchMapStartHandler(w http.ResponseWriter, r *http.Request) { mUpdate.StartTime.Scan(time.Now()) db.SQLAccess.Gorm.Model(&m).Update(&mUpdate) db.SQLAccess.Gorm.Save(&mUpdate) - fmt.Println(MapStats) } // MatchMapUpdateHandler Handler for /api/v1/match/{matchID}/map/{mapNumber}/update API. diff --git a/server/src/db/db.go b/server/src/db/db.go index 43b94a70..9740ac2c 100644 --- a/server/src/db/db.go +++ b/server/src/db/db.go @@ -82,7 +82,7 @@ func init() { // Cookie string, the session's client cookie name, for example: "mysessionid" // // Defaults to "gosessionid" - Cookie: "mysessionid", + Cookie: "mysessionid", // TODO // it's time.Duration, from the time cookie is created, how long it can be alive? // 0 means no expire. // -1 means expire when browser closes @@ -98,6 +98,7 @@ func init() { // LoginHandler HTTP Handler for /login page. func LoginHandler(w http.ResponseWriter, r *http.Request) { + fmt.Printf("LoginHandler\n") opID := steam_go.NewOpenId(r) switch opID.Mode() { case "": @@ -129,14 +130,11 @@ func LoginHandler(w http.ResponseWriter, r *http.Request) { // Register to DB if its new player http.Redirect(w, r, "/", 302) } - vars := mux.Vars(r) //パスパラメータ取得 - fmt.Printf("LoginHandler\nvars : %v\n", vars) - w.WriteHeader(http.StatusOK) } // LogoutHandler HTTP Handler for /logout func LogoutHandler(w http.ResponseWriter, r *http.Request) { - //fmt.Printf("LogoutHandler\n") + fmt.Printf("LogoutHandler\n") s := Sess.Start(w, r) s.Destroy() http.Redirect(w, r, "/", 302) diff --git a/server/src/db/models.go b/server/src/db/models.go index a46c7a50..1fab7501 100644 --- a/server/src/db/models.go +++ b/server/src/db/models.go @@ -71,7 +71,7 @@ func (u *UserData) GetOrCreate(g *gorm.DB, steamid string) (*UserData, error) { g.Create(&SQLUserData) } else { fmt.Println("USER EXIST") - fmt.Println(SQLUserData) + // fmt.Println(SQLUserData) u.Name = SQLUserData.Name u.ID = SQLUserData.ID u.Admin = SQLUserData.Admin @@ -82,7 +82,6 @@ func (u *UserData) GetOrCreate(g *gorm.DB, steamid string) (*UserData, error) { // GetURL Get user page URL func (u *UserData) GetURL() string { - fmt.Println(Cnf) return fmt.Sprintf("http://%s/user/%d", Cnf.HOST, u.ID) } @@ -195,14 +194,12 @@ func (g *GameServerData) SendRcon(cmd string) (string, error) { o := &gosteam.ConnectOptions{RCONPassword: g.RconPassword} rcon, err := gosteam.Connect(g.GetHostPort(), o) if err != nil { - fmt.Println(err) return "", err } defer rcon.Close() resp, err := rcon.Send(cmd) if err != nil { - fmt.Println(err) return "", err } return resp, nil @@ -651,7 +648,6 @@ func (m *MatchData) GetCurrentScore(g *gorm.DB) (int, int) { //g.First(&m).Association("MapStats").Find(&m) m.MapStats = []MapStatsData{} g.First(&m.MapStats, "match_id = ?", m.ID) - fmt.Println(m.MapStats) if m.MaxMaps == 1 { if len(m.MapStats) == 0 { // check ok? return 0, 0 @@ -752,7 +748,6 @@ func (m *MatchData) SendToServer() error { } res, err := m.Server.SendRcon(fmt.Sprintf("get5_loadmatch_url %s/api/v1/match/%v/config", Cnf.HOST, m.ID)) res, err = m.Server.SendRcon(fmt.Sprintf("get5_web_api_key %s", m.APIKey)) - fmt.Println(res) if err != nil || res != "" { return err } diff --git a/server/src/util/util.go b/server/src/util/util.go index a9a55505..301712f1 100644 --- a/server/src/util/util.go +++ b/server/src/util/util.go @@ -84,14 +84,12 @@ func SendRCON(host string, port int, pass string, cmd string) (string, error) { o := &gosteam.ConnectOptions{RCONPassword: pass} rcon, err := gosteam.Connect(dest, o) if err != nil { - fmt.Println(err) return "", err } defer rcon.Close() resp, err := rcon.Send(cmd) if err != nil { - fmt.Println(err) return "", err } return resp, nil From 3576ac51b35d11346bbe565ba003af5e32c114fb Mon Sep 17 00:00:00 2001 From: Flowing Date: Wed, 8 Jan 2020 00:33:54 +0900 Subject: [PATCH 4/8] Update db.go --- server/src/db/db.go | 1 - 1 file changed, 1 deletion(-) diff --git a/server/src/db/db.go b/server/src/db/db.go index 9740ac2c..f9a99c48 100644 --- a/server/src/db/db.go +++ b/server/src/db/db.go @@ -6,7 +6,6 @@ import ( "time" "github.com/go-ini/ini" - "github.com/gorilla/mux" _ "github.com/jinzhu/gorm/dialects/mysql" "github.com/kataras/go-sessions" "github.com/solovev/steam_go" From d139ce49166351c1525d745d4178c3cae1c185d3 Mon Sep 17 00:00:00 2001 From: Flowing Date: Wed, 8 Jan 2020 01:05:25 +0900 Subject: [PATCH 5/8] refactoring repalced Gorm.Where("id = ?",id).First(&res) -> Gorm.First(&res,id) API fixes --- server/src/api/api.go | 72 +++++++++++++++++++----------------- server/src/api/get5.go | 21 ++++++----- server/src/db/models.go | 47 +++++++++++++---------- web/src/components/Team.vue | 4 +- web/src/components/Teams.vue | 2 +- 5 files changed, 81 insertions(+), 65 deletions(-) diff --git a/server/src/api/api.go b/server/src/api/api.go index 13ac440e..8ed66436 100644 --- a/server/src/api/api.go +++ b/server/src/api/api.go @@ -82,12 +82,12 @@ func GetMatchInfo(w http.ResponseWriter, r *http.Request) { team1 := APITeamData{} team2 := APITeamData{} user := APIUserData{} - db.SQLAccess.Gorm.Where("id = ?", matchid).First(&match) + db.SQLAccess.Gorm.First(&match, matchid) db.SQLAccess.Gorm.Where("match_id = ?", matchid).Limit(7).Find(&mapstats) - db.SQLAccess.Gorm.Where("id = ?", match.ServerID).First(&server) - db.SQLAccess.Gorm.Where("id = ?", match.Team1ID).First(&team1) - db.SQLAccess.Gorm.Where("id = ?", match.Team2ID).First(&team2) - db.SQLAccess.Gorm.Where("id = ?", match.UserID).First(&user) + db.SQLAccess.Gorm.First(&server, match.ServerID) + db.SQLAccess.Gorm.First(&team1, match.Team1ID) + db.SQLAccess.Gorm.First(&team2, match.Team2ID) + db.SQLAccess.Gorm.First(&user, match.UserID) var winner int64 if match.Winner.Valid { winner_, err := match.Winner.Value() @@ -178,7 +178,7 @@ func GetUserInfo(w http.ResponseWriter, r *http.Request) { fmt.Printf("GetUserInfo\n") userid := vars["userID"] response := APIUserData{} - db.SQLAccess.Gorm.Where("id = ?", userid).First(&response) + db.SQLAccess.Gorm.First(&response, userid) db.SQLAccess.Gorm.Where("user_id = ?", userid).Limit(20).Find(&response.Teams) db.SQLAccess.Gorm.Where("user_id = ?", userid).Limit(20).Find(&response.Servers) @@ -193,10 +193,10 @@ func GetUserInfo(w http.ResponseWriter, r *http.Request) { team2 := APITeamData{} user := APIUserData{} db.SQLAccess.Gorm.Where("match_id = ?", matches[i].ID).Limit(7).Find(&mapstats) - db.SQLAccess.Gorm.Where("id = ?", matches[i].ServerID).First(&server) - db.SQLAccess.Gorm.Where("id = ?", matches[i].Team1ID).First(&team1) - db.SQLAccess.Gorm.Where("id = ?", matches[i].Team2ID).First(&team2) - db.SQLAccess.Gorm.Where("id = ?", matches[i].UserID).First(&user) + db.SQLAccess.Gorm.First(&server, matches[i].ServerID) + db.SQLAccess.Gorm.First(&user, matches[i].UserID) + db.SQLAccess.Gorm.First(&team1, matches[i].Team1ID) + db.SQLAccess.Gorm.First(&team2, matches[i].Team2ID) var winner int64 if matches[i].Winner.Valid { winner_, err := matches[i].Winner.Value() @@ -264,7 +264,7 @@ func GetServerInfo(w http.ResponseWriter, r *http.Request) { fmt.Printf("GetServerInfo\n") serverID := vars["serverID"] response := db.GameServerData{} - db.SQLAccess.Gorm.Where("id = ?", serverID).First(&response) + db.SQLAccess.Gorm.First(&response, serverID) jsonbyte, err := json.Marshal(response) if err != nil { http.Error(w, "JSON Format invalid", http.StatusInternalServerError) @@ -280,7 +280,7 @@ func GetStatusString(w http.ResponseWriter, r *http.Request) { fmt.Printf("GetStatusString\n") matchid := vars["matchID"] response := db.MatchData{} - db.SQLAccess.Gorm.Where("id = ?", matchid).First(&response) + db.SQLAccess.Gorm.First(&response, matchid) status, err := response.GetStatusString(true) if err != nil { http.Error(w, "Failed to get status", http.StatusInternalServerError) @@ -318,11 +318,15 @@ func GetMatches(w http.ResponseWriter, r *http.Request) { func GetTeamInfo(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) fmt.Printf("GetTeamInfo\n") - teamid := vars["teamID"] + teamid, err := strconv.Atoi(vars["teamID"]) + if err != nil { + http.Error(w, "teamID should be int.", http.StatusBadRequest) + return + } response := APITeamData{} - db.SQLAccess.Gorm.Where("id = ?", teamid).First(&response) + db.SQLAccess.Gorm.First(&response, teamid) var steamids = make([]string, 5) - steamids, err := response.GetPlayers() + steamids, err = response.GetPlayers() if err != nil { http.Error(w, "Failed to get players.", http.StatusInternalServerError) return @@ -354,10 +358,10 @@ func GetRecentMatches(w http.ResponseWriter, r *http.Request) { team2 := APITeamData{} user := APIUserData{} db.SQLAccess.Gorm.Where("match_id = ?", matches[i].ID).Limit(7).Find(&mapstats) - db.SQLAccess.Gorm.Where("id = ?", matches[i].ServerID).First(&server) - db.SQLAccess.Gorm.Where("id = ?", matches[i].Team1ID).First(&team1) - db.SQLAccess.Gorm.Where("id = ?", matches[i].Team2ID).First(&team2) - db.SQLAccess.Gorm.Where("id = ?", matches[i].UserID).First(&user) + db.SQLAccess.Gorm.First(&server, matches[i].ServerID) + db.SQLAccess.Gorm.First(&team1, matches[i].Team1ID) + db.SQLAccess.Gorm.First(&team2, matches[i].Team2ID) + db.SQLAccess.Gorm.First(&user, matches[i].UserID) var winner int64 if matches[i].Winner.Valid { winner_, err := matches[i].Winner.Value() @@ -426,7 +430,7 @@ func CheckUserCanEdit(w http.ResponseWriter, r *http.Request) { teamid := vars["teamID"] useridstr := q.Get("userID") team := db.TeamData{} - db.SQLAccess.Gorm.Where("id = ?", teamid).First(&team) + db.SQLAccess.Gorm.First(&team, teamid) userid, err := strconv.Atoi(useridstr) if err != nil { http.Error(w, "userid should be int", http.StatusBadRequest) @@ -791,7 +795,7 @@ func MatchCancelHandler(w http.ResponseWriter, r *http.Request) { return } Match := db.MatchData{} - rec := db.SQLAccess.Gorm.Where("id = ?", matchid).First(&Match) + rec := db.SQLAccess.Gorm.First(&Match, matchid) if rec.RecordNotFound() { http.Error(w, "Failed to find match", http.StatusNotFound) } @@ -802,7 +806,7 @@ func MatchCancelHandler(w http.ResponseWriter, r *http.Request) { db.SQLAccess.Gorm.Save(&MatchUpdate) Server := db.GameServerData{} - db.SQLAccess.Gorm.Where("id = ?", Match.ServerID).First(&Server) + db.SQLAccess.Gorm.First(&Server, Match.ServerID) ServerUpdate := Server db.SQLAccess.Gorm.First(&ServerUpdate) ServerUpdate.InUse = false @@ -838,14 +842,14 @@ func MatchRconHandler(w http.ResponseWriter, r *http.Request) { q := r.URL.Query() command := q.Get("command") Match := db.MatchData{} - rec := db.SQLAccess.Gorm.Where("id = ?", matchid).First(&Match) + rec := db.SQLAccess.Gorm.First(&Match, matchid) if rec.RecordNotFound() { http.Error(w, "Failed to find match", http.StatusNotFound) return } Server := db.GameServerData{} - rec = db.SQLAccess.Gorm.Where("id = ?", Match.ServerID).First(&Server) + rec = db.SQLAccess.Gorm.First(&Server, Match.ServerID) if rec.RecordNotFound() { http.Error(w, "Failed to find server", http.StatusNotFound) return @@ -886,13 +890,13 @@ func MatchPauseHandler(w http.ResponseWriter, r *http.Request) { return } Match := db.MatchData{} - rec := db.SQLAccess.Gorm.Where("id = ?", matchid).First(&Match) + rec := db.SQLAccess.Gorm.First(&Match, matchid) if rec.RecordNotFound() { http.Error(w, "Failed to find match", http.StatusNotFound) } Server := db.GameServerData{} - rec = db.SQLAccess.Gorm.Where("id = ?", Match.ServerID).First(&Server) + rec = db.SQLAccess.Gorm.First(&Server, Match.ServerID) if rec.RecordNotFound() { http.Error(w, "Failed to find server", http.StatusNotFound) } @@ -926,13 +930,13 @@ func MatchUnpauseHandler(w http.ResponseWriter, r *http.Request) { return } Match := db.MatchData{} - rec := db.SQLAccess.Gorm.Where("id = ?", matchid).First(&Match) + rec := db.SQLAccess.Gorm.First(&Match, matchid) if rec.RecordNotFound() { http.Error(w, "Failed to find match", http.StatusNotFound) } Server := db.GameServerData{} - rec = db.SQLAccess.Gorm.Where("id = ?", Match.ServerID).First(&Server) + rec = db.SQLAccess.Gorm.First(&Server, Match.ServerID) if rec.RecordNotFound() { http.Error(w, "Failed to find server", http.StatusNotFound) } @@ -979,14 +983,14 @@ func MatchAddUserHandler(w http.ResponseWriter, r *http.Request) { return } Match := db.MatchData{} - rec := db.SQLAccess.Gorm.Where("id = ?", matchid).First(&Match) + rec := db.SQLAccess.Gorm.First(&Match, matchid) if rec.RecordNotFound() { http.Error(w, "Failed to find match", http.StatusNotFound) return } Server := db.GameServerData{} - rec = db.SQLAccess.Gorm.Where("id = ?", Match.ServerID).First(&Server) + rec = db.SQLAccess.Gorm.First(&Server, Match.ServerID) if rec.RecordNotFound() { http.Error(w, "Failed to find server", http.StatusNotFound) return @@ -1027,14 +1031,14 @@ func MatchListBackupsHandler(w http.ResponseWriter, r *http.Request) { return } Match := db.MatchData{} - rec := db.SQLAccess.Gorm.Where("id = ?", matchid).First(&Match) + rec := db.SQLAccess.Gorm.First(&Match, matchid) if rec.RecordNotFound() { http.Error(w, "Failed to find match", http.StatusNotFound) return } Server := db.GameServerData{} - rec = db.SQLAccess.Gorm.Where("id = ?", Match.ServerID).First(&Server) + rec = db.SQLAccess.Gorm.First(&Server, Match.ServerID) if rec.RecordNotFound() { http.Error(w, "Failed to find server", http.StatusNotFound) return @@ -1085,14 +1089,14 @@ func MatchLoadBackupsHandler(w http.ResponseWriter, r *http.Request) { return } Match := db.MatchData{} - rec := db.SQLAccess.Gorm.Where("id = ?", matchid).First(&Match) + rec := db.SQLAccess.Gorm.First(&Match, matchid) if rec.RecordNotFound() { http.Error(w, "Failed to find match", http.StatusNotFound) return } Server := db.GameServerData{} - rec = db.SQLAccess.Gorm.Where("id = ?", Match.ServerID).First(&Server) + rec = db.SQLAccess.Gorm.First(&Server, Match.ServerID) if rec.RecordNotFound() { http.Error(w, "Failed to find server", http.StatusNotFound) return diff --git a/server/src/api/get5.go b/server/src/api/get5.go index bd29191b..d95c7345 100644 --- a/server/src/api/get5.go +++ b/server/src/api/get5.go @@ -16,7 +16,7 @@ func MatchConfigHandler(w http.ResponseWriter, r *http.Request) { fmt.Printf("MatchConfigHandler\n") matchid := vars["matchID"] match := db.MatchData{} - db.SQLAccess.Gorm.Where("id = ?", matchid).First(&match) + db.SQLAccess.Gorm.First(&match, matchid) res, err := match.BuildMatchDict() if err != nil { http.Error(w, "Internal ERROR", http.StatusInternalServerError) @@ -53,7 +53,7 @@ func MatchFinishHandler(w http.ResponseWriter, r *http.Request) { fmt.Printf("winner : %s\n", winner) fmt.Printf("forfeit : %s\n", forfeit) var Match = db.MatchData{} - db.SQLAccess.Gorm.Where("id = ?", matchid).First(&Match) + db.SQLAccess.Gorm.First(&Match, matchid) fmt.Printf("Requested API key : %s\n", r.FormValue("key")) fmt.Printf("Real API key : %s\n", Match.APIKey) var MatchUpdate = Match @@ -83,7 +83,10 @@ func MatchFinishHandler(w http.ResponseWriter, r *http.Request) { } } MatchUpdate.EndTime.Scan(time.Now()) - server := MatchUpdate.GetServer() + server, err := MatchUpdate.GetServer() + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + } serverUpdate := server db.SQLAccess.Gorm.First(&serverUpdate) serverUpdate.InUse = false @@ -111,7 +114,7 @@ func MatchMapStartHandler(w http.ResponseWriter, r *http.Request) { mapname := r.FormValue("mapname") fmt.Printf("mapname : %s\n", mapname) var m = db.MatchData{} - db.SQLAccess.Gorm.Where("id = ?", matchid).First(&m) + db.SQLAccess.Gorm.First(&m, matchid) err = MatchAPICheck(&m, r) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) @@ -161,7 +164,7 @@ func MatchMapUpdateHandler(w http.ResponseWriter, r *http.Request) { fmt.Printf("team2score : %d\n", team2score) var m = db.MatchData{} - db.SQLAccess.Gorm.Where("id = ?", matchid).First(&m) + db.SQLAccess.Gorm.First(&m, matchid) err = MatchAPICheck(&m, r) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) @@ -204,7 +207,7 @@ func MatchMapFinishHandler(w http.ResponseWriter, r *http.Request) { fmt.Printf("winner : %s\n", winner) m := db.MatchData{} - db.SQLAccess.Gorm.Where("id = ?", matchid).First(&m) + db.SQLAccess.Gorm.First(&m, matchid) mUpdate := m db.SQLAccess.Gorm.First(&mUpdate) err = MatchAPICheck(&m, r) @@ -423,7 +426,7 @@ func MatchMapPlayerUpdateHandler(w http.ResponseWriter, r *http.Request) { fmt.Printf("firstdeath_ct : %d\n", FormFirstDeathCT) var m = db.MatchData{} - db.SQLAccess.Gorm.Where("id = ?", matchid).First(&m) + db.SQLAccess.Gorm.First(&m, matchid) if m.APIKey != r.FormValue("key") { http.Error(w, "Wrong API Key", http.StatusBadRequest) return @@ -501,7 +504,7 @@ func MatchVetoUpdateHandler(w http.ResponseWriter, r *http.Request) { fmt.Printf("FormPickOrVeto : %s\n", FormPickOrVeto) m := &db.MatchData{} - db.SQLAccess.Gorm.Where("id = ?", matchid).First(&m) + db.SQLAccess.Gorm.First(&m,matchid) err = MatchAPICheck(m, r) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) @@ -546,7 +549,7 @@ func MatchDemoUploadHandler(w http.ResponseWriter, r *http.Request) { DemoFile := r.FormValue("demoFile") m := &db.MatchData{} - db.SQLAccess.Gorm.Where("id = ?", matchid).First(&m) + db.SQLAccess.Gorm.First(&m,matchid) err = MatchAPICheck(m, r) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) diff --git a/server/src/db/models.go b/server/src/db/models.go index 1fab7501..8e2741ba 100644 --- a/server/src/db/models.go +++ b/server/src/db/models.go @@ -148,7 +148,7 @@ func (g *GameServerData) Create(userid int, displayname string, ipstring string, // CanEdit Check if server is editable for user or not. func (g *GameServerData) CanEdit(userid int) bool { if g.UserID == 0 { - SQLAccess.Gorm.Where("id = ?", g.ID).First(&g) + SQLAccess.Gorm.First(&g, g.ID) } if userid == 0 { return false @@ -167,7 +167,10 @@ func (g *GameServerData) Edit() (*GameServerData, error) { return nil, fmt.Errorf("ID not valid") } Server := GameServerData{} - SQLAccess.Gorm.Where("id = ?", g.ID).First(&Server) + rec := SQLAccess.Gorm.First(&Server, g.ID) + if rec.RecordNotFound() { + return nil, fmt.Errorf("Server not found") + } SQLAccess.Gorm.Model(&Server).Update(&g) SQLAccess.Gorm.Save(&g) return g, nil @@ -178,7 +181,7 @@ func (g *GameServerData) Delete() error { if g.ID == 0 { return fmt.Errorf("ID not valid") } - rec := SQLAccess.Gorm.Where("id = ?", g.ID).First(&g) + rec := SQLAccess.Gorm.First(&g, g.ID) if rec.RecordNotFound() { return fmt.Errorf("Server not found") } @@ -267,7 +270,10 @@ func (t *TeamData) Edit() (*TeamData, error) { return nil, fmt.Errorf("ID not valid") } Team := TeamData{} - SQLAccess.Gorm.Where("id = ?", t.ID).First(&Team) + rec := SQLAccess.Gorm.First(&Team, t.ID) + if rec.RecordNotFound() { + return nil, fmt.Errorf("Team not found") + } SQLAccess.Gorm.Model(&Team).Update(&t) SQLAccess.Gorm.Save(&t) return t, nil @@ -278,7 +284,7 @@ func (t *TeamData) Delete() error { if t.ID == 0 { return fmt.Errorf("ID not valid") } - rec := SQLAccess.Gorm.Where("id = ?", t.ID).Delete(&t) + rec := SQLAccess.Gorm.Delete(&t, t.ID) if rec.RecordNotFound() { return fmt.Errorf("Team not found") } @@ -288,7 +294,7 @@ func (t *TeamData) Delete() error { // CanEdit Check if server is editable for user or not. func (t *TeamData) CanEdit(userid int) bool { if t.UserID == 0 { - SQLAccess.Gorm.Where("id = ?", t.ID).First(&t) + SQLAccess.Gorm.First(&t, t.ID) } if userid == 0 { return false @@ -369,7 +375,7 @@ func (t *TeamData) GetRecentMatches(limit int) []MatchData { SQLAccess.Gorm.Where("team1_id = ?", t.ID).Or("team2_id = ?", t.ID).Not("start_time = null AND cancelled = true").Limit(limit).Find(&matches) } else { var owner UserData - SQLAccess.Gorm.Where("id = ?", t.UserID).First(&owner) + SQLAccess.Gorm.First(&owner, t.UserID) SQLAccess.Gorm.Where("user_id = ?", t.UserID).Find(&owner.Matches).Limit(limit) matches = owner.Matches } @@ -382,15 +388,15 @@ func (t *TeamData) GetVSMatchResult(matchid int) (string, error) { myscore := 0 otherteamscore := 0 var match MatchData - SQLAccess.Gorm.Where("id = ?", matchid).First(&match) + SQLAccess.Gorm.First(&match, matchid) if int(match.Team1ID) == t.ID { myscore = match.Team1Score otherteamscore = match.Team2Score - SQLAccess.Gorm.Where("id = ?", match.Team2ID).First(&otherteam) + SQLAccess.Gorm.First(&otherteam, match.Team2ID) } else { myscore = match.Team2Score otherteamscore = match.Team1Score - SQLAccess.Gorm.Where("id = ?", match.Team2ID).First(&otherteam) + SQLAccess.Gorm.First(&otherteam, match.Team2ID) } // for a bo1 replace series score with the map score... @@ -638,16 +644,19 @@ func (m *MatchData) Live() bool { } // GetServer Get match server ID as GameServerData -func (m *MatchData) GetServer() *GameServerData { - SQLAccess.Gorm.Where("id = ?", m.ServerID).First(&m.Server) - return &m.Server +func (m *MatchData) GetServer() (*GameServerData, error) { + rec := SQLAccess.Gorm.First(&m.Server, m.ServerID) + if rec.RecordNotFound() { + return nil, fmt.Errorf("Server not found") + } + return &m.Server, nil } // GetCurrentScore Returns current match score. returns map-score if match is BO1. func (m *MatchData) GetCurrentScore(g *gorm.DB) (int, int) { //g.First(&m).Association("MapStats").Find(&m) m.MapStats = []MapStatsData{} - g.First(&m.MapStats, "match_id = ?", m.ID) + g.First(&m.MapStats, m.ID) if m.MaxMaps == 1 { if len(m.MapStats) == 0 { // check ok? return 0, 0 @@ -661,7 +670,7 @@ func (m *MatchData) GetCurrentScore(g *gorm.DB) (int, int) { func (m *MatchData) GetTeam1() (TeamData, error) { var Team = TeamData{} var STeam TeamData - SQLAccess.Gorm.Where("id = ?", m.Team1ID).First(&STeam) + SQLAccess.Gorm.First(&STeam, m.Team1ID) Team.ID = STeam.ID Team.Name = STeam.Name Team.Tag = STeam.Tag @@ -681,7 +690,7 @@ func (m *MatchData) GetTeam1() (TeamData, error) { func (m *MatchData) GetTeam2() (TeamData, error) { var Team = TeamData{} var STeam TeamData - SQLAccess.Gorm.Where("id = ?", m.Team2ID).First(&STeam) + SQLAccess.Gorm.First(&STeam, m.Team2ID) Team.ID = STeam.ID Team.Name = STeam.Name Team.Tag = STeam.Tag @@ -699,7 +708,7 @@ func (m *MatchData) GetTeam2() (TeamData, error) { // GetUser Get Match owner as "UserData" struct. func (m *MatchData) GetUser() UserData { - SQLAccess.Gorm.Where("id = ?", m.UserID).First(&m.User) + SQLAccess.Gorm.First(&m.User, m.UserID) return m.User } @@ -758,7 +767,7 @@ func (m *MatchData) SendToServer() error { // BuildMatchDict Builds match JSON data. func (m *MatchData) BuildMatchDict() (*MatchConfig, error) { - SQLAccess.Gorm.Where("id = ?", m.ID).First(&m) + SQLAccess.Gorm.First(&m, m.ID) m.VetoMapPoolJSON = strings.Split(m.VetoMapPool, " ") team1, err := m.GetTeam1() team2, err := m.GetTeam2() @@ -844,7 +853,7 @@ func (m *MapStatsData) TableName() string { // GetOrCreate Get or register mapstats data. func (m *MapStatsData) GetOrCreate(matchID int, MapNumber int, mapname string) (*MapStatsData, error) { Match := MatchData{} - MatchRecord := SQLAccess.Gorm.Where("id = ?", matchID).First(&Match) + MatchRecord := SQLAccess.Gorm.First(&Match, matchID) if MatchRecord.RecordNotFound() { return nil, fmt.Errorf("Match not found") } diff --git a/web/src/components/Team.vue b/web/src/components/Team.vue index 773f442d..3b0dafb4 100644 --- a/web/src/components/Team.vue +++ b/web/src/components/Team.vue @@ -84,8 +84,8 @@ export default { } const loggedin = await this.axios.get('/api/v1/CheckLoggedIn') this.user = loggedin.data - this.Editable = this.CheckTeamEditable(this.user.userid) - this.Deletable = this.CheckTeamDeletable(this.user.userid) + this.Editable = this.CheckTeamEditable(this.user.user_id) + this.Deletable = this.CheckTeamDeletable(this.user.user_id) }, methods: { async GetTeamData (teamid) { diff --git a/web/src/components/Teams.vue b/web/src/components/Teams.vue index de547013..c5ae168b 100644 --- a/web/src/components/Teams.vue +++ b/web/src/components/Teams.vue @@ -1,7 +1,7 @@