2023-09-19 08:15:42 +02:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"log"
|
|
|
|
|
"net/http"
|
|
|
|
|
"strconv"
|
2023-09-22 11:38:32 +02:00
|
|
|
"time"
|
2023-09-19 08:15:42 +02:00
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2023-09-22 11:38:32 +02:00
|
|
|
"gitlab.com/pactual1/backend/database/contract"
|
2023-09-19 08:15:42 +02:00
|
|
|
"gitlab.com/pactual1/backend/models"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func GetLatestContracts(c *gin.Context) {
|
2023-09-22 11:38:32 +02:00
|
|
|
// Get limit, offset, and status from query parameters with defaults
|
2023-09-19 08:15:42 +02:00
|
|
|
limitStr := c.DefaultQuery("limit", "99999999999999999999999999999999")
|
|
|
|
|
offsetStr := c.DefaultQuery("offset", "0")
|
2023-09-22 08:28:50 +02:00
|
|
|
status := c.DefaultQuery("status", models.ContractStatusActive)
|
2023-09-19 08:15:42 +02:00
|
|
|
|
2023-09-22 11:38:32 +02:00
|
|
|
// 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
|
|
|
|
|
|
2023-09-19 08:15:42 +02:00
|
|
|
// Convert limit and offset to int
|
|
|
|
|
limit, err := strconv.Atoi(limitStr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("GetLatestContracts Error: Invalid limit value: %v", err)
|
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid limit value"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
offset, err := strconv.Atoi(offsetStr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("GetLatestContracts Error: Invalid offset value: %v", err)
|
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid offset value"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-22 11:38:32 +02:00
|
|
|
// 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
|
|
|
|
|
}
|
|
|
|
|
startTime = time.Unix(startTimeUnix, 0)
|
2023-09-19 08:15:42 +02:00
|
|
|
}
|
|
|
|
|
|
2023-09-22 11:38:32 +02:00
|
|
|
// 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
|
2023-09-19 08:15:42 +02:00
|
|
|
}
|
2023-09-22 11:38:32 +02:00
|
|
|
deviceIDs = append(deviceIDs, id)
|
2023-09-19 08:15:42 +02:00
|
|
|
}
|
|
|
|
|
|
2023-09-22 11:38:32 +02:00
|
|
|
// 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
|
|
|
|
|
}
|
2023-09-19 08:15:42 +02:00
|
|
|
// Respond with the contracts and the total count
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"total": total, "contracts": contracts})
|
|
|
|
|
}
|