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

@@ -19,9 +19,9 @@ import (
"gitlab.com/pactual1/backend/shared"
)
func GetContracts(status []string, companyName string, companyAddress string,
companyEmail string, companyPhone string, startTime *time.Time, endTime *time.Time,
contractName string, deviceIDs []int64, contractIDs []int64, dateCreated *time.Time, company, limit, offset int) ([]models.Contract, int64, int, error) {
func GetContracts(status []string, companyName string, companyAddress string, companyEmail string, companyPhone string,
startTime *time.Time, endTime *time.Time, contractName string, deviceIDs []int64, contractIDs []int64, dateCreated *time.Time,
uuid string, company, limit, offset int) ([]models.Contract, int64, int, error) {
var contracts []models.Contract
db := shared.GetDb()
@@ -69,6 +69,12 @@ func GetContracts(status []string, companyName string, companyAddress string,
countDb = countDb.Where("lower(contracts.name) LIKE ?", "%"+strings.ToLower(contractName)+"%")
}
// Check if uuid is present
if uuid != "" {
db = db.Where("contracts.uuid = ?", uuid)
countDb = countDb.Where("contracts.uuid = ?", uuid)
}
// Search by Start Time and End Time
if startTime != nil && !startTime.IsZero() {
db = db.Where("start_time >= ?", startTime)
@@ -139,7 +145,7 @@ func UpdateContract(contract models.Contract) (models.Contract, int, error) {
}
// get old contract to compare updates
oldContract, status, err := GetContractByID(contract.ID)
oldContract, status, err := GetContractByID(contract.ID, "")
if err != nil {
return contract, status, err
}
@@ -167,7 +173,7 @@ func UpdateContract(contract models.Contract) (models.Contract, int, error) {
return contract, http.StatusInternalServerError, err
}
contract, status, err = GetContractByID(contract.ID)
contract, status, err = GetContractByID(contract.ID, "")
if err != nil {
return contract, status, err
}
@@ -218,7 +224,7 @@ func validateContractDevices(contractID uint, devices []models.Device) (int, err
for _, device := range devices {
if device.CurrentContractID != nil && *device.CurrentContractID != contractID {
currentDeviceContract, status, err := GetContractByID(*device.CurrentContractID)
currentDeviceContract, status, err := GetContractByID(*device.CurrentContractID, "")
if err != nil {
return status, err
}
@@ -233,11 +239,17 @@ func validateContractDevices(contractID uint, devices []models.Device) (int, err
return http.StatusOK, nil
}
func GetContractByID(contractID uint) (models.Contract, int, error) {
func GetContractByID(contractID uint, uuid string) (models.Contract, int, error) {
// Fetch the contract creation date based on contractID
var contract models.Contract
if err := shared.GetDb().Unscoped().Where("id = ?", contractID).First(&contract).Error; err != nil {
db := shared.GetDb().Unscoped().Where("id = ?", contractID)
// Include UUID in the query if provided
if uuid != "" {
db = db.Where("uuid = ?", uuid)
}
if err := db.First(&contract).Error; err != nil {
log.Printf("GetContractByID Error: Could not fetch contract: %v", err)
return contract, http.StatusInternalServerError, err
}

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