Added logic for finding devices

This commit is contained in:
Nedim
2023-09-13 21:54:43 +02:00
parent 386f051d67
commit 1353404aed
6 changed files with 115 additions and 29 deletions

View File

@@ -2,6 +2,7 @@ package controllers
import (
"encoding/json"
"log"
"net/http"
"github.com/gin-gonic/gin"
@@ -20,12 +21,30 @@ func SaveDeviceInfofunc(c *gin.Context) {
return
}
// Convert raw JSON bytes to string
// 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)
// Save deviceInfo to your database here
db :=shared.GetDb()
db.Create(&deviceInfo)
// 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)
}
c.JSON(http.StatusOK, gin.H{"message": "Successfully received device info", "data": deviceInfo})
// 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})
}