From 56bb362e5154e8c600a431d8254803e6fe12d85b Mon Sep 17 00:00:00 2001 From: Nedim Date: Thu, 14 Sep 2023 18:33:27 +0200 Subject: [PATCH] Soft delete device --- controllers/devices_controller.go | 62 ++++++++++++++++---------- controllers/devices_controller_test.go | 2 +- models/device.go | 14 ++++-- routes/protected_routes.go | 2 +- routes/public_routes.go | 2 +- 5 files changed, 52 insertions(+), 30 deletions(-) diff --git a/controllers/devices_controller.go b/controllers/devices_controller.go index 5835427..5868132 100644 --- a/controllers/devices_controller.go +++ b/controllers/devices_controller.go @@ -12,7 +12,7 @@ import ( "gitlab.com/pactual1/backend/shared" ) -func SaveDeviceInfofunc(c *gin.Context) { +func SaveDeviceInfo(c *gin.Context) { var deviceInfo models.DeviceInfo rawData, _ := c.GetRawData() @@ -20,43 +20,57 @@ func SaveDeviceInfofunc(c *gin.Context) { err := json.Unmarshal(rawData, &deviceInfo) if err != nil { 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 } + deviceInfo.RawJSON = string(rawData) - // Attempt to find the device by IMEI; if not found, create a new device - var device models.Device - if err := shared.GetDb().Where("imei = ?", deviceInfo.IMEI).First(&device).Error; err != nil { - if errors.Is(err, gorm.ErrRecordNotFound) { - // Create new device - 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"}) + // Attempt to find the device by IMEI; if not found, create a new device + var device models.Device + if err := shared.GetDb().Unscoped().Where("imei = ?", deviceInfo.IMEI).First(&device).Error; err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + // Create new device + 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 } - deviceInfo.DeviceID = newDevice.ID } else { - log.Printf("CREATE -Device DB Error: %v", err) - c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"}) - return + log.Printf("Current device deleted at: %v",device.DeletedAt) + + 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 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"}) 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}) } diff --git a/controllers/devices_controller_test.go b/controllers/devices_controller_test.go index d0d7790..fb11487 100644 --- a/controllers/devices_controller_test.go +++ b/controllers/devices_controller_test.go @@ -29,7 +29,7 @@ func runTestServer() *httptest.Server { shared.Init() r := gin.Default() - r.POST("/device_info", SaveDeviceInfofunc) + r.POST("/device_info", SaveDeviceInfo) return httptest.NewServer(r) } diff --git a/models/device.go b/models/device.go index cc746ec..8d7133a 100644 --- a/models/device.go +++ b/models/device.go @@ -1,6 +1,8 @@ package models -import "github.com/jinzhu/gorm" +import ( + "github.com/jinzhu/gorm" +) type Device struct { gorm.Model @@ -18,6 +20,12 @@ func (Device) Update() (bool, error) { func (Device) Create() (bool, error) { return false, nil } -func (Device) Delete() (bool, error) { - return false, nil +func (d *Device) Delete(db *gorm.DB) (bool, error) { + + // Soft delete the device record. + if err := db.Delete(d).Error; err != nil { + return false, err + } + + return true, nil } diff --git a/routes/protected_routes.go b/routes/protected_routes.go index e283ba3..10beda5 100644 --- a/routes/protected_routes.go +++ b/routes/protected_routes.go @@ -18,6 +18,6 @@ func RegisterProtectedRoutes(r *gin.Engine) { authGroup.Use(middlewares.AuthHandler("admin")) { - authGroup.GET("/getmessage", controllers.GetSecretText) + authGroup.GET("/getmessage",controllers.GetSecretText) } } diff --git a/routes/public_routes.go b/routes/public_routes.go index 3cdf975..e45a6ba 100644 --- a/routes/public_routes.go +++ b/routes/public_routes.go @@ -9,5 +9,5 @@ import ( func RegisterPublicRoutes(r *gin.Engine) { r.GET("/publicmessage", controllers.GetPublicText) - r.POST("/device_info", controllers.SaveDeviceInfofunc) + r.POST("/device_info", controllers.SaveDeviceInfo) }