Files
old-backend/database/contract/contract.go

112 lines
3.7 KiB
Go
Raw Normal View History

2023-09-22 11:38:32 +02:00
package contract
import (
"errors"
"log"
"net/http"
"time"
"github.com/jinzhu/gorm"
2023-09-27 19:20:44 +02:00
"github.com/lib/pq"
2023-09-22 11:38:32 +02:00
"gitlab.com/pactual1/backend/models"
"gitlab.com/pactual1/backend/shared"
)
2023-09-27 19:20:44 +02:00
func GetContracts(status string, companyName string, companyAddress string,
companyEmail string, companyPhone string, startTime time.Time, endTime time.Time,
contractName string, deviceIDs []int64, limit, offset int) ([]models.Contract, int64, int, error) {
var contracts []models.Contract
db := shared.GetDb()
2023-09-22 11:38:32 +02:00
2023-09-27 19:20:44 +02:00
countDb := db
2023-09-22 11:38:32 +02:00
2023-09-27 19:20:44 +02:00
// Search by Status
if status != "" {
db = shared.GetDb().Where("contracts.status = ?", status)
countDb = countDb.Where("contracts.status = ?", status)
}
2023-09-22 11:38:32 +02:00
2023-09-27 19:20:44 +02:00
// Search by Company Fields
if companyName != "" || companyAddress != "" || companyEmail != "" || companyPhone != "" {
db = db.Joins("left join companies on companies.id = contracts.buyer_id")
countDb = countDb.Joins("left join companies on companies.id = contracts.buyer_id")
if companyName != "" {
db = db.Where("companies.name LIKE ?", "%"+companyName+"%")
countDb = countDb.Where("companies.name LIKE ?", "%"+companyName+"%")
}
if companyAddress != "" {
db = db.Where("companies.address LIKE ?", "%"+companyAddress+"%")
countDb = countDb.Where("companies.address LIKE ?", "%"+companyAddress+"%")
}
if companyEmail != "" {
db = db.Where("companies.email LIKE ?", "%"+companyEmail+"%")
countDb = countDb.Where("companies.email LIKE ?", "%"+companyEmail+"%")
}
2023-09-22 11:38:32 +02:00
2023-09-27 19:20:44 +02:00
if companyPhone != "" {
db = db.Where("companies.phone LIKE ?", "%"+companyPhone+"%")
countDb = countDb.Where("companies.phone LIKE ?", "%"+companyPhone+"%")
}
}
// Search by Contract Name
if contractName != "" {
db = db.Where("contracts.name LIKE ?", "%"+contractName+"%")
countDb = countDb.Where("contracts.name LIKE ?", "%"+contractName+"%")
2023-09-22 11:38:32 +02:00
}
2023-09-27 19:20:44 +02:00
// Search by Start Time
if !startTime.IsZero() {
db = db.Where("start_time >= ?", startTime)
countDb = countDb.Where("start_time >= ?", startTime)
}
// Search by End Time
if !endTime.IsZero() {
db = db.Where("end_time <= ?", endTime)
countDb = countDb.Where("end_time <= ?", endTime)
}
// Search by Device IDs
if len(deviceIDs) > 0 {
db = db.Where("device_ids && ?", pq.Array(deviceIDs))
countDb = countDb.Where("device_ids && ?", pq.Array(deviceIDs))
}
var total int64
if err := countDb.Model(&models.Contract{}).Count(&total).Error; err != nil {
log.Printf("GetContracts Error: Database error: %v", err)
return contracts, total, http.StatusInternalServerError, err
}
if err := db.Order("created_at desc").Limit(limit).Offset(offset).Find(&contracts).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
log.Printf("GetContracts Error: No contracts found: %v", err)
return contracts, total, http.StatusNotFound, err
} else {
log.Printf("GetContracts Error: Database error: %v", err)
return contracts, total, http.StatusInternalServerError, err
}
}
return contracts, total, http.StatusOK, nil
2023-09-22 11:38:32 +02:00
}
2023-09-27 19:20:44 +02:00
2023-09-22 11:38:32 +02:00
func GetContractByID(contractID uint64) (models.Contract, int ,error) {
// 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)
return contract, http.StatusInternalServerError, err
}
return contract , http.StatusOK, nil
}