Soft delete device
This commit is contained in:
@@ -12,7 +12,7 @@ import (
|
|||||||
"gitlab.com/pactual1/backend/shared"
|
"gitlab.com/pactual1/backend/shared"
|
||||||
)
|
)
|
||||||
|
|
||||||
func SaveDeviceInfofunc(c *gin.Context) {
|
func SaveDeviceInfo(c *gin.Context) {
|
||||||
var deviceInfo models.DeviceInfo
|
var deviceInfo models.DeviceInfo
|
||||||
rawData, _ := c.GetRawData()
|
rawData, _ := c.GetRawData()
|
||||||
|
|
||||||
@@ -20,43 +20,57 @@ 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)
|
log.Printf("Invalid json pyload : %v",err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
deviceInfo.RawJSON = string(rawData)
|
deviceInfo.RawJSON = string(rawData)
|
||||||
|
|
||||||
// Attempt to find the device by IMEI; if not found, create a new device
|
// 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().Unscoped().Where("imei = ?", deviceInfo.IMEI).First(&device).Error; err != nil {
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
// Create new device
|
// Create new device
|
||||||
newDevice := models.Device{
|
newDevice := models.Device{
|
||||||
IMEI: deviceInfo.IMEI,
|
IMEI: deviceInfo.IMEI,
|
||||||
IMSI: deviceInfo.IMSI,
|
IMSI: deviceInfo.IMSI,
|
||||||
DeviceConfiguration: string(rawData),
|
DeviceConfiguration: string(rawData),
|
||||||
}
|
}
|
||||||
if err := shared.GetDb().Create(&newDevice).Error; err != nil {
|
if err := shared.GetDb().Create(&newDevice).Error; err != nil {
|
||||||
log.Printf("CREATE -Device DB Error: %v", err)
|
log.Printf("CREATE -Device DB Error: %v",err)
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not create new device"})
|
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
|
return
|
||||||
}
|
}
|
||||||
deviceInfo.DeviceID = newDevice.ID
|
|
||||||
} else {
|
} else {
|
||||||
log.Printf("CREATE -Device DB Error: %v", err)
|
log.Printf("Current device deleted at: %v",device.DeletedAt)
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"})
|
|
||||||
return
|
if device.DeletedAt != nil {
|
||||||
|
// Use raw SQL to update the record
|
||||||
|
if err := shared.GetDb().Exec("UPDATE devices SET deleted_at = NULL, company_id = NULL WHERE id = ?", device.ID).Error; err != nil {
|
||||||
|
log.Printf("UNDELETE -Device DB Error: %v", err)
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not revive deleted device"})
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
log.Printf("Device undeleted successfuly : %v",device.DeletedAt)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
deviceInfo.DeviceID = device.ID
|
||||||
}
|
}
|
||||||
} 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)
|
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
|
||||||
}
|
}
|
||||||
|
log.Printf("Successfully received and saved device info: %v",deviceInfo)
|
||||||
c.JSON(http.StatusOK, gin.H{"message": "Successfully received and saved device info", "data": deviceInfo})
|
c.JSON(http.StatusOK, gin.H{"message": "Successfully received and saved device info", "data": deviceInfo})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ func runTestServer() *httptest.Server {
|
|||||||
|
|
||||||
shared.Init()
|
shared.Init()
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
r.POST("/device_info", SaveDeviceInfofunc)
|
r.POST("/device_info", SaveDeviceInfo)
|
||||||
return httptest.NewServer(r)
|
return httptest.NewServer(r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import "github.com/jinzhu/gorm"
|
import (
|
||||||
|
"github.com/jinzhu/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
type Device struct {
|
type Device struct {
|
||||||
gorm.Model
|
gorm.Model
|
||||||
@@ -18,6 +20,12 @@ func (Device) Update() (bool, error) {
|
|||||||
func (Device) Create() (bool, error) {
|
func (Device) Create() (bool, error) {
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
func (Device) Delete() (bool, error) {
|
func (d *Device) Delete(db *gorm.DB) (bool, error) {
|
||||||
return false, nil
|
|
||||||
|
// Soft delete the device record.
|
||||||
|
if err := db.Delete(d).Error; err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,6 @@ func RegisterProtectedRoutes(r *gin.Engine) {
|
|||||||
|
|
||||||
authGroup.Use(middlewares.AuthHandler("admin"))
|
authGroup.Use(middlewares.AuthHandler("admin"))
|
||||||
{
|
{
|
||||||
authGroup.GET("/getmessage", controllers.GetSecretText)
|
authGroup.GET("/getmessage",controllers.GetSecretText)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,5 +9,5 @@ import (
|
|||||||
func RegisterPublicRoutes(r *gin.Engine) {
|
func RegisterPublicRoutes(r *gin.Engine) {
|
||||||
|
|
||||||
r.GET("/publicmessage", controllers.GetPublicText)
|
r.GET("/publicmessage", controllers.GetPublicText)
|
||||||
r.POST("/device_info", controllers.SaveDeviceInfofunc)
|
r.POST("/device_info", controllers.SaveDeviceInfo)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user