Upstream sync

This commit is contained in:
Senad Uka
2023-10-16 05:44:09 +02:00
parent d01c1a0232
commit 533451f39d
3 changed files with 99 additions and 47 deletions

View File

@@ -13,28 +13,29 @@ import (
"gitlab.com/pactual1/backend/shared"
)
func GetDevicesForContract(contractID uint64) ([]models.Device , int, error) {
// Create a slice to hold the devices
var devices []models.Device
func GetDevicesForContract(contractID uint64) ([]models.Device, int, error) {
// Fetch the contract from the database
var contract models.Contract
if err := shared.GetDb().Where("id = ?", contractID).First(&contract).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
log.Printf("GetDevicesByContract Error: No contract found: %v", err)
return devices, http.StatusNotFound ,err
return nil, http.StatusNotFound, err
} else {
log.Printf("GetDevicesByContract Error: Database error: %v", err)
return devices, http.StatusInternalServerError, err
return nil, http.StatusInternalServerError, err
}
}
log.Printf("This is the device IDS ID: %v", contract.DeviceIDs)
return GetDevicesByID(contract.DeviceIDs)
}
func GetDevicesByID(deviceIDs []int64) ([]models.Device, int, error) {
// Create a slice to hold the devices
var devices []models.Device
// Convert the integer array to a comma-separated string
idStrings := []string{}
for _, id := range contract.DeviceIDs {
for _, id := range deviceIDs {
idStrings = append(idStrings, strconv.FormatInt(id, 10))
}
idStr := strings.Join(idStrings, ",")
@@ -50,53 +51,51 @@ func GetDevicesForContract(contractID uint64) ([]models.Device , int, error) {
return devices, http.StatusNotFound, err
} else {
log.Printf("GetDevicesByContract Error: Database error: %v", err)
return devices, http.StatusInternalServerError, err
return devices, http.StatusInternalServerError, err
}
}
return devices , http.StatusOK, nil
return devices, http.StatusOK, nil
}
func GetDeviceInfoForContract (deviceID uint64, contract models.Contract) (models.GeoJSONFeatureCollection , int, error) {
func GetDeviceInfoForContract(deviceID uint64, contract models.Contract) (models.GeoJSONFeatureCollection, int, error) {
var deviceInfos []models.DeviceInfo
// Create a GeoJSON feature collection
var featureCollection models.GeoJSONFeatureCollection
featureCollection.Type = "FeatureCollection"
featureCollection.Features = []models.GeoJSONFeature{}
// Create a GeoJSON feature collection
var featureCollection models.GeoJSONFeatureCollection
featureCollection.Type = "FeatureCollection"
featureCollection.Features = []models.GeoJSONFeature{}
// Modify your query to include filtering based on the contract's creation date
query := shared.GetDb().Where("device_id = ?", deviceID).Where("created_at >= ? AND created_at <= ?", contract.StartTime, contract.EndTime)
// Modify your query to include filtering based on the contract's creation date
query := shared.GetDb().Where("device_id = ?", deviceID).Where("created_at >= ? AND created_at <= ?", contract.StartTime,contract.EndTime)
if err := query.Find(&deviceInfos).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
log.Printf("GetDeviceData Error: No device info found: %v", err)
if err := query.Find(&deviceInfos).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
log.Printf("GetDeviceData Error: No device info found: %v", err)
return featureCollection, http.StatusNotFound, err
} else {
log.Printf("GetDeviceData Error: Database error: %v", err)
} else {
log.Printf("GetDeviceData Error: Database error: %v", err)
return featureCollection, http.StatusNotFound, err
}
}
}
}
deviceInfosResponse := models.ConvertDeviceInfoToResponse(deviceInfos)
// Loop through each deviceInfo to create GeoJSON features
for _, info := range deviceInfosResponse {
info.RawJSON = ""
feature := models.GeoJSONFeature{
Type: "Feature",
DeviceInfoResponse: &info,
Geometry: models.GeoJSONGeometry{
Type: "Point",
Coordinates: []float64{info.Lon, info.Lat},
},
Properties: map[string]interface{}{},
}
featureCollection.Features = append(featureCollection.Features, feature)
}
// Loop through each deviceInfo to create GeoJSON features
for _, info := range deviceInfosResponse {
info.RawJSON = ""
feature := models.GeoJSONFeature{
Type: "Feature",
DeviceInfoResponse: &info,
Geometry: models.GeoJSONGeometry{
Type: "Point",
Coordinates: []float64{info.Lon, info.Lat},
},
Properties: map[string]interface{}{},
}
featureCollection.Features = append(featureCollection.Features, feature)
}
return featureCollection , 0, nil
return featureCollection, 0, nil
}
}