Home > Software design >  One to many inserting
One to many inserting

Time:08-02

I have two models: Order and OrderItem. I need to insert order and item of it from same request, e.g.:

{
    "user_id": "1",
    "total_price": "200",
    "items": [
        {
            "product_id": 1,
            "quantity": 10
        },
        {
            "product_id": 2,
            "quantity": 5
        },
        {
            "product_id": 3,
            "quantity":3
        }
    ]
}

This is Order and OrderItem model

=========================== Order ===========================
type Order struct {
    ID          int       `boil:"id" json:"id" toml:"id" yaml:"id"`
    OrderNumber string    `boil:"order_number" json:"order_number" toml:"order_number" yaml:"order_number"`
    OrderDate   time.Time `boil:"order_date" json:"order_date" toml:"order_date" yaml:"order_date"`
    Status      string    `boil:"status" json:"status" toml:"status" yaml:"status"`
    Note        string    `boil:"note" json:"note" toml:"note" yaml:"note"`
    UserID      int       `boil:"user_id" json:"user_id" toml:"user_id" yaml:"user_id"`
    CreatedAt   time.Time `boil:"created_at" json:"created_at" toml:"created_at" yaml:"created_at"`
    UpdatedAt   time.Time `boil:"updated_at" json:"updated_at" toml:"updated_at" yaml:"updated_at"`

    R *orderR `boil:"-" json:"-" toml:"-" yaml:"-"`
    L orderL  `boil:"-" json:"-" toml:"-" yaml:"-"`
}

=========================== OrderItem ===========================
type OrderItem struct {
    ID           int       `boil:"id" json:"id" toml:"id" yaml:"id"`
    OrderID      int       `boil:"order_id" json:"order_id" toml:"order_id" yaml:"order_id"`
    ProductID    int       `boil:"product_id" json:"product_id" toml:"product_id" yaml:"product_id"`
    ProductPrice float64   `boil:"product_price" json:"product_price" toml:"product_price" yaml:"product_price"`
    ProductName  string    `boil:"product_name" json:"product_name" toml:"product_name" yaml:"product_name"`
    Quantity     int       `boil:"quantity" json:"quantity" toml:"quantity" yaml:"quantity"`
    Discount     float64   `boil:"discount" json:"discount" toml:"discount" yaml:"discount"`
    Note         string    `boil:"note" json:"note" toml:"note" yaml:"note"`
    CreatedAt    time.Time `boil:"created_at" json:"created_at" toml:"created_at" yaml:"created_at"`
    UpdatedAt    time.Time `boil:"updated_at" json:"updated_at" toml:"updated_at" yaml:"updated_at"`

    R *orderItemR `boil:"-" json:"-" toml:"-" yaml:"-"`
    L orderItemL  `boil:"-" json:"-" toml:"-" yaml:"-"`
}

What do people usually do? Is there a way to do this quickly with sqlboiler?

CodePudding user response:

I think you needn't to do that, you can insert order model and orderItems model in different requests. This solution provide more option than for client. If you need to update or add new order item into the order, you also need this API to solve it.

CodePudding user response:

Create some models like

type OrderItemInput struct {
    ProductId int `json:"product_id"`
    Quantity  int `json:"quantity"`
}

type OrderInsertInput struct {
    UserID      int        `json:"user_id"`
    TotalPrice  float64    `json:"total_price"`
    Items []OrderItemInput `json:"items"`
}

Create new Order by fields UserId and TotalPrice of OrderInsertInput.

When there was OrderID, we will create OrderItem with each OrderItemInput and the OrderID.

  • Related