Added invoices endpoint
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
@@ -12,62 +11,76 @@ import (
|
||||
)
|
||||
|
||||
func GetLatestContracts(c *gin.Context) {
|
||||
// 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)
|
||||
// Existing parameters
|
||||
limitStr := c.DefaultQuery("limit", "50")
|
||||
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("deviceIDs[]") // Assuming deviceIDs are passed as an array query parameter
|
||||
// New/Updated optional parameters
|
||||
companyName := c.Query("company_name")
|
||||
companyAddress := c.Query("company_address")
|
||||
companyEmail := c.Query("company_email")
|
||||
companyPhone := c.Query("company_phone")
|
||||
contractName := c.Query("contract_name")
|
||||
startTimeStr := c.Query("start_time")
|
||||
endTimeStr := c.Query("end_time")
|
||||
deviceIDsStr := c.QueryArray("deviceIDs[]")
|
||||
|
||||
// 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
|
||||
}
|
||||
// Convert limit and offset to int
|
||||
limit, err := strconv.Atoi(limitStr)
|
||||
if err != nil {
|
||||
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
|
||||
}
|
||||
offset, err := strconv.Atoi(offsetStr)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid offset value"})
|
||||
return
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
// Convert startTime to time.Time
|
||||
var startTime time.Time
|
||||
if startTimeStr != "" {
|
||||
startTimeUnix, err := strconv.ParseInt(startTimeStr, 10, 64)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid start_time value"})
|
||||
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)
|
||||
}
|
||||
// Convert endTime to time.Time
|
||||
var endTime time.Time
|
||||
if endTimeStr != "" {
|
||||
endTimeUnix, err := strconv.ParseInt(endTimeStr, 10, 64)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid end_time value"})
|
||||
return
|
||||
}
|
||||
endTime = time.Unix(endTimeUnix, 0)
|
||||
}
|
||||
|
||||
// Fetch contracts
|
||||
contracts, total, st, err := contract.GetContracts(status, companyName, startTime, deviceIDs,limit, offset)
|
||||
// Convert deviceIDs to []int64
|
||||
var deviceIDs []int64
|
||||
for _, idStr := range deviceIDsStr {
|
||||
id, err := strconv.ParseInt(idStr, 10, 64)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid deviceID value"})
|
||||
return
|
||||
}
|
||||
deviceIDs = append(deviceIDs, id)
|
||||
}
|
||||
|
||||
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})
|
||||
// Fetch contracts
|
||||
contracts, total, st, err := contract.GetContracts(status, companyName, companyAddress, companyEmail, companyPhone, startTime, endTime, contractName, 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