Added create contact ednpoint
This commit is contained in:
@@ -9,22 +9,127 @@ import (
|
||||
|
||||
type Contract struct {
|
||||
gorm.Model
|
||||
Name string
|
||||
DeviceIDs pq.Int64Array `gorm:"type:integer[]"`
|
||||
BuyerID uint
|
||||
StartLat float64
|
||||
StartLon float64
|
||||
EndLat float64
|
||||
EndLon float64
|
||||
StartTime time.Time
|
||||
EndTime time.Time
|
||||
Status string
|
||||
BlockchainID string
|
||||
ContractInfos []ContractInfo
|
||||
BuyerName string `gorm:"-"`
|
||||
NumberOfDevices int `gorm:"-"`
|
||||
Name string `json:"name"`
|
||||
DeviceIDs pq.Int64Array `json:"deviceIds" gorm:"type:integer[]"`
|
||||
BuyerID uint `json:"buyerId"`
|
||||
SellerID uint `json:"sellerId"`
|
||||
Description string `json:"description"`
|
||||
StartLat float64 `json:"startLat"`
|
||||
StartLon float64 `json:"startLon"`
|
||||
EndLat float64 `json:"endLat"`
|
||||
EndLon float64 `json:"endLon"`
|
||||
StartTime time.Time `json:"startTime"`
|
||||
EndTime time.Time `json:"endTime"`
|
||||
Status string `json:"status"`
|
||||
BlockchainID string `json:"blockchainId"`
|
||||
ContractInfos []ContractInfo `json:"contractInfos"`
|
||||
ProductID uint `json:"productId"`
|
||||
MaxTemp float64 `json:"maxTemp"`
|
||||
MinTemp float64 `json:"minTemp"`
|
||||
ArrivalDate time.Time `json:"arrivalDate"`
|
||||
PenaltyType string `json:"penaltyType"`
|
||||
PenaltyValue int `json:"penaltyValue"`
|
||||
PenaltyRec string `json:"penaltyRec"`
|
||||
BuyerName string `json:"buyerName" gorm:"-"`
|
||||
NumberOfDevices int `json:"numberOfDevices" gorm:"-"`
|
||||
}
|
||||
|
||||
type ContractResponse struct {
|
||||
BaseModel
|
||||
Name string `json:"name"`
|
||||
DeviceIDs pq.Int64Array `json:"deviceIds" gorm:"type:integer[]"`
|
||||
BuyerID uint `json:"buyerId"`
|
||||
SellerID uint `json:"sellerId"`
|
||||
Description string `json:"description"`
|
||||
StartLat float64 `json:"startLat"`
|
||||
StartLon float64 `json:"startLon"`
|
||||
EndLat float64 `json:"endLat"`
|
||||
EndLon float64 `json:"endLon"`
|
||||
StartTime time.Time `json:"startTime"`
|
||||
EndTime time.Time `json:"endTime"`
|
||||
Status string `json:"status"`
|
||||
BlockchainID string `json:"blockchainId"`
|
||||
ContractInfos []ContractInfo `json:"contractInfos"`
|
||||
ProductID uint `json:"productId"`
|
||||
MaxTemp float64 `json:"maxTemp"`
|
||||
MinTemp float64 `json:"minTemp"`
|
||||
ArrivalDate time.Time `json:"arrivalDate"`
|
||||
PenaltyType string `json:"penaltyType"`
|
||||
PenaltyValue int `json:"penaltyValue"`
|
||||
PenaltyRec string `json:"penaltyRec"`
|
||||
BuyerName string `json:"buyerName" gorm:"-"`
|
||||
NumberOfDevices int `json:"numberOfDevices" gorm:"-"`
|
||||
}
|
||||
|
||||
|
||||
|
||||
func ConvertContractToResponse(contracts []Contract) []ContractResponse {
|
||||
var contractResponses []ContractResponse
|
||||
for _, contract := range contracts {
|
||||
contractResponse := ContractResponse{
|
||||
BaseModel: BaseModel{
|
||||
ID: contract.ID,
|
||||
CreatedAt: contract.CreatedAt,
|
||||
UpdatedAt: contract.UpdatedAt,
|
||||
},
|
||||
Name: contract.Name,
|
||||
DeviceIDs: contract.DeviceIDs,
|
||||
BuyerID: contract.BuyerID,
|
||||
SellerID: contract.SellerID,
|
||||
Description: contract.Description,
|
||||
StartLat: contract.StartLat,
|
||||
StartLon: contract.StartLon,
|
||||
EndLat: contract.EndLat,
|
||||
EndLon: contract.EndLon,
|
||||
StartTime: contract.StartTime,
|
||||
EndTime: contract.EndTime,
|
||||
Status: contract.Status,
|
||||
BlockchainID: contract.BlockchainID,
|
||||
ContractInfos: contract.ContractInfos,
|
||||
ProductID: contract.ProductID,
|
||||
MaxTemp: contract.MaxTemp,
|
||||
MinTemp: contract.MinTemp,
|
||||
ArrivalDate: contract.ArrivalDate,
|
||||
PenaltyType: contract.PenaltyType,
|
||||
PenaltyValue: contract.PenaltyValue,
|
||||
PenaltyRec: contract.PenaltyRec,
|
||||
BuyerName: contract.BuyerName,
|
||||
NumberOfDevices: contract.NumberOfDevices,
|
||||
}
|
||||
contractResponses = append(contractResponses, contractResponse)
|
||||
}
|
||||
return contractResponses
|
||||
}
|
||||
|
||||
func ConvertContractToResponseModel(contracts []Contract) []ListContractResponse {
|
||||
var listInvoiceResponses []ListContractResponse
|
||||
|
||||
// Get all statuses
|
||||
statuses := GetContractStatuses()
|
||||
statusMap := make(map[string]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 = Status{Key: "", Value: ""}
|
||||
}
|
||||
|
||||
listInvoiceResponse := ListContractResponse{
|
||||
Status: KeyValue{Key: status.Key, Value: status.Value},
|
||||
Buyer: CompanyShortResponse{ID: int(contract.BuyerID), Name: contract.BuyerName},
|
||||
ContractID: int(contract.ID),
|
||||
DateCreated: contract.CreatedAt,
|
||||
NumberOfDevices: contract.NumberOfDevices,
|
||||
}
|
||||
listInvoiceResponses = append(listInvoiceResponses, listInvoiceResponse)
|
||||
}
|
||||
return listInvoiceResponses
|
||||
|
||||
}
|
||||
const ContractStatusActive = "active"
|
||||
const ContractStatusPending = "pending"
|
||||
const ContractStatusDraft = "draft"
|
||||
@@ -34,6 +139,13 @@ const ContractStatusExecuted = "executed"
|
||||
const ContractStatusRevoked = "revoked"
|
||||
|
||||
|
||||
const PenaltyTypeAmount = "amount"
|
||||
const PenaltyTypePercentage = "percentage"
|
||||
|
||||
const PenaltyRecDaily = "daily"
|
||||
const PenaltyRecMonthly = "monthly"
|
||||
|
||||
|
||||
type Status struct {
|
||||
Key string `json:"key"`
|
||||
Value string `json:"value"`
|
||||
@@ -41,25 +153,38 @@ type Status struct {
|
||||
|
||||
func GetContractStatuses() []Status {
|
||||
return []Status{
|
||||
{Key: "Active", Value: "active"},
|
||||
{Key: "Pending signature", Value: "pending"},
|
||||
{Key: "Draft", Value: "draft"},
|
||||
{Key: "Signed", Value: "signed"},
|
||||
{Key: "Ready for Activation", Value: "ready_for_activation"},
|
||||
{Key: "Executed", Value: "executed"},
|
||||
{Key: "Revoked", Value: "revoked"},
|
||||
{Value: "Active", Key: "active"},
|
||||
{Value: "Pending signature", Key: "pending"},
|
||||
{Value: "Draft", Key: "draft"},
|
||||
{Value: "Signed", Key: "signed"},
|
||||
{Value: "Ready for Activation", Key: "ready_for_activation"},
|
||||
{Value: "Executed", Key: "executed"},
|
||||
{Value: "Revoked", Key: "revoked"},
|
||||
}
|
||||
}
|
||||
|
||||
type ListContractResponse struct {
|
||||
Status KeyValue `json:"Status"`
|
||||
Buyer CompanyShortResponse `json:"Buyer"`
|
||||
ContractID int
|
||||
NumberOfDevices int
|
||||
DateCreated time.Time
|
||||
Status KeyValue `json:"status"`
|
||||
Buyer CompanyShortResponse `json:"buyer"`
|
||||
ContractID int `json:"contractID"`
|
||||
NumberOfDevices int `json:"numberOfDevices"`
|
||||
DateCreated time.Time `json:"dateCreated"`
|
||||
|
||||
}
|
||||
|
||||
type CreateContractRequestPayload struct {
|
||||
SellerID uint `json:"sellerId" binding:"required"`
|
||||
BuyerID uint `json:"buyerId" binding:"required"`
|
||||
Description string `json:"description"`
|
||||
ProductID uint `json:"productId"`
|
||||
MinTemp float64 `json:"minTemp" binding:"required"`
|
||||
MaxTemp float64 `json:"maxTemp" binding:"required"`
|
||||
ArrivalDate int64 `json:"arrivalDate"`
|
||||
PenaltyType string `json:"penaltyType"`
|
||||
PenaltyValue int `json:"penaltyValue"`
|
||||
PenaltyRec string `json:"penaltyRec"`
|
||||
}
|
||||
|
||||
func (Contract) Update() (bool, error) {
|
||||
return false, nil
|
||||
}
|
||||
@@ -69,3 +194,5 @@ func (Contract) Create() (bool, error) {
|
||||
func (Contract) Delete() (bool, error) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user