Added logic for finding devices
This commit is contained in:
@@ -32,11 +32,11 @@ func Load() error {
|
||||
Environment: getEnv("NOVATECH_ADMIN_SERVICE_ENVIRONMENT", "DEV"),
|
||||
},
|
||||
Database: Database{
|
||||
UserName: mustGetEnv("NOVATECH_DATABASE_USERNAME"),
|
||||
Password: mustGetEnv("NOVATECH_DATABASE_PASSWORD"),
|
||||
DatabaseName: mustGetEnv("NOVATECH_DATABASE_NAME"),
|
||||
HostName: mustGetEnv("NOVATECH_DATABASE_ADDRESS"),
|
||||
Port: mustGetEnv("NOVATECH_DATABASE_PORT"),
|
||||
UserName: getEnv("NOVATECH_DATABASE_USERNAME", "root"),
|
||||
Password: getEnv("NOVATECH_DATABASE_PASSWORD", "root"),
|
||||
DatabaseName: getEnv("NOVATECH_DATABASE_NAME", "postgres"),
|
||||
HostName: getEnv("NOVATECH_DATABASE_ADDRESS", "localhost"),
|
||||
Port: getEnv("NOVATECH_DATABASE_PORT", " "),
|
||||
|
||||
},
|
||||
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
package controllers
|
||||
|
||||
// import (
|
||||
// "net/http"
|
||||
// models "gitlab.com/pactual1/backend/models"
|
||||
|
||||
// "gitlab.com/pactual1/backend/models"
|
||||
// "github.com/gin-gonic/gin"
|
||||
// )
|
||||
|
||||
// func Companies(c *gin.Context) {
|
||||
// var companies []models.Company
|
||||
// // Fetch companies from DB here
|
||||
// if err := models.FetchCompanies(&companies); err != nil {
|
||||
// c.JSON(http.StatusInternalServerError, gin.H{"error": "Error fetching companies"})
|
||||
// return
|
||||
// }
|
||||
// c.JSON(http.StatusOK, gin.H{"companies": companies})
|
||||
// }
|
||||
@@ -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})
|
||||
}
|
||||
|
||||
81
controllers/DevicesController_test.go
Normal file
81
controllers/DevicesController_test.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"gitlab.com/pactual1/backend/config"
|
||||
"gitlab.com/pactual1/backend/shared"
|
||||
)
|
||||
|
||||
// Assume setupServer() returns your gin.Engine instance
|
||||
func runTestServer() *httptest.Server {
|
||||
// Initialize your database or any other setup
|
||||
// For the sake of this example, I'll just return the Gin engine
|
||||
|
||||
// LOAD APPLICATION CONFIGURATION
|
||||
err := config.Load()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
shared.Init()
|
||||
r := gin.Default()
|
||||
r.POST("/device_info", SaveDeviceInfofunc)
|
||||
return httptest.NewServer(r)
|
||||
}
|
||||
|
||||
func TestSaveDeviceInfofunc(t *testing.T) {
|
||||
ts := runTestServer()
|
||||
defer ts.Close()
|
||||
|
||||
t.Run("it should return 400 when JSON payload is invalid", func(t *testing.T) {
|
||||
payload := []byte(`{"InvalidPayload": `)
|
||||
resp, err := http.Post(fmt.Sprintf("%s/device_info", ts.URL), "application/json", bytes.NewBuffer(payload))
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
|
||||
|
||||
assert.Equal(t, 400, resp.StatusCode)
|
||||
})
|
||||
|
||||
t.Run("it should log a message when IMEI is not found", func(t *testing.T) {
|
||||
// Add a valid payload but with an unknown IMEI
|
||||
payload := []byte(`{"IMEI": "UNKNOWN", "SomeOtherField": "Value"}`)
|
||||
resp, err := http.Post(fmt.Sprintf("%s/device_info", ts.URL), "application/json", bytes.NewBuffer(payload))
|
||||
|
||||
if err != nil {
|
||||
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)
|
||||
})
|
||||
|
||||
t.Run("it should return 200 when all conditions are correct", func(t *testing.T) {
|
||||
// Add a valid payload
|
||||
deviceInfo := map[string]interface{}{
|
||||
"IMEI": "SOME_VALID_IMEI",
|
||||
"SomeOtherField": "Value",
|
||||
}
|
||||
|
||||
payload, _ := json.Marshal(deviceInfo)
|
||||
resp, err := http.Post(fmt.Sprintf("%s/device_info", ts.URL), "application/json", bytes.NewBuffer(payload))
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
|
||||
assert.Equal(t, 200, resp.StatusCode)
|
||||
})
|
||||
}
|
||||
3
go.mod
3
go.mod
@@ -8,6 +8,7 @@ require (
|
||||
github.com/jinzhu/gorm v1.9.16
|
||||
github.com/joho/godotenv v1.5.1
|
||||
github.com/qor/admin v1.2.0
|
||||
github.com/stretchr/testify v1.8.3
|
||||
)
|
||||
|
||||
replace gitlab.com/pactual1/backend => ./
|
||||
@@ -18,6 +19,7 @@ require (
|
||||
github.com/bytedance/sonic v1.9.1 // indirect
|
||||
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
|
||||
github.com/chris-ramon/douceur v0.2.0 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
|
||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||
github.com/go-playground/locales v0.14.1 // indirect
|
||||
@@ -40,6 +42,7 @@ require (
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/qor/assetfs v0.0.0-20170713023933-ff57fdc13a14 // indirect
|
||||
github.com/qor/middlewares v0.0.0-20170822143614-781378b69454 // indirect
|
||||
github.com/qor/qor v0.0.0-20210618082622-a52aba0a0ce1 // indirect
|
||||
|
||||
@@ -5,6 +5,8 @@ import "github.com/jinzhu/gorm"
|
||||
type Device struct {
|
||||
gorm.Model
|
||||
DeviceName string
|
||||
IMEI string `json:"imei"`
|
||||
IMSI string `json:"imsi"`
|
||||
DeviceConfiguration string `gorm:"type:json"`
|
||||
CompanyID uint
|
||||
DeviceInfos []DeviceInfo
|
||||
|
||||
Reference in New Issue
Block a user