{"items": [
{
"brand": "brand",
"id": 6,
"img": "img",
"itemTotal": 734000,
"price": "734000",
"quantity": 1,
"title": "title",
"type": "type"
},
{
"brand": "brand",
"id": 10,
"img": "img",
"itemtotal": 250000,
"price": "250000",
"quantity": 1,
"title": "title",
"type": "type"
}
],
"phone": 81241421431
}
type OrderReq struct {
Items []Order `json:"items"`
Phone int
}
type Order struct {
Brand string
Id uint
Img string
ItemTotal uint
Price string
Quantity uint
Title string
Type string
}
var order models.Order
var orders []models.OrderStructure
func PostOrder(c *gin.Context) {
var orderBody OrderReq
owner := c.Params.ByName("id")
if err := c.ShouldBindJSON(&orderBody); err != nil {
fmt.Println(err.Error())
return
}
for _, item := range orderBody.Items {
orderStruct := models.OrderStructure{
Brand: item.Brand,
ItemId: int(item.Id),
Img: item.Img,
ItemTotal: int(item.ItemTotal),
Price: item.Price,
Quantity: int(item.Quantity),
Title: item.Title,
Type: item.Type,
}
orders = append(orders, orderStruct)
}
order = models.Order{
Order: orders,
Owner: owner,
Status: "ordered",
Phone: orderBody.Phone,
}
database.DB.Create(&order)
c.JSON(201, gin.H{"message": order})
}
type Order struct {
ID uint `gorm:"primaryKey;autoIncrement;unique"json:"id"`
Owner string `json:"owner"`
Status string `gorm:"default:ordered"json:"status"`
Phone int `json:"phone"`
Order []OrderStructure `gorm:"type:json[]"json:"order"`
}
type OrderStructure struct {
ID uint `gorm:"primaryKey;autoIncrement;unique"json:"id"`
Brand string `json:"brand"`
ItemId int `json:"item_id"`
Img string `json:"img"`
ItemTotal int `json:"item_total"`
Price string `json:"price"`
Quantity int `json:"quantity"`
Title string `json:"title"`
Type string `json:"type"`
}
"order": [
{
"id": 0,
"brand": "brand",
"item_id": 6,
"img": "img",
"item_total": 734000,
"price": "734000",
"quantity": 1,
"title": "title,
"type": "type"
},
{
"id": 0,
"brand": "brand",
"item_id": 10,
"img": "img",
"item_total": 250000,
"price": "250000",
"quantity": 1,
"title": "title",
"type": "type"
}
]
"ewogICAgImVtYWlsIjogInRlc3RAZ21haWwuY29tIiwKICAgICJwYXNzd29yZCI6ICJ0ZXN0Igp9"