Added search params for get contract
This commit is contained in:
@@ -1,23 +1,27 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/jinzhu/gorm"
|
||||
"gitlab.com/pactual1/backend/database/contract"
|
||||
"gitlab.com/pactual1/backend/models"
|
||||
"gitlab.com/pactual1/backend/shared"
|
||||
)
|
||||
|
||||
func GetLatestContracts(c *gin.Context) {
|
||||
// Get limit and offset from query parameter with defaults
|
||||
// Get limit, offset, and status from query parameters with defaults
|
||||
limitStr := c.DefaultQuery("limit", "99999999999999999999999999999999")
|
||||
offsetStr := c.DefaultQuery("offset", "0")
|
||||
status := c.DefaultQuery("status", models.ContractStatusActive)
|
||||
|
||||
// Optional search parameters
|
||||
companyName := c.Query("company_name")
|
||||
startTimeStr := c.Query("start_time")
|
||||
deviceIDsStr := c.QueryArray("device_ids[]") // Assuming deviceIDs are passed as an array query parameter
|
||||
|
||||
// Convert limit and offset to int
|
||||
limit, err := strconv.Atoi(limitStr)
|
||||
if err != nil {
|
||||
@@ -33,29 +37,37 @@ func GetLatestContracts(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Create a slice to hold the contracts
|
||||
var contracts []models.Contract
|
||||
|
||||
// Count the total number of contracts
|
||||
var total int64
|
||||
if err := shared.GetDb().Where("status = ?", status).Model(&models.Contract{}).Count(&total).Error; err != nil {
|
||||
log.Printf("GetLatestContracts Error: Database error: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"})
|
||||
return
|
||||
}
|
||||
|
||||
// Fetch the latest contracts from the database with LIMIT and OFFSET
|
||||
if err := shared.GetDb().Where("status = ?", status).Order("created_at desc").Limit(limit).Offset(offset).Find(&contracts).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
log.Printf("GetLatestContracts Error: No contracts found: %v", err)
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "No contracts found"})
|
||||
} else {
|
||||
log.Printf("GetLatestContracts Error: Database error: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"})
|
||||
// Convert startTime to time.Time from Unix time in seconds
|
||||
var startTime time.Time
|
||||
if startTimeStr != "" {
|
||||
startTimeUnix, err := strconv.ParseInt(startTimeStr, 10, 64)
|
||||
if err != nil {
|
||||
log.Printf("GetLatestContracts Error: Invalid startTime value: %v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid startTime value"})
|
||||
return
|
||||
}
|
||||
return
|
||||
startTime = time.Unix(startTimeUnix, 0)
|
||||
}
|
||||
|
||||
// Convert deviceIDs to []int64
|
||||
var deviceIDs []int64
|
||||
for _, idStr := range deviceIDsStr {
|
||||
id, err := strconv.ParseInt(idStr, 10, 64)
|
||||
if err != nil {
|
||||
log.Printf("GetLatestContracts Error: Invalid deviceID value: %v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid deviceID value"})
|
||||
return
|
||||
}
|
||||
deviceIDs = append(deviceIDs, id)
|
||||
}
|
||||
|
||||
// Fetch contracts
|
||||
contracts, total, st, err := contract.GetContracts(status, companyName, startTime, deviceIDs,limit, offset)
|
||||
|
||||
if err != nil {
|
||||
c.JSON(st, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
// Respond with the contracts and the total count
|
||||
c.JSON(http.StatusOK, gin.H{"total": total, "contracts": contracts})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user