I'm trying to refactor my code but I am stuck.
I want to clean this HandleExportDevicesAsCsv
function. So, decided to code from where I start my comment to the end of the comment. Then I refactored and moved that block of code to another function.
Can anyone guide me on returning the value from ExportDevicesAsCsv
so that I can pass it to c.Data(http.StatusOK, "text/csv", b.Bytes())
this function working correctly
func HandleExportDevicesAsCsv(c *gin.Context) {
deviceIDs := c.Request.FormValue("deviceIDs")
var data []byte
token := c.Request.Header.Get("Authorization")
data = services.ExportDevicesData(token, deviceIDs)
//===================================================================
//I want to refactor this code from here
//====================================================================
type DR []models.DeviceResponse
var deviceResponse DR
json.Unmarshal([]byte(data), &deviceResponse)
b := &bytes.Buffer{}
w := csv.NewWriter(b)
if err := w.Write([]string{"Name", "Id", "Createdon","Lastupdated", "qrcode", "mac", "state", "kind"});
err != nil {
log.Fatalln("error writing record to csv:", err)
}
for _, device := range deviceResponse {
var record []string
record = append(record, device.Name)
record = append(record, device.ID)
record = append(record, device.Createdon)
record = append(record, device.Lastupdated)
record = append(record, device.Qrcode)
record = append(record, device.State)
record = append(record, device.Kind)
if err := w.Write(record); err != nil {
log.Fatalln("error writing record to csv:", err)
}
}
w.Flush()
if err := w.Error();
err != nil {
log.Fatal(err)
}
//===================================================================
// to here end of refactor code
//====================================================================
c.Header("Content-type", "application/octet-stream")
c.Header("Content-Disposition", "attachment; filename=devices.csv")
c.Data(http.StatusOK, "text/csv", b.Bytes())
}
when I refactor function this is not working.
func HandleExportDevicesAsCsv(c *gin.Context) {
METHOD_NAME := "HandleExportDevicesAsCsv() - "
mlog.Debug("%s request = %v", METHOD_NAME, c.Request)
deviceIDs := c.Request.FormValue("deviceIDs")
mlog.Debug("%s deviceIDs = %s", METHOD_NAME, deviceIDs)
var data []byte
token := c.Request.Header.Get("Authorization")
data = services.ExportDevicesData(token, deviceIDs)
cdata := services.ExportDevicesAsCsv(data)
ccdata := cdata.Bytes()
mlog.Debug("%s response = %s", METHOD_NAME, string(data))
c.Header("Content-type", "application/octet-stream")
c.Header("Content-Disposition", "attachment; filename=devices.csv")
//c.Data(http.StatusOK, "application/octet-stream", data) // text/csv
// c.Header("Content-Description", "File Transfer")
// c.Header("Content-Disposition", "attachment; filename=devices.csv")
c.Data(http.StatusOK, "text/csv", ccdata)
// c.Header("Content-Type", "text/csv")
// c.Writer.Write(b.Bytes())
// c.Writer.Flush()
}
Here I refactor and put that code in another function and call in HandleExportDevicesAsCsv
func ExportDevicesAsCsv(data []byte) *bytes.Buffer {
//METHOD_NAME := "ExportDevicesAsCsv() - "
//var Handlersdata handlers.data
//var csvVal []byte
//var devices []interface{}
//var nextPageToken string
//for len(nextPageToken) > 0 {
type DR []models.DeviceResponse
var deviceResponse DR
json.Unmarshal([]byte(data), &deviceResponse)
b := &bytes.Buffer{}
w := csv.NewWriter(b)
if err := w.Write([]string{"Name", "Id", "Createdon",
"Lastupdated", "qrcode", "mac", "state", "kind"}); err != nil {
log.Fatalln("error writing record to csv:", err)
}
for _, device := range deviceResponse {
var record []string
record = append(record, device.Name)
record = append(record, device.ID)
record = append(record, device.Createdon)
record = append(record, device.Lastupdated)
record = append(record, device.Qrcode)
record = append(record, device.State)
record = append(record, device.Kind)
if err := w.Write(record); err != nil {
log.Fatalln("error writing record to csv:", err)
}
}
w.Flush()
if err := w.Error(); err != nil {
log.Fatal(err)
}
// c.Header("Content-Description", "File Transfer")
// c.Header("Content-Disposition", "attachment; filename=devices.csv")
// c.Data(http.StatusOK, "text/csv", b.Bytes())
return b
}