2
I need to do an api where query using optional query string parameters.
I tried something like:
func (app *App) getFunctionarys(w http.ResponseWriter, r *http.Request) {
v := r.URL.Query()
Id := v.Get("Id")
Name := v.Get("Name")
Cpf := v.Get("Cpf")
Role:= v.Get("Role")
Status := v.Get("Status")
dbdata := &Funcionary{}
err := app.Database.QueryRow("SELECT Id,DataCad,Role,Cpf,Name,Uf,Salary,Status FROM `funcionary` WHERE Id = ? OR Name= ? OR Cpf = ? OR Role= ? OR Status = ?", Id, Name, Cpf, Role, Status).Scan(&dbdata.Id, &dbdata.DataCad, &dbdata.Role, &dbdata.Cpf, &dbdata.Name, &dbdata.Uf, &dbdata.Salary, &dbdata.Status)
if err != nil {
log.Println(err)
}
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(dbdata); err != nil {
panic(err)
}
}
I have 5 records, but my api is only returning the first record, regardless of which query strings I put. How can I make an api that performs a Where based on the optional parameters received from the request?