my function receives a http request and sends back json string as http response. This is my Code:
func homePage(res http.ResponseWriter, req *http.Request) {
type Resp struct {
Result []map[string]interface{} `json:"result,omitempty"`
Status string `json:"status"`
}
type Inputs struct {
ShopID string `json:"ShopID"`
DeviceID string `json:"DeviceID"`
EmpID string `json:"EmpID"`
Token string `json:"Token"`
Version string `json:"Version"`
}
var Response Resp
Response.Status = "failed"
db, err := sql.Open("mysql", "root:password@tcp(127.0.0.1:3306)/b2b")
if err != nil {
panic(err.Error())
}
defer db.Close()
rnd := render.New()
b, err := ioutil.ReadAll(req.Body)
defer req.Body.Close()
if err != nil {
panic(err.Error())
}
// Unmarshal the request body
var msg Inputs
err = json.Unmarshal(b, &msg)
if err != nil {
panic(err.Error())
}
bookingrows, bookingerr := db.Query("SELECT DISTINCT b2b_booking_id,b2b_check_in_report,b2b_vehicle_at_garage,b2b_service_under_progress,b2b_vehicle_ready,b2b_vehicle_delivered FROM b2b_booking_tbl WHERE b2b_flag=0 and b2b_shop_id=?", msg.ShopID)
if bookingerr != nil {
panic(bookingerr.Error())
} else {
Result := make(map[string]interface{})
for bookingrows.Next() {
var bookingid, checkinreported, vehicleingarage, serviceinprogress, vehicleready, vehicledelivered string
err = bookingrows.Scan(&bookingid, &checkinreported, &vehicleingarage, &serviceinprogress, &vehicleready, &vehicledelivered)
type Output struct {
checkins int `json:"checkins"`
inprogress int `json:"inprogress"`
ready int `json:"ready"`
completed int `json:"completed"`
}
var output Output
if err != nil {
panic(err.Error())
Response.Status = "failed"
}
if vehicledelivered == "1" {
output.completed = output.completed + 1
}
if vehicledelivered == "0" && vehicleready == "1" {
output.ready = output.ready + 1
}
if vehicledelivered == "0" && vehicleready == "0" && serviceinprogress == "1" {
output.inprogress = output.inprogress + 1
}
if vehicledelivered == "0" && vehicleready == "0" && serviceinprogress == "0" && (checkinreported == "1" || vehicleingarage == "1") {
output.checkins = output.checkins + 1
}
values := make([]string, 4)
val := reflect.ValueOf(output)
for i := range values {
//Result[usercolumns[i]] = fmt.Sprintf("%s", v)
Result[val.Type().Field(i).Tag.Get("json")] = fmt.Sprintf("%v", i)
}
}
Response.Result = append(Response.Result, Result)
Response.Status = "success"
}
res.Header().Set("Content-Type", "application/json")
rnd.JSON(res, http.StatusOK, Response)
}
Im getting the desired output when the input values are correct and the corresponding row exists in Db. but when the input is wrong or the query fails, i should return only the status as "failed" but my output contains the result [] which is empty like this.
{
"result": [
{}
],
"status": "success"
}
I tried checking Result == nil and add the array only on true. but this doesn't help me. it might be because the struct is not nill. I need some help to to find the issue and solve it.
Trying checking the length of the Result
array before appending it to output
Something like this
if (len(Result) != 0) {
Response.Result = append(Response.Result, Result)
}
Maybe you also need to write this also Result := make(map[string]interface{})
inside the if condition, before the append statement