Skip to content

Commit

Permalink
extract sql parameterization
Browse files Browse the repository at this point in the history
  * return an empty array when no results are found
  • Loading branch information
audibleblink committed Aug 21, 2020
1 parent f8f5bc3 commit 7598514
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func handleUsername(w http.ResponseWriter, r *http.Request) {
JSONError(w, err, http.StatusInternalServerError)
return
}
resultWriter(w, &records)
resultWriter(w, records)
}

func handlePassword(w http.ResponseWriter, r *http.Request) {
Expand All @@ -103,7 +103,7 @@ func handlePassword(w http.ResponseWriter, r *http.Request) {
JSONError(w, err, http.StatusInternalServerError)
return
}
resultWriter(w, &records)
resultWriter(w, records)
}

func handleDomain(w http.ResponseWriter, r *http.Request) {
Expand All @@ -113,7 +113,7 @@ func handleDomain(w http.ResponseWriter, r *http.Request) {
JSONError(w, err, http.StatusInternalServerError)
return
}
resultWriter(w, &records)
resultWriter(w, records)
}

func handleEmail(w http.ResponseWriter, r *http.Request) {
Expand All @@ -123,7 +123,7 @@ func handleEmail(w http.ResponseWriter, r *http.Request) {
JSONError(w, err, http.StatusBadRequest)
return
}
resultWriter(w, &records)
resultWriter(w, records)
}

func handleBreaches(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -173,30 +173,38 @@ func recordsByEmail(email string) (records []*record, err error) {
err = fmt.Errorf("invalid email format")
return
}
username, domain := usernameAndDomain[0], usernameAndDomain[1]

queryString := fmt.Sprintf(
`SELECT DISTINCT * FROM %s WHERE username = @username AND domain = @domain`,
bigQueryTable,
)
query := bq.Query(queryString)

query.QueryConfig.Parameters = []bigquery.QueryParameter{
bigquery.QueryParameter{"username", username},
bigquery.QueryParameter{"domain", domain},
params := map[string]string {
"username": usernameAndDomain[0],
"domain": usernameAndDomain[1],
}
query := parameterize(queryString, params)
return queryRecords(query)
}

func recordsBy(column, value string) (records []*record, err error) {
queryString := fmt.Sprintf(`SELECT DISTINCT * FROM %s WHERE %s = @%s`, bigQueryTable, column, column)
query := bq.Query(queryString)
query.QueryConfig.Parameters = []bigquery.QueryParameter{
bigquery.QueryParameter{ Name: column, Value: value},
}
params := map[string]string {column: value}
query := parameterize(queryString, params)
return queryRecords(query)
}

func parameterize(q string, fields map[string]string) *bigquery.Query {
var params []bigquery.QueryParameter
for key, value := range fields {
param := bigquery.QueryParameter{Name: key, Value: value}
params = append(params, param)
}
query := bq.Query(q)
query.QueryConfig.Parameters = params
return query
}

func queryRecords(query *bigquery.Query) (records []*record, err error) {
ctx := context.Background()
results, err := query.Read(ctx)
Expand All @@ -219,12 +227,16 @@ func queryRecords(query *bigquery.Query) (records []*record, err error) {
return
}

func resultWriter(w http.ResponseWriter, records *[]*record) {
func resultWriter(w http.ResponseWriter, records []*record) {
if records == nil{
records = make([]*record, 0)
}
resultJson, err := json.Marshal(records)
if err != nil {
JSONError(w, err, http.StatusInternalServerError)
return
}

w.Write(resultJson)
}

Expand Down

0 comments on commit 7598514

Please sign in to comment.