Updated to allow for a single query to the db with multiple columns, and a conversion to allow string outputs instead of sql.NullStrings

This commit is contained in:
Opaque02 2024-09-30 00:27:19 +10:00
parent a4becb36bd
commit ffaf455695
2 changed files with 48 additions and 61 deletions

View File

@ -759,15 +759,6 @@ func handleAdminDiscordUnlink(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}
// this is for the output for the admin search, but should probably be moved elsewhere, though not sure where
// account/info has its own version under api/account/info.ts, but not sure if we want a new folder/file for admin stuff or to put it elsewhere?
type AdminSearchResponse struct {
Username string `json:"username"`
DiscordId string `json:"discordId"`
GoogleId string `json:"googleId"`
LastLoggedIn string `json:"lastLoggedIn"`
}
func handleAdminSearch(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
@ -800,59 +791,21 @@ func handleAdminSearch(w http.ResponseWriter, r *http.Request) {
username := r.Form.Get("username")
log.Printf("USERNAME SEARCH STARTING")
// this does a quick call to make sure the username exists on the server before allowing the rest of the code to run
// this calls error value 204 (StatusNoContent) if there's no data; this means the username does not exist in the server
_, err = db.CheckUsernameExists(username)
if err != nil {
httpError(w, r, fmt.Errorf("username does not exist on the server"), http.StatusNoContent)
return
}
// this way does a single call that does a query for multiple columns from our database and makes an object out of it, which is returned to us
// this does a single call that does a query for multiple columns from our database and makes an object out of it, which is returned to us
adminSearchResult, err := db.FetchAdminDetailsByUsername(username)
if err != nil {
httpError(w, r, err, http.StatusInternalServerError)
return
}
log.Printf("Username is: %s", adminSearchResult.Username.String)
writeJSON(w, r, adminSearchResult)
// this way does multiple calls to get individual things (for example, a single call for username, a single call for discord Id, a single call for google Id etc)
// once we have all the single fields we need, it then makes an object out of them with the info that we want
/*
dbUsername, err := db.CheckUsernameExists(username)
if err != nil {
httpError(w, r, err, http.StatusInternalServerError)
return
}
log.Printf("Username is: %s", dbUsername)
discordId, err := db.FetchDiscordIdByUsername(username)
if err != nil {
httpError(w, r, err, http.StatusUnauthorized)
return
}
log.Printf("Discord Id is: %s", discordId)
googleId, err := db.FetchGoogleIdByUsername(username)
if err != nil {
httpError(w, r, err, http.StatusUnauthorized)
return
}
log.Printf("Google Id is: %s", googleId)
lastLoggedIn, err := db.FetchLastLoggedInDateByUsername(username)
if err != nil {
httpError(w, r, err, http.StatusUnauthorized)
return
}
log.Printf("Last Logged in date is: %s", lastLoggedIn)
adminResponse := AdminSearchResponse{
Username: username,
DiscordId: discordId,
GoogleId: googleId,
LastLoggedIn: lastLoggedIn,
}
writeJSON(w, r, adminResponse)
*/
log.Printf("%s: %s searched for username %s", userDiscordId, r.URL.Path, username)
}

View File

@ -179,19 +179,53 @@ func FetchLastLoggedInDateByUsername(username string) (string, error) {
}
type AdminSearchResponse struct {
Username sql.NullString `json:"username"`
DiscordId sql.NullString `json:"discordId"`
GoogleId sql.NullString `json:"googleId"`
LastLoggedIn sql.NullString `json:"lastLoggedIn"`
Username string `json:"username"`
DiscordId string `json:"discordId"`
GoogleId string `json:"googleId"`
LastLoggedIn string `json:"lastLoggedIn"`
}
func FetchAdminDetailsByUsername(dbUsername string) (AdminSearchResponse, error) {
var resultUsername, resultDiscordId, resultGoogleId, resultLastLoggedIn sql.NullString
var username, discordId, googleId, lastLoggedIn string
var adminResponse AdminSearchResponse
err := handle.QueryRow("SELECT username, discordId, googleId, lastLoggedIn from accounts WHERE username = ?", dbUsername).Scan(&adminResponse.Username, &adminResponse.DiscordId, &adminResponse.GoogleId, &adminResponse.LastLoggedIn)
err := handle.QueryRow("SELECT username, discordId, googleId, lastLoggedIn from accounts WHERE username = ?", dbUsername).Scan(&resultUsername, &resultDiscordId, &resultGoogleId, &resultLastLoggedIn)
if err != nil {
return adminResponse, err
}
if resultUsername.Valid {
username = resultUsername.String
} else {
username = ""
}
if resultDiscordId.Valid {
discordId = resultDiscordId.String
} else {
discordId = ""
}
if resultGoogleId.Valid {
googleId = resultGoogleId.String
} else {
googleId = ""
}
if resultLastLoggedIn.Valid {
lastLoggedIn = resultLastLoggedIn.String
} else {
lastLoggedIn = ""
}
adminResponse = AdminSearchResponse{
Username: username,
DiscordId: discordId,
GoogleId: googleId,
LastLoggedIn: lastLoggedIn,
}
return adminResponse, nil
}