Skip to content

Commit

Permalink
Merge pull request #105 from pzaino/develop
Browse files Browse the repository at this point in the history
Minor code refactoring
  • Loading branch information
pzaino authored Feb 16, 2024
2 parents bd9a133 + 4d612a4 commit a5ec028
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions services/api/search_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ import (
_ "github.com/lib/pq"
)

const (
sqlQueryLabel = "SQL query: %s"
sqlQueryParamsLabel = "SQL params: %v"
dbConnErrorLabel = "Error connecting to the database: %v"
SearchLabel = "Performing search for: %s"
)

func tokenize(input string) []string {
var tokens []string
var currentToken strings.Builder
Expand Down Expand Up @@ -263,12 +270,12 @@ func performSearch(query string) (SearchResult, error) {
// Connect to the database
err = db.Connect(config)
if err != nil {
cmn.DebugMsg(cmn.DbgLvlError, "Error connecting to the database: %v", err)
cmn.DebugMsg(cmn.DbgLvlError, dbConnErrorLabel, err)
return SearchResult{}, err
}
defer db.Close()

cmn.DebugMsg(cmn.DbgLvlDebug, "Performing search for: %s", query)
cmn.DebugMsg(cmn.DbgLvlDebug, SearchLabel, query)

// Prepare the query body
var queryBody string
Expand Down Expand Up @@ -307,8 +314,8 @@ func performSearch(query string) (SearchResult, error) {
if err != nil {
return SearchResult{}, err
}
cmn.DebugMsg(cmn.DbgLvlDebug1, "SQL query: %s", sqlQuery)
cmn.DebugMsg(cmn.DbgLvlDebug1, "SQL params: %v", sqlParams)
cmn.DebugMsg(cmn.DbgLvlDebug1, sqlQueryLabel, sqlQuery)
cmn.DebugMsg(cmn.DbgLvlDebug1, sqlQueryParamsLabel, sqlParams)

// Execute the query
rows, err := db.ExecuteQuery(sqlQuery, sqlParams...)
Expand Down Expand Up @@ -350,12 +357,12 @@ func performScreenshotSearch(query string, qType int) (ScreenshotResponse, error
// Connect to the database
err = db.Connect(config)
if err != nil {
cmn.DebugMsg(cmn.DbgLvlError, "Error connecting to the database: %v", err)
cmn.DebugMsg(cmn.DbgLvlError, dbConnErrorLabel, err)
return ScreenshotResponse{}, err
}
defer db.Close()

cmn.DebugMsg(cmn.DbgLvlDebug, "Performing search for: %s", query)
cmn.DebugMsg(cmn.DbgLvlDebug, SearchLabel, query)

// Parse the user input
var sqlQuery string
Expand All @@ -373,8 +380,8 @@ func performScreenshotSearch(query string, qType int) (ScreenshotResponse, error
return ScreenshotResponse{}, err
}
}
cmn.DebugMsg(cmn.DbgLvlDebug1, "SQL query: %s", sqlQuery)
cmn.DebugMsg(cmn.DbgLvlDebug1, "SQL params: %v", sqlParams)
cmn.DebugMsg(cmn.DbgLvlDebug1, sqlQueryLabel, sqlQuery)
cmn.DebugMsg(cmn.DbgLvlDebug1, sqlQueryParamsLabel, sqlParams)

// Execute the query
rows, err := db.ExecuteQuery(sqlQuery, sqlParams...)
Expand Down Expand Up @@ -489,12 +496,12 @@ func performNetInfoSearch(query string, qType int) (NetInfoResponse, error) {
// Connect to the database
err = db.Connect(config)
if err != nil {
cmn.DebugMsg(cmn.DbgLvlError, "Error connecting to the database: %v", err)
cmn.DebugMsg(cmn.DbgLvlError, dbConnErrorLabel, err)
return NetInfoResponse{}, err
}
defer db.Close()

cmn.DebugMsg(cmn.DbgLvlDebug, "Performing search for: %s", query)
cmn.DebugMsg(cmn.DbgLvlDebug, SearchLabel, query)

// Parse the user input
var sqlQuery string
Expand All @@ -512,8 +519,8 @@ func performNetInfoSearch(query string, qType int) (NetInfoResponse, error) {
return NetInfoResponse{}, err
}
}
cmn.DebugMsg(cmn.DbgLvlDebug1, "SQL query: %s", sqlQuery)
cmn.DebugMsg(cmn.DbgLvlDebug1, "SQL params: %v", sqlParams)
cmn.DebugMsg(cmn.DbgLvlDebug1, sqlQueryLabel, sqlQuery)
cmn.DebugMsg(cmn.DbgLvlDebug1, sqlQueryParamsLabel, sqlParams)

// Execute the query
rows, err := db.ExecuteQuery(sqlQuery, sqlParams...)
Expand Down

0 comments on commit a5ec028

Please sign in to comment.