Patch contract by ImeiID
This commit is contained in:
@@ -120,8 +120,9 @@ func UpdateContract(contract models.Contract) (models.Contract, int, error) {
|
||||
var devices []models.Device
|
||||
var status int
|
||||
var err error
|
||||
if contract.DeviceIDs != nil {
|
||||
devices, status, err = device.GetDevicesByID(contract.DeviceIDs)
|
||||
if contract.DevicesImeis != nil {
|
||||
log.Printf("Found devices imes %v", contract.DevicesImeis)
|
||||
devices, status, err = device.GetDevicesByImei(contract.DevicesImeis)
|
||||
if err != nil {
|
||||
return contract, status, err
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user