Fixed comments for create new device where device not found while saving device_info

This commit is contained in:
Nedim
2023-09-15 07:41:39 +02:00
parent faea7f213b
commit aefa8d927b
3 changed files with 35 additions and 17 deletions

View File

@@ -1,6 +1,9 @@
package controllers package controllers
import ( import (
"log"
"net/http"
"gitlab.com/pactual1/backend/middlewares" "gitlab.com/pactual1/backend/middlewares"
"gitlab.com/pactual1/backend/shared" "gitlab.com/pactual1/backend/shared"
@@ -11,7 +14,7 @@ import (
type AuthController struct{} type AuthController struct{}
func (this AuthController)HandleLogin(c *gin.Context) { func (AuthController)HandleLogin(c *gin.Context) {
userId:="123" userId:="123"
username:="Beast" username:="Beast"
roles:= []string{shared.RoleAdmin, shared.RoleProUser} roles:= []string{shared.RoleAdmin, shared.RoleProUser}
@@ -21,6 +24,8 @@ func (this AuthController)HandleLogin(c *gin.Context) {
//issue token //issue token
token, err := middlewares.GenerateToken([]byte(middlewares.SigningKey), userId,username, roles) token, err := middlewares.GenerateToken([]byte(middlewares.SigningKey), userId,username, roles)
if err != nil { if err != nil {
log.Printf("Unable to generate token %v", err)
c.JSON(http.StatusInternalServerError, err)
} }
c.JSON(200, token) c.JSON(200, token)

View File

@@ -2,10 +2,12 @@ package controllers
import ( import (
"encoding/json" "encoding/json"
"errors"
"log" "log"
"net/http" "net/http"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
"gitlab.com/pactual1/backend/models" "gitlab.com/pactual1/backend/models"
"gitlab.com/pactual1/backend/shared" "gitlab.com/pactual1/backend/shared"
) )
@@ -18,30 +20,42 @@ func SaveDeviceInfofunc(c *gin.Context) {
err := json.Unmarshal(rawData, &deviceInfo) err := json.Unmarshal(rawData, &deviceInfo)
if err != nil { if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid JSON payload"}) c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid JSON payload"})
log.Printf("Invalid json pyload : %v",err)
return 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) deviceInfo.RawJSON = string(rawData)
// Attempt to find the device by IMEI; if found, set the DeviceID // Attempt to find the device by IMEI; if not found, create a new device
var device models.Device var device models.Device
if err := shared.GetDb().Where("imei = ?", deviceInfo.IMEI).First(&device).Error; err == nil { if err := shared.GetDb().Where("imei = ?", deviceInfo.IMEI).First(&device).Error; err != nil {
deviceInfo.DeviceID = device.ID if errors.Is(err, gorm.ErrRecordNotFound) {
} else { // Create new device
log.Printf("Could not find device with imei %v", deviceInfo.IMEI) newDevice := models.Device{
} IMEI: deviceInfo.IMEI,
IMSI: deviceInfo.IMSI,
DeviceConfiguration: string(rawData),
}
if err := shared.GetDb().Create(&newDevice).Error; err != nil {
log.Printf("CREATE -Device DB Error: %v",err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not create new device"})
return
}
deviceInfo.DeviceID = newDevice.ID
} else {
log.Printf("CREATE -Device DB Error: %v",err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"})
return
}
} else {
deviceInfo.DeviceID = device.ID
}
// Save deviceInfo to your database // Save deviceInfo to your database
if err := shared.GetDb().Create(&deviceInfo).Error; err != nil { if err := shared.GetDb().Create(&deviceInfo).Error; err != nil {
log.Printf("SaveDeviceInfo CREATE -DeviceInfo DB Error: %v",err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not save device info"}) c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not save device info"})
return return
} }

View File

@@ -59,7 +59,6 @@ func TestSaveDeviceInfofunc(t *testing.T) {
t.Fatalf("Expected no error, got %v", err) t.Fatalf("Expected no error, got %v", err)
} }
// Here, ideally you'd want to check your logs to ensure the message was logged
assert.Equal(t, 200, resp.StatusCode) assert.Equal(t, 200, resp.StatusCode)
}) })