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"`
}
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 []OrderProduct `gorm:"foreignKey:OrderId"json:"products"`
}
type OrderProduct struct {
ID uint `gorm:"primaryKey;autoIncrement;unique"json:"id"`
// добавляем это поле, чтобы была связь с таблицей Orders
OrderID uint `json:"-"`
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 []OrderProduct `gorm:"foreignKey:OrderId"json:"products"`
type OrderProduct struct
OrderID uint `json:"-"`
type Order struct {
ID uint `gorm:"primaryKey;autoIncrement;unique" json:"id"`
Order []OrderProduct `json:"products"`
}
type OrderProduct struct {
OrderID uint `json:"-"`
}
type Order struct {
ID uint `gorm:"primaryKey;autoIncrement;unique" json:"id"`
Order []OrderProduct `gorm:"foreignkey:OrderID" json:"products"`
}
type OrderProduct struct {
OrderID uint `json:"-"`
}
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:"foreignkey:OrderID" json:"order"`
}
type OrderStructure struct {
ID uint `gorm:"primaryKey;autoIncrement;unique"json:"id"`
OrderID int `json:"-"`
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"`
}
type Order struct {
...
// меняем тут тип
Order datatypes.JSON `json:"order"`
}
// заполняете orderStructures
// потом преобразуем структуры в []bytes,
jsonData, err := json.Marshal(orderStructures)
// потом заполняем поле
order.Order = datatypes.JSON(jsonData)
// тут сохраняем
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"
}
]
// OrderProduct товар в заказе.
type OrderProduct struct {
ID uint `gorm:"primaryKey;autoIncrement;unique"json:"id"`
OrderID uint `json:"-"`
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"`
}
{"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
}
// OrderRequest запрос на оформление заказа.
type OrderRequest struct {
Products []OrderProduct `json:"items"`
Phone int `json:"phone"`
}
// OrderProduct запись товара.
type OrderProduct struct {
ID uint `gorm:"primaryKey;autoIncrement;unique" json:"id"`
OrderID uint `json:"-"`
ItemID uint `json:"item_id"`
Brand string `json:"brand"`
Img string `json:"img"`
ItemTotal uint `json:"item_total"`
Price string `json:"price"`
Quantity uint `json:"quantity"`
Title string `json:"title"`
Type string `json:"type"`
}
// 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"`
Products []OrderProduct `gorm:"foreignkey:OrderID" json:"order"`
// возможно foreignkey:order_id, не могу точно сказать, с GORM не работал ранее
}
func PostOrder(c *gin.Context) {
var req OrderRequest
if err := c.ShouldBindJSON(&req); err != nil {
fmt.Println(err.Error())
return
}
// т.к. мы используем одну структуру OrderProduct, чтобы избежать намеренного искажения данных кем-то
// обнулим эти поля.
for idx := range req.Products {
req.Products[idx].ID = 0
req.Products[idx].OrderID = 0
}
owner := c.Params.ByName("id")
order = Order{
Products: req.Products,
Owner: owner,
Status: "ordered",
Phone: orderRequest.Phone,
}
database.DB.Create(&order)
c.JSON(201, gin.H{"message": order})
}