feat: fix order create issues
This commit is contained in:
@@ -157,39 +157,39 @@ func (m *ordersModel) List(ctx context.Context, pagination *requests.Pagination,
|
||||
}
|
||||
|
||||
// Create creates a new order
|
||||
func (m *ordersModel) Create(ctx context.Context, userId, postId int64) (*model.Orders, error) {
|
||||
func (o *ordersModel) Create(ctx context.Context, userId, postId int64) (*model.Orders, error) {
|
||||
post, err := Posts.GetByID(ctx, postId)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to get post")
|
||||
}
|
||||
|
||||
model := &model.Orders{}
|
||||
model.CreatedAt = time.Now()
|
||||
model.UpdatedAt = time.Now()
|
||||
model.Status = fields.OrderStatusPending
|
||||
model.OrderNo = fmt.Sprintf("%s", time.Now().Format("20060102150405"))
|
||||
model.SubOrderNo = model.OrderNo
|
||||
model.UserID = userId
|
||||
model.PostID = postId
|
||||
model.Meta = fields.ToJson(fields.OrderMeta{})
|
||||
model.Price = post.Price
|
||||
model.Discount = post.Discount
|
||||
m := &model.Orders{}
|
||||
m.CreatedAt = time.Now()
|
||||
m.UpdatedAt = time.Now()
|
||||
m.Status = fields.OrderStatusPending
|
||||
m.OrderNo = fmt.Sprintf("%s", time.Now().Format("20060102150405"))
|
||||
m.SubOrderNo = m.OrderNo
|
||||
m.UserID = userId
|
||||
m.PostID = postId
|
||||
m.Meta = fields.ToJson(fields.OrderMeta{})
|
||||
m.Price = post.Price
|
||||
m.Discount = post.Discount
|
||||
|
||||
tbl := table.Orders
|
||||
stmt := tbl.INSERT(tbl.MutableColumns).MODEL(model)
|
||||
m.log.Infof("sql: %s", stmt.DebugSql())
|
||||
stmt := tbl.INSERT(tbl.MutableColumns).MODEL(m).RETURNING(tbl.AllColumns)
|
||||
o.log.Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
||||
m.log.Errorf("error creating order: %v", err)
|
||||
var order model.Orders
|
||||
if err := stmt.QueryContext(ctx, db, &order); err != nil {
|
||||
o.log.Errorf("error creating order: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
return model, nil
|
||||
return &order, nil
|
||||
}
|
||||
|
||||
func (m *ordersModel) SetMeta(ctx context.Context, id int64, metaFunc func(fields.OrderMeta) fields.OrderMeta) error {
|
||||
order, err := m.GetByID(ctx, id)
|
||||
if err != nil {
|
||||
m.log.Errorf("error getting order by ID: %v", err)
|
||||
return errors.Wrap(err, "failed to get order")
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user