Added search params for get contract
This commit is contained in:
@@ -3,14 +3,14 @@ package controllers
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/jinzhu/gorm"
|
||||
"gitlab.com/pactual1/backend/database/contract"
|
||||
"gitlab.com/pactual1/backend/database/device"
|
||||
"gitlab.com/pactual1/backend/models"
|
||||
"gitlab.com/pactual1/backend/shared"
|
||||
)
|
||||
@@ -107,11 +107,10 @@ func GetDeviceData(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Fetch the contract creation date based on contractID
|
||||
var contract models.Contract
|
||||
if err := shared.GetDb().Where("id = ?", contractID).First(&contract).Error; err != nil {
|
||||
log.Printf("GetDeviceData Error: Could not fetch contract: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not fetch contract"})
|
||||
contract , st, err := contract.GetContractByID(contractID)
|
||||
|
||||
if err != nil {
|
||||
c.JSON(st, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -128,41 +127,13 @@ func GetDeviceData(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var deviceInfos []models.DeviceInfo
|
||||
// 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)
|
||||
featureCollection, st, err := device.GetDeviceInfoForContract(deviceID, contract)
|
||||
|
||||
if err := query.Find(&deviceInfos).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
log.Printf("GetDeviceData Error: No device info found: %v", err)
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "No device info found"})
|
||||
} else {
|
||||
log.Printf("GetDeviceData Error: Database error: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"})
|
||||
}
|
||||
if err != nil {
|
||||
c.JSON(st, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// Create a GeoJSON feature collection
|
||||
var featureCollection models.GeoJSONFeatureCollection
|
||||
featureCollection.Type = "FeatureCollection"
|
||||
featureCollection.Features = []models.GeoJSONFeature{}
|
||||
|
||||
// Loop through each deviceInfo to create GeoJSON features
|
||||
for _, info := range deviceInfos {
|
||||
info.RawJSON = ""
|
||||
feature := models.GeoJSONFeature{
|
||||
Type: "Feature",
|
||||
DeviceInfo: &info,
|
||||
Geometry: models.GeoJSONGeometry{
|
||||
Type: "Point",
|
||||
Coordinates: []float64{info.Lon, info.Lat},
|
||||
},
|
||||
Properties: map[string]interface{}{},
|
||||
}
|
||||
featureCollection.Features = append(featureCollection.Features, feature)
|
||||
}
|
||||
|
||||
// Respond with the GeoJSON feature collection
|
||||
c.JSON(http.StatusOK, featureCollection)
|
||||
}
|
||||
@@ -187,46 +158,12 @@ func GetDevicesByContract(c *gin.Context) {
|
||||
}
|
||||
|
||||
log.Printf("This is the ID: %v", contractID)
|
||||
// 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)
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "No contract found"})
|
||||
return
|
||||
} else {
|
||||
log.Printf("GetDevicesByContract Error: Database error: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"})
|
||||
return
|
||||
}
|
||||
}
|
||||
log.Printf("This is the device IDS ID: %v", contract.DeviceIDs)
|
||||
// Create a slice to hold the devices
|
||||
var devices []models.Device
|
||||
devices, st, err := device.GetDevicesForContract(contractID)
|
||||
|
||||
// Convert the integer array to a comma-separated string
|
||||
idStrings := []string{}
|
||||
for _, id := range contract.DeviceIDs {
|
||||
idStrings = append(idStrings, strconv.FormatInt(id, 10))
|
||||
}
|
||||
idStr := strings.Join(idStrings, ",")
|
||||
|
||||
// Construct the SQL query manually
|
||||
sqlQuery := fmt.Sprintf("SELECT * FROM devices WHERE id 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)
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "No devices found"})
|
||||
} else {
|
||||
log.Printf("GetDevicesByContract Error: Database error: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"})
|
||||
}
|
||||
if err != nil {
|
||||
c.JSON(st, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// Respond with the devices
|
||||
c.JSON(http.StatusOK, devices)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user