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

@@ -9,6 +9,7 @@ import (
"time"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"gitlab.com/pactual1/backend/database/contract"
"gitlab.com/pactual1/backend/models"
"gitlab.com/pactual1/backend/shared"
@@ -36,6 +37,7 @@ func GetLatestContracts(c *gin.Context) {
iDsStr := c.QueryArray("ids[]")
company := c.GetInt("companyID")
uuid := c.Query("uuid")
// Convert limit and offset to int
limit, err := strconv.Atoi(limitStr)
@@ -95,7 +97,7 @@ func GetLatestContracts(c *gin.Context) {
}
// Fetch contracts
contracts, total, st, err := contract.GetContracts(status, companyName, companyAddress, companyEmail, companyPhone, &startTime, &endTime, contractName, deviceIDs, contractIDs, nil, company, limit, offset)
contracts, total, st, err := contract.GetContracts(status, companyName, companyAddress, companyEmail, companyPhone, &startTime, &endTime, contractName, deviceIDs, contractIDs, nil, uuid, company, limit, offset)
if err != nil {
c.JSON(st, gin.H{"error": err.Error()})
@@ -178,7 +180,7 @@ func GetBuyerContracts(c *gin.Context) {
}
// Fetch contracts
contracts, total, st, err := contract.GetContracts(status, "", "", "", "", startTime, endTime, qStr, nil, contractIDs, &dateCreated, company, limit, offset)
contracts, total, st, err := contract.GetContracts(status, "", "", "", "", startTime, endTime, qStr, nil, contractIDs, &dateCreated, "", company, limit, offset)
if err != nil {
c.JSON(st, gin.H{"error": err.Error()})
return
@@ -207,6 +209,7 @@ func CreateContract(c *gin.Context) {
db := shared.GetDb()
newContract := models.Contract{
UUID: uuid.New().String(),
BuyerID: payload.BuyerID,
SellerID: payload.SellerID,
Description: payload.Description,
@@ -256,7 +259,7 @@ func GetContractByID(c *gin.Context) {
}
// Fetch contract
contract, st, err := contract.GetContractByID(uint(contractID))
contract, st, err := contract.GetContractByID(uint(contractID), "")
if err != nil {
c.JSON(st, gin.H{"error": err.Error()})
return

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)})
}