Upstream sync

This commit is contained in:
Senad Uka
2023-10-13 11:48:14 +02:00
parent 7369739bdd
commit d01c1a0232
29 changed files with 563 additions and 444 deletions

View File

@@ -15,10 +15,10 @@ import (
)
func GetLatestContracts(c *gin.Context) {
// Existing parameters
limitStr := c.DefaultQuery("limit", "50")
offsetStr := c.DefaultQuery("offset", "0")
status := strings.Split(c.DefaultQuery("status", models.ContractStatusActive), ",")
// Existing parameters
limitStr := c.DefaultQuery("limit", "50")
offsetStr := c.DefaultQuery("offset", "0")
status := strings.Split(c.DefaultQuery("status", models.ContractStatusActive), ",")
// New/Updated optional parameters
companyName := c.Query("company_name")
@@ -29,6 +29,7 @@ func GetLatestContracts(c *gin.Context) {
startTimeStr := c.Query("start_time")
endTimeStr := c.Query("end_time")
deviceIDsStr := c.QueryArray("deviceIDs[]")
iDsStr := c.QueryArray("ids[]")
// Convert limit and offset to int
limit, err := strconv.Atoi(limitStr)
@@ -76,84 +77,119 @@ func GetLatestContracts(c *gin.Context) {
deviceIDs = append(deviceIDs, id)
}
// Fetch contracts
contracts, total, st, err := contract.GetContracts(status, companyName, companyAddress, companyEmail, companyPhone, &startTime, &endTime, contractName, deviceIDs, 0, nil, limit, offset)
if err != nil {
c.JSON(st, gin.H{"error": err.Error()})
return
}
// Convert ids to []int64
var contractIDs []int64
for _, idStr := range iDsStr {
id, err := strconv.ParseInt(idStr, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid id value"})
return
}
contractIDs = append(contractIDs, id)
}
// Respond with the contracts and the total count
c.JSON(http.StatusOK, gin.H{"total": total, "data": models.ConvertContractToResponse(contracts)})
}
// Fetch contracts
contracts, total, st, err := contract.GetContracts(status, companyName, companyAddress, companyEmail, companyPhone, &startTime, &endTime, contractName, deviceIDs, contractIDs, nil, limit, offset)
func GetBuyerContracts(c *gin.Context) {
// Existing parameters
limitStr := c.DefaultQuery("limit", "50")
offsetStr := c.DefaultQuery("offset", "0")
status := strings.Split(c.DefaultQuery("status", models.ContractStatusActive), ",")
contractIDStr := c.DefaultQuery("contract_id","0")
dateCreatedStr := c.Query("date_created")
// 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 {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid offset value"})
return
}
// Convert limit and offset to int
contractID, err := strconv.Atoi(contractIDStr)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid contractID value"})
return
}
// Convert startTime to time.Time
var dateCreated time.Time
if dateCreatedStr != "" {
startTimeUnix, err := strconv.ParseInt(dateCreatedStr, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid start_time value"})
return
}
dateCreated = time.Unix(startTimeUnix, 0)
}
// Fetch contracts
contracts, total, st, err := contract.GetContracts(status, "", "", "", "", nil, nil, "", nil, contractID, &dateCreated, limit, offset)
if err != nil {
c.JSON(st, gin.H{"error": err.Error()})
return
}
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, "data": models.ConvertContractToResponseModel(contracts)})
c.JSON(http.StatusOK, gin.H{"total": total, "data": models.ConvertContractToResponse(contracts)})
}
func GetBuyerContracts(c *gin.Context) {
// Existing parameters
limitStr := c.DefaultQuery("limit", "50")
offsetStr := c.DefaultQuery("offset", "0")
status := c.QueryArray("status")
iDsStr := c.QueryArray("ids[]")
dateCreatedStr := c.Query("date_created")
startTimeStr := c.Query("start_time")
endTimeStr := c.Query("end_time")
// 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 {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid offset value"})
return
}
// 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
}
startTimeValue := time.Unix(startTimeUnix, 0)
startTime = &startTimeValue
}
// 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
}
endTimeValue := time.Unix(endTimeUnix, 0)
endTime = &endTimeValue
}
// Convert startTime to time.Time
var dateCreated time.Time
if dateCreatedStr != "" {
startTimeUnix, err := strconv.ParseInt(dateCreatedStr, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid start_time value"})
return
}
dateCreated = time.Unix(startTimeUnix, 0)
}
// Convert ids to []int64
var contractIDs []int64
for _, idStr := range iDsStr {
id, err := strconv.ParseInt(idStr, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid id value"})
return
}
contractIDs = append(contractIDs, id)
}
// Fetch contracts
contracts, total, st, err := contract.GetContracts(status, "", "", "", "", startTime, endTime, "", nil, contractIDs, &dateCreated, 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, "data": models.ConvertContractToResponseModel(contracts)})
}
func GetContractStatuses(c *gin.Context) {
// Respond with the contract statuses
c.JSON(http.StatusOK, gin.H{"data": models.GetContractStatuses()})
// Respond with the contract statuses
c.JSON(http.StatusOK, gin.H{"data": models.GetContractStatuses()})
}
func CreateContract(c *gin.Context) {
var payload models.CreateContractRequestPayload
var payload models.CreateContractRequestPayload
if err := c.ShouldBindJSON(&payload); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid JSON payload"})
log.Printf("Invalid JSON payload: %v", err)
@@ -163,16 +199,19 @@ func CreateContract(c *gin.Context) {
db := shared.GetDb()
newContract := models.Contract{
BuyerID: payload.BuyerID,
SellerID: payload.SellerID,
Description: payload.Description,
ProductID: payload.ProductID,
MinTemp: payload.MinTemp,
MaxTemp: payload.MaxTemp,
ArrivalDate: time.Unix(payload.ArrivalDate, 0),
PenaltyType: payload.PenaltyRec,
PenaltyValue : payload.PenaltyValue,
PenaltyRec : payload.PenaltyRec,
BuyerID: payload.BuyerID,
SellerID: payload.SellerID,
Description: payload.Description,
ProductID: payload.ProductID,
MinTemp: payload.MinTemp,
MaxTemp: payload.MaxTemp,
ArrivalDate: time.Unix(payload.ArrivalDate, 0),
PenaltyType: payload.PenaltyRec,
PenaltyValue: payload.PenaltyValue,
PenaltyRec: payload.PenaltyRec,
StartLat: payload.StartLat,
StartLon: payload.StartLon,
Status: models.ContractStatusDraft,
}
if err := db.Create(&newContract).Error; err != nil {
@@ -228,7 +267,6 @@ func UpdateContract(c *gin.Context) {
// Get the contract ID from url parameters
contractIDStr := c.Param("contract_id")
if contractIDStr == "" {
log.Printf("UpdateContract Error: ID is required")
c.JSON(http.StatusBadRequest, gin.H{"error": "ID is required"})
@@ -254,4 +292,3 @@ func UpdateContract(c *gin.Context) {
// Respond with the contracts and the total count
c.JSON(http.StatusOK, gin.H{"data": contractModel})
}