Added uuid middleware

This commit is contained in:
Nedim
2023-11-21 18:28:30 +01:00
parent a7ab058d01
commit d54b643378
11 changed files with 133 additions and 27 deletions

View File

@@ -41,7 +41,7 @@ func SaveDeviceInfo(c *gin.Context) {
}
if currentDevice.CurrentContractID != nil {
deviceContract, _, err := contract.GetContractByID(*currentDevice.CurrentContractID)
deviceContract, _, err := contract.GetContractByID(*currentDevice.CurrentContractID, "")
if err != nil {
log.Printf("SaveDeviceInfo - GetContractByID error : %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not fetch device contract"})
@@ -94,6 +94,8 @@ func GetDeviceData(c *gin.Context) {
deviceIDStr := c.DefaultQuery("device_id", "")
contractIDStr := c.DefaultQuery("contract_id", "")
uuid := c.DefaultQuery("uuid", "")
if deviceIDStr == "" {
log.Printf("GetDeviceData Error: Device ID is required")
c.JSON(http.StatusBadRequest, gin.H{"error": "Device ID is required"})
@@ -114,7 +116,7 @@ func GetDeviceData(c *gin.Context) {
return
}
contract, st, err := contract.GetContractByID(uint(contractID))
contract, st, err := contract.GetContractByID(uint(contractID), uuid) // Update this line to pass UUID
if err != nil {
c.JSON(st, gin.H{"error": err.Error()})
return
@@ -145,13 +147,16 @@ func GetDeviceData(c *gin.Context) {
}
func GetDevicesByContract(c *gin.Context) {
// Get the contract ID from query parameter
// Get the contract ID and UUID from query parameters
contractIDStr := c.DefaultQuery("contract_id", "")
uuid := c.DefaultQuery("uuid", "") // Add this line to get the UUID
if contractIDStr == "" {
log.Printf("GetDevicesByContract Error: Contract ID is required")
c.JSON(http.StatusBadRequest, gin.H{"error": "Contract ID is required"})
return
}
companyID := c.GetInt("companyID")
// Convert string to uint
@@ -162,13 +167,14 @@ func GetDevicesByContract(c *gin.Context) {
return
}
log.Printf("This is the ID: %v", contractID)
devices, st, err := device.GetDevicesForContract(contractID, companyID)
log.Printf("This is the Contract ID: %v, UUID: %s", contractID, uuid)
devices, st, err := device.GetDevicesForContract(contractID, uuid, companyID) // Pass UUID here
if err != nil {
c.JSON(st, gin.H{"error": err.Error()})
return
}
// Respond with the devices
c.JSON(http.StatusOK, gin.H{"data": models.ConvertDeviceToResponse(devices)})
}