Patch contract by ImeiID

This commit is contained in:
Nedim
2023-11-02 11:56:06 +01:00
parent b9fb75ff36
commit 4767756499
4 changed files with 145 additions and 20 deletions

View File

@@ -59,6 +59,35 @@ func GetDevicesByID(deviceIDs []int64) ([]models.Device, int, error) {
return devices, http.StatusOK, nil
}
func GetDevicesByImei(imeiIDs []string) ([]models.Device, int, error) {
// Create a slice to hold the devices
var devices []models.Device
// Quote each IMEI ID
for i, imei := range imeiIDs {
imeiIDs[i] = "'" + imei + "'"
}
// Join the quoted IMEI IDs into a comma-separated string
idStr := strings.Join(imeiIDs, ",")
// Construct the SQL query manually
sqlQuery := fmt.Sprintf("SELECT * FROM devices WHERE imei IN (%s)", idStr)
// Fetch devices from the database based on DeviceIDs in the contract
// Execute the query
if err := shared.GetDb().Raw(sqlQuery).Scan(&devices).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
log.Printf("GetDevicesByContract Error: No devices found: %v", err)
return devices, http.StatusNotFound, err
} else {
log.Printf("GetDevicesByContract Error: Database error: %v", err)
return devices, http.StatusInternalServerError, err
}
}
return devices, http.StatusOK, nil
}
func GetDeviceInfoForContract(deviceID uint64, contract models.Contract) (models.GeoJSONFeatureCollection, int, error) {
var deviceInfos []models.DeviceInfo