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

@@ -15,19 +15,27 @@ import (
"gitlab.com/pactual1/backend/shared"
)
func GetDevicesForContract(contractID uint64, companyID int) ([]models.Device, int, error) {
// Fetch the contract from the database
func GetDevicesForContract(contractID uint64, uuid string, companyID int) ([]models.Device, int, error) {
// Fetch the contract from the database using both contractID and UUID
var contract models.Contract
if err := shared.GetDb().Where("id = ?", contractID).First(&contract).Error; err != nil {
query := shared.GetDb().Where("id = ?", contractID)
// If UUID is provided, include it in the query
if uuid != "" {
query = query.Where("uuid = ?", uuid)
}
if err := query.First(&contract).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
log.Printf("GetDevicesByContract Error: No contract found: %v", err)
log.Printf("GetDevicesForContract Error: No contract found: %v", err)
return nil, http.StatusNotFound, err
} else {
log.Printf("GetDevicesByContract Error: Database error: %v", err)
log.Printf("GetDevicesForContract Error: Database error: %v", err)
return nil, http.StatusInternalServerError, err
}
}
log.Printf("This is the device IDS ID: %v", contract.DeviceIDs)
log.Printf("This is the device IDs: %v", contract.DeviceIDs)
return GetDevicesByID(contract.DeviceIDs)
}