Added create contact ednpoint

This commit is contained in:
Nedim
2023-10-06 10:47:26 +02:00
parent f32b5d5748
commit 314abe0462
15 changed files with 1278 additions and 146 deletions

View File

@@ -1,20 +1,29 @@
package models
import "github.com/jinzhu/gorm"
import (
"time"
"github.com/jinzhu/gorm"
)
type BaseModel struct {
ID uint `json:"id" gorm:"primaryKey"`
CreatedAt time.Time `json:"createdAt" gorm:"autoCreateTime"`
UpdatedAt time.Time `json:"updatedAt" gorm:"autoUpdateTime"`
}
type Company struct {
gorm.Model
Name string
Address string
Email string
Phone string
Users []User
Devices []Device
Name string `json:"name"`
Address string `json:"address"`
Email string `json:"email"`
Phone string `json:"phone"`
Users []User `json:"users"`
Devices []Device `json:"devices"`
}
type CompanyShortResponse struct {
ID int
Name string
ID int `json:"id"`
Name string `json:"name"`
}

View File

@@ -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
}

View File

@@ -4,15 +4,54 @@ import (
"github.com/jinzhu/gorm"
)
type Device struct {
gorm.Model
DeviceID string `json:"deviceId"`
DeviceName string
DeviceID string `json:"deviceId"`
DeviceName string `json:"deviceName"`
IMEI string `json:"imei"`
IMSI string `json:"imsi"`
DeviceConfiguration string `gorm:"type:json"`
CompanyID uint
DeviceInfos []DeviceInfo
DeviceConfiguration string `json:"deviceConfiguration" gorm:"type:json"`
CompanyID uint `json:"companyId"`
DeviceInfos *[]DeviceInfo `json:"deviceInfos"`
}
type DeviceResponse struct {
BaseModel
DeviceID string `json:"deviceId"`
DeviceName string `json:"deviceName"`
IMEI string `json:"imei"`
IMSI string `json:"imsi"`
DeviceConfiguration string `json:"deviceConfiguration" gorm:"type:json"`
CompanyID uint `json:"companyId"`
DeviceInfos *[]DeviceInfo `json:"deviceInfos"`
}
func ConvertDeviceToResponse(devices []Device) []DeviceResponse {
var deviceResponses []DeviceResponse
for _, device := range devices {
if device.DeviceInfos == nil {
emptySlice := make([]DeviceInfo, 0)
device.DeviceInfos = &emptySlice
}
deviceResponse := DeviceResponse{
BaseModel: BaseModel{
ID: device.ID,
CreatedAt: device.CreatedAt,
UpdatedAt: device.UpdatedAt,
},
DeviceID: device.DeviceID,
DeviceName: device.DeviceName,
IMEI: device.IMEI,
IMSI: device.IMSI,
DeviceConfiguration: device.DeviceConfiguration,
CompanyID: device.CompanyID,
DeviceInfos: device.DeviceInfos,
}
deviceResponses = append(deviceResponses, deviceResponse)
}
return deviceResponses
}
func (Device) Update() (bool, error) {
@@ -30,3 +69,4 @@ func (d *Device) Delete(db *gorm.DB) (bool, error) {
return true, nil
}

View File

@@ -1,6 +1,8 @@
package models
import "github.com/jinzhu/gorm"
import (
"github.com/jinzhu/gorm"
)
// Location holds latitude and longitude.
type Location struct {
@@ -10,17 +12,15 @@ type Location struct {
// ImportantInfo holds fields that are important for quick access.
type SensorData struct {
IMEI string `json:"imei"`
IMSI string `json:"imsi"`
Timestamp int64 `json:"timestamp"`
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
WifiLoc Location `json:"wifi_location"`
CellLoc Location `json:"cell_location"`
Temperature float64 `json:"temperature"`
// Used to parse incoming json data
AccInfo AccelerometerInfo `json:"accelerometerInfo"`
// Used to store coordinates of accelerometer to DB
IMEI string `json:"imei"`
IMSI string `json:"imsi"`
Timestamp int64 `json:"timestamp"`
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
WifiLoc Location `json:"wifiLocation"`
CellLoc Location `json:"cellLocation"`
Temperature float64 `json:"temperature"`
AccInfo AccelerometerInfo `json:"accelerometerInfo"`
AccelerometerInfo
}
@@ -33,12 +33,38 @@ type DeviceInfo struct {
}
type AccelerometerInfo struct {
X int64
Y int64
Z int64
type DeviceInfoResponse struct {
BaseModel
RawJSON string `json:"rawJson" gorm:"type:json"`
SensorData
DeviceID uint `json:"deviceId"`
ExternalDeviceID string `json:"externalDeviceId"`
}
func ConvertDeviceInfoToResponse(deviceInfos []DeviceInfo) []DeviceInfoResponse {
var deviceInfoResponses []DeviceInfoResponse
for _, deviceInfo := range deviceInfos {
deviceInfoResponse := DeviceInfoResponse{
BaseModel: BaseModel{
ID: deviceInfo.ID,
CreatedAt: deviceInfo.CreatedAt,
UpdatedAt: deviceInfo.UpdatedAt,
},
RawJSON: deviceInfo.RawJSON,
SensorData: deviceInfo.SensorData,
DeviceID: deviceInfo.DeviceID,
ExternalDeviceID: deviceInfo.ExternalDeviceID,
}
deviceInfoResponses = append(deviceInfoResponses, deviceInfoResponse)
}
return deviceInfoResponses
}
type AccelerometerInfo struct {
X int64 `json:"x"`
Y int64 `json:"y"`
Z int64 `json:"z"`
}
type GeoJSONFeatureCollection struct {
Type string `json:"type"`
Features []GeoJSONFeature `json:"features"`
@@ -47,7 +73,7 @@ type GeoJSONFeatureCollection struct {
type GeoJSONFeature struct {
Type string `json:"type"`
Geometry GeoJSONGeometry `json:"geometry"`
DeviceInfo *DeviceInfo `json:",omitempty"`
DeviceInfoResponse *DeviceInfoResponse `json:"deviceInfo,omitempty"`
Properties map[string]interface{} `json:"properties"`
}
@@ -56,7 +82,6 @@ type GeoJSONGeometry struct {
Coordinates []float64 `json:"coordinates"`
}
func (DeviceInfo) Update() (bool, error) {
return false, nil
}

View File

@@ -1,6 +1,7 @@
package models
import (
"database/sql"
"time"
"github.com/jinzhu/gorm"
@@ -8,42 +9,113 @@ import (
type Invoice struct {
gorm.Model
BuyerID uint
BuyerName string
BuyerAddress string
BuyerEmail string
BuyerPhone string
SellerID uint
SellerName string
SellerAddress string
SellerEmail string
SellerPhone string
PriceCents int64 `gorm:"column:price_cents"`
Discount int64
Tax int64
TermsAndConditions string
InvoiceName string
InvoiceDate time.Time
InvoiceDueDate time.Time
ContractID uint
InvoiceItem [] InvoiceItem
Status string
ID uint `json:"id" gorm:"primaryKey"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt sql.NullTime `json:"deletedAt" gorm:"index"`
BuyerID uint `json:"buyerId"`
BuyerName string `json:"buyerName"`
BuyerAddress string `json:"buyerAddress"`
BuyerEmail string `json:"buyerEmail"`
BuyerPhone string `json:"buyerPhone"`
SellerID uint `json:"sellerId"`
SellerName string `json:"sellerName"`
SellerAddress string `json:"sellerAddress"`
SellerEmail string `json:"sellerEmail"`
SellerPhone string `json:"sellerPhone"`
PriceCents int64 `json:"priceCents" gorm:"column:price_cents"`
Discount int64 `json:"discount"`
Tax int64 `json:"tax"`
TermsAndConditions string `json:"termsAndConditions"`
InvoiceName string `json:"invoiceName"`
InvoiceDate time.Time `json:"invoiceDate"`
InvoiceDueDate time.Time `json:"invoiceDueDate"`
ContractID uint `json:"contractId"`
InvoiceItem *[]InvoiceItem `json:"invoiceItem"`
Status string `json:"status"`
}
type InvoiceResponse struct {
BaseModel
ID uint `json:"id" gorm:"primaryKey"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt sql.NullTime `json:"deletedAt" gorm:"index"`
BuyerID uint `json:"buyerId"`
BuyerName string `json:"buyerName"`
BuyerAddress string `json:"buyerAddress"`
BuyerEmail string `json:"buyerEmail"`
BuyerPhone string `json:"buyerPhone"`
SellerID uint `json:"sellerId"`
SellerName string `json:"sellerName"`
SellerAddress string `json:"sellerAddress"`
SellerEmail string `json:"sellerEmail"`
SellerPhone string `json:"sellerPhone"`
PriceCents int64 `json:"priceCents" gorm:"column:price_cents"`
Discount int64 `json:"discount"`
Tax int64 `json:"tax"`
TermsAndConditions string `json:"termsAndConditions"`
InvoiceName string `json:"invoiceName"`
InvoiceDate time.Time `json:"invoiceDate"`
InvoiceDueDate time.Time `json:"invoiceDueDate"`
ContractID uint `json:"contractId"`
InvoiceItem *[]InvoiceItem `json:"invoiceItem"`
Status string `json:"status"`
}
func ConvertInvoiceToResponse(invoices []Invoice) []InvoiceResponse {
var invoiceResponses []InvoiceResponse
for _, invoice := range invoices {
if invoice.InvoiceItem == nil {
emptySlice := make([]InvoiceItem, 0)
invoice.InvoiceItem = &emptySlice
}
invoiceResponse := InvoiceResponse{
BaseModel: BaseModel{
ID: invoice.ID,
CreatedAt: invoice.CreatedAt,
UpdatedAt: invoice.UpdatedAt,
},
BuyerID: invoice.BuyerID,
BuyerName: invoice.BuyerName,
BuyerAddress: invoice.BuyerAddress,
BuyerEmail: invoice.BuyerEmail,
BuyerPhone: invoice.BuyerPhone,
SellerID: invoice.SellerID,
SellerName: invoice.SellerName,
SellerAddress: invoice.SellerAddress,
SellerEmail: invoice.SellerEmail,
SellerPhone: invoice.SellerPhone,
PriceCents: invoice.PriceCents,
Discount: invoice.Discount,
Tax: invoice.Tax,
TermsAndConditions: invoice.TermsAndConditions,
InvoiceName: invoice.InvoiceName,
InvoiceDate: invoice.InvoiceDate,
InvoiceDueDate: invoice.InvoiceDueDate,
ContractID: invoice.ContractID,
InvoiceItem: invoice.InvoiceItem,
Status: invoice.Status,
}
invoiceResponses = append(invoiceResponses, invoiceResponse)
}
return invoiceResponses
}
type ListInvoiceResponse struct {
Status KeyValue `json:"Status"`
Buyer CompanyShortResponse `json:"Buyer"`
ContractID int
DateCreated time.Time
DueDate time.Time
Amount string
Status KeyValue `json:"status"`
Buyer CompanyShortResponse `json:"buyer"`
ContractID int `json:"contractId"`
DateCreated time.Time `json:"dateCreated"`
DueDate time.Time `json:"dueDate"`
Amount string `json:"amount"`
}
type KeyValue struct {
Key string
Value string
Key string `json:"key"`
Value string `json:"value"`
}

View File

@@ -1,18 +1,60 @@
package models
import (
"database/sql"
"time"
"github.com/jinzhu/gorm"
)
type InvoiceItem struct {
gorm.Model
Description string
Quantity int64
Unit string
PriceCents int64 `gorm:"column:price_cents"`
InvoiceID uint
ID uint `json:"id" gorm:"primaryKey"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt sql.NullTime `json:"deletedAt" gorm:"index"`
Description string `json:"description"`
Quantity int64 `json:"quantity"`
Unit string `json:"unit"`
PriceCents int64 `json:"priceCents" gorm:"column:price_cents"`
InvoiceID uint `json:"invoiceId"`
}
type InvoiceItemResponse struct {
BaseModel
ID uint `json:"id" gorm:"primaryKey"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt sql.NullTime `json:"deletedAt" gorm:"index"`
Description string `json:"description"`
Quantity int64 `json:"quantity"`
Unit string `json:"unit"`
PriceCents int64 `json:"priceCents" gorm:"column:price_cents"`
InvoiceID uint `json:"invoiceId"`
}
// ConvertSliceOfInvoiceItemToResponse converts a slice of InvoiceItem models to a slice of InvoiceItemResponse models
func ConvertInvoiceItemToResponse(items []InvoiceItem) []InvoiceItemResponse {
var itemResponses []InvoiceItemResponse
for _, item := range items {
itemResponse := InvoiceItemResponse{
BaseModel: BaseModel{
ID: item.ID,
CreatedAt: item.CreatedAt,
UpdatedAt: item.UpdatedAt,
},
Description: item.Description,
Quantity: item.Quantity,
Unit: item.Unit,
PriceCents: item.PriceCents,
InvoiceID: item.InvoiceID,
}
itemResponses = append(itemResponses, itemResponse)
}
return itemResponses
}
func (InvoiceItem) Update() (bool, error) {

View File

@@ -1,14 +1,23 @@
package models
import "github.com/jinzhu/gorm"
import (
"database/sql"
"time"
"github.com/jinzhu/gorm"
)
type User struct {
gorm.Model
Username string
Password string
Email string
Avatar string
CompanyID uint
ID uint `json:"id" gorm:"primaryKey"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt sql.NullTime `json:"deletedAt" gorm:"index"`
Username string `json:"username"`
Password string `json:"password"`
Email string `json:"email"`
Avatar string `json:"avatar"`
CompanyID uint `json:"companyId"`
}
func (User) Update() (bool, error) {