upstream sync
This commit is contained in:
@@ -56,5 +56,5 @@ func ListBuyers(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Respond with the buyers and the total count
|
// Respond with the buyers and the total count
|
||||||
c.JSON(http.StatusOK, gin.H{"total": total, "buyers": buyers})
|
c.JSON(http.StatusOK, gin.H{"total": total, "data": buyers})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package controllers
|
package controllers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -19,61 +20,61 @@ func GetLatestContracts(c *gin.Context) {
|
|||||||
offsetStr := c.DefaultQuery("offset", "0")
|
offsetStr := c.DefaultQuery("offset", "0")
|
||||||
status := strings.Split(c.DefaultQuery("status", models.ContractStatusActive), ",")
|
status := strings.Split(c.DefaultQuery("status", models.ContractStatusActive), ",")
|
||||||
|
|
||||||
// New/Updated optional parameters
|
// New/Updated optional parameters
|
||||||
companyName := c.Query("company_name")
|
companyName := c.Query("company_name")
|
||||||
companyAddress := c.Query("company_address")
|
companyAddress := c.Query("company_address")
|
||||||
companyEmail := c.Query("company_email")
|
companyEmail := c.Query("company_email")
|
||||||
companyPhone := c.Query("company_phone")
|
companyPhone := c.Query("company_phone")
|
||||||
contractName := c.Query("contract_name")
|
contractName := c.Query("contract_name")
|
||||||
startTimeStr := c.Query("start_time")
|
startTimeStr := c.Query("start_time")
|
||||||
endTimeStr := c.Query("end_time")
|
endTimeStr := c.Query("end_time")
|
||||||
deviceIDsStr := c.QueryArray("deviceIDs[]")
|
deviceIDsStr := c.QueryArray("deviceIDs[]")
|
||||||
|
|
||||||
// Convert limit and offset to int
|
// Convert limit and offset to int
|
||||||
limit, err := strconv.Atoi(limitStr)
|
limit, err := strconv.Atoi(limitStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid limit value"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid limit value"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
offset, err := strconv.Atoi(offsetStr)
|
offset, err := strconv.Atoi(offsetStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid offset value"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid offset value"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert startTime to time.Time
|
// Convert startTime to time.Time
|
||||||
var startTime time.Time
|
var startTime time.Time
|
||||||
if startTimeStr != "" {
|
if startTimeStr != "" {
|
||||||
startTimeUnix, err := strconv.ParseInt(startTimeStr, 10, 64)
|
startTimeUnix, err := strconv.ParseInt(startTimeStr, 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid start_time value"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid start_time value"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
startTime = time.Unix(startTimeUnix, 0)
|
startTime = time.Unix(startTimeUnix, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert endTime to time.Time
|
// Convert endTime to time.Time
|
||||||
var endTime time.Time
|
var endTime time.Time
|
||||||
if endTimeStr != "" {
|
if endTimeStr != "" {
|
||||||
endTimeUnix, err := strconv.ParseInt(endTimeStr, 10, 64)
|
endTimeUnix, err := strconv.ParseInt(endTimeStr, 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid end_time value"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid end_time value"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
endTime = time.Unix(endTimeUnix, 0)
|
endTime = time.Unix(endTimeUnix, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert deviceIDs to []int64
|
// Convert deviceIDs to []int64
|
||||||
var deviceIDs []int64
|
var deviceIDs []int64
|
||||||
for _, idStr := range deviceIDsStr {
|
for _, idStr := range deviceIDsStr {
|
||||||
id, err := strconv.ParseInt(idStr, 10, 64)
|
id, err := strconv.ParseInt(idStr, 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid deviceID value"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid deviceID value"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
deviceIDs = append(deviceIDs, id)
|
deviceIDs = append(deviceIDs, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch contracts
|
// Fetch contracts
|
||||||
contracts, total, st, err := contract.GetContracts(status, companyName, companyAddress, companyEmail, companyPhone, &startTime, &endTime, contractName, deviceIDs, 0, nil, limit, offset)
|
contracts, total, st, err := contract.GetContracts(status, companyName, companyAddress, companyEmail, companyPhone, &startTime, &endTime, contractName, deviceIDs, 0, nil, limit, offset)
|
||||||
@@ -139,7 +140,7 @@ func GetBuyerContracts(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Respond with the contracts and the total count
|
// 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.ConvertContractToResponseModel(contracts)})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -187,4 +188,73 @@ func CreateContract(c *gin.Context) {
|
|||||||
c.JSON(http.StatusOK, gin.H{"message": "Successfully received and saved contract", "id": newContract.ID})
|
c.JSON(http.StatusOK, gin.H{"message": "Successfully received and saved contract", "id": newContract.ID})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetContractByID(c *gin.Context) {
|
||||||
|
// Get the contract ID from url parameters
|
||||||
|
contractIDStr := c.Param("contract_id")
|
||||||
|
|
||||||
|
if contractIDStr == "" {
|
||||||
|
log.Printf("GetContractByID Error: ID is required")
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "ID is required"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
contractID, err := strconv.ParseUint(contractIDStr, 10, 32)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("GetContractByID Error: Invalid ID: %v", err)
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid ID"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch contract
|
||||||
|
contract, st, err := contract.GetContractByID(contractID)
|
||||||
|
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{"data": contract})
|
||||||
|
}
|
||||||
|
|
||||||
|
func UpdateContract(c *gin.Context) {
|
||||||
|
|
||||||
|
var contractModel models.Contract
|
||||||
|
rawData, _ := c.GetRawData()
|
||||||
|
|
||||||
|
// Unmarshal to the important info structure
|
||||||
|
err := json.Unmarshal(rawData, &contractModel)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid JSON payload"})
|
||||||
|
log.Printf("Invalid json pyload : %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
contractID, err := strconv.ParseUint(contractIDStr, 10, 32)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("UpdateContract Error: Invalid ID: %v", err)
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid ID"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
contractModel.ID = uint(contractID)
|
||||||
|
|
||||||
|
// update contract
|
||||||
|
contractModel, st, err := contract.UpdateContract(contractModel)
|
||||||
|
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{"data": contractModel})
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ func ListProductTemplates(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Respond with the product templates and the total count
|
// Respond with the product templates and the total count
|
||||||
c.JSON(http.StatusOK, gin.H{"total": total, "product_templates": productTemplates})
|
c.JSON(http.StatusOK, gin.H{"total": total, "data": productTemplates})
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetProductTemplate(c *gin.Context) {
|
func GetProductTemplate(c *gin.Context) {
|
||||||
|
|||||||
@@ -111,18 +111,27 @@ func GetContracts(status []string, companyName string, companyAddress string,
|
|||||||
return contracts, total, http.StatusOK, nil
|
return contracts, total, http.StatusOK, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func UpdateContract(contract models.Contract) (models.Contract, int, error) {
|
||||||
|
|
||||||
|
// Update contract
|
||||||
|
if err := shared.GetDb().Update(contract).Error; err != nil {
|
||||||
|
log.Printf("UpdateContractByID Error: Could not update contract: %v", err)
|
||||||
|
return contract, http.StatusInternalServerError, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return GetContractByID(uint64(contract.ID))
|
||||||
|
|
||||||
func GetContractByID(contractID uint64) (models.Contract, int ,error) {
|
}
|
||||||
|
|
||||||
|
func GetContractByID(contractID uint64) (models.Contract, int, error) {
|
||||||
|
|
||||||
// Fetch the contract creation date based on contractID
|
// Fetch the contract creation date based on contractID
|
||||||
var contract models.Contract
|
var contract models.Contract
|
||||||
if err := shared.GetDb().Where("id = ?", contractID).First(&contract).Error; err != nil {
|
if err := shared.GetDb().Where("id = ?", contractID).First(&contract).Error; err != nil {
|
||||||
log.Printf("GetDeviceData Error: Could not fetch contract: %v", err)
|
log.Printf("GetContractByID Error: Could not fetch contract: %v", err)
|
||||||
return contract, http.StatusInternalServerError, err
|
return contract, http.StatusInternalServerError, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return contract , http.StatusOK, nil
|
return contract, http.StatusOK, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -15,7 +15,7 @@ func RegisterPublicRoutes(r *gin.Engine) {
|
|||||||
|
|
||||||
// Invoices
|
// Invoices
|
||||||
r.GET("/invoices", controllers.GetInvoices)
|
r.GET("/invoices", controllers.GetInvoices)
|
||||||
r.GET("/invoice/:id", controllers.GetInvoiceByID)
|
r.GET("/invoice/:id", controllers.GetInvoiceByID)
|
||||||
|
|
||||||
r.POST("/device_data/save", controllers.SaveDeviceInfo)
|
r.POST("/device_data/save", controllers.SaveDeviceInfo)
|
||||||
r.GET("/buyers/", controllers.ListBuyers)
|
r.GET("/buyers/", controllers.ListBuyers)
|
||||||
@@ -29,4 +29,6 @@ func RegisterPublicRoutes(r *gin.Engine) {
|
|||||||
r.GET("/contracts", controllers.GetBuyerContracts)
|
r.GET("/contracts", controllers.GetBuyerContracts)
|
||||||
r.POST("/contracts/create", controllers.CreateContract)
|
r.POST("/contracts/create", controllers.CreateContract)
|
||||||
|
|
||||||
|
r.GET("/contracts/:contract_id", controllers.GetContractByID)
|
||||||
|
r.PATCH("/contracts/:contract_id", controllers.UpdateContract)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user