Added create contact ednpoint
This commit is contained in:
@@ -1,20 +1,23 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gitlab.com/pactual1/backend/database/contract"
|
||||
"gitlab.com/pactual1/backend/models"
|
||||
"gitlab.com/pactual1/backend/shared"
|
||||
)
|
||||
|
||||
func GetLatestContracts(c *gin.Context) {
|
||||
// Existing parameters
|
||||
limitStr := c.DefaultQuery("limit", "50")
|
||||
offsetStr := c.DefaultQuery("offset", "0")
|
||||
status := c.DefaultQuery("status", models.ContractStatusActive)
|
||||
status := strings.Split(c.DefaultQuery("status", models.ContractStatusActive), ",")
|
||||
|
||||
// New/Updated optional parameters
|
||||
companyName := c.Query("company_name")
|
||||
@@ -81,7 +84,7 @@ func GetLatestContracts(c *gin.Context) {
|
||||
}
|
||||
|
||||
// Respond with the contracts and the total count
|
||||
c.JSON(http.StatusOK, gin.H{"total": total, "data": contracts})
|
||||
c.JSON(http.StatusOK, gin.H{"total": total, "data": models.ConvertContractToResponse(contracts)})
|
||||
}
|
||||
|
||||
|
||||
@@ -89,7 +92,7 @@ func GetBuyerContracts(c *gin.Context) {
|
||||
// Existing parameters
|
||||
limitStr := c.DefaultQuery("limit", "50")
|
||||
offsetStr := c.DefaultQuery("offset", "0")
|
||||
status := c.Query("status")
|
||||
status := strings.Split(c.DefaultQuery("status", models.ContractStatusActive), ",")
|
||||
|
||||
contractIDStr := c.DefaultQuery("contract_id","0")
|
||||
dateCreatedStr := c.Query("date_created")
|
||||
@@ -137,44 +140,51 @@ func GetBuyerContracts(c *gin.Context) {
|
||||
}
|
||||
|
||||
// Respond with the contracts and the total count
|
||||
c.JSON(http.StatusOK, gin.H{"total": total, "data": convertContractToResponseModel(contracts)})
|
||||
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{"statuses": models.GetContractStatuses()})
|
||||
}
|
||||
|
||||
|
||||
func convertContractToResponseModel(contracts []models.Contract) []models.ListContractResponse {
|
||||
var listInvoiceResponses []models.ListContractResponse
|
||||
|
||||
// Get all statuses
|
||||
statuses := models.GetContractStatuses()
|
||||
statusMap := make(map[string]models.Status)
|
||||
for _, s := range statuses {
|
||||
statusMap[s.Value] = s
|
||||
}
|
||||
|
||||
for _, contract := range contracts {
|
||||
// Get the status based on Value in the DB
|
||||
status, ok := statusMap[contract.Status]
|
||||
if !ok {
|
||||
status = models.Status{Key: "Unknown", Value: "unknown"}
|
||||
}
|
||||
|
||||
listInvoiceResponse := models.ListContractResponse{
|
||||
Status: models.KeyValue{Key: status.Key, Value: status.Value},
|
||||
Buyer: models.CompanyShortResponse{ID: int(contract.BuyerID), Name: contract.BuyerName},
|
||||
ContractID: int(contract.ID),
|
||||
DateCreated: contract.CreatedAt,
|
||||
NumberOfDevices: contract.NumberOfDevices,
|
||||
}
|
||||
listInvoiceResponses = append(listInvoiceResponses, listInvoiceResponse)
|
||||
}
|
||||
return listInvoiceResponses
|
||||
c.JSON(http.StatusOK, gin.H{"data": models.GetContractStatuses()})
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
func CreateContract(c *gin.Context) {
|
||||
|
||||
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)
|
||||
return
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
|
||||
if err := db.Create(&newContract).Error; err != nil {
|
||||
log.Printf("SaveContractInfo CREATE - Contract DB Error: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not create new contract"})
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("Successfully received and saved contract: %v", newContract)
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Successfully received and saved contract", "id": newContract.ID})
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user