package controllers import ( "encoding/json" "log" "net/http" "github.com/gin-gonic/gin" "gitlab.com/pactual1/backend/models" "gitlab.com/pactual1/backend/shared" ) func SaveDeviceInfofunc(c *gin.Context) { var deviceInfo models.DeviceInfo rawData, _ := c.GetRawData() // Unmarshal to the important info structure err := json.Unmarshal(rawData, &deviceInfo) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid JSON payload"}) return } // Unmarshal to the deviceInfo structure err = json.Unmarshal(rawData, &deviceInfo) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid JSON payload"}) return } deviceInfo.RawJSON = string(rawData) // Attempt to find the device by IMEI; if found, set the DeviceID var device models.Device if err := shared.GetDb().Where("imei = ?", deviceInfo.IMEI).First(&device).Error; err == nil { deviceInfo.DeviceID = device.ID } else { log.Printf("Could not find device with imei %v", deviceInfo.IMEI) } // Save deviceInfo to your database if err := shared.GetDb().Create(&deviceInfo).Error; err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not save device info"}) return } c.JSON(http.StatusOK, gin.H{"message": "Successfully received and saved device info", "data": deviceInfo}) }