Skip to content

Instantly share code, notes, and snippets.

@BK1031
Created July 16, 2024 16:19
Show Gist options
  • Save BK1031/0a81ca551f9580682ffce901f4f05605 to your computer and use it in GitHub Desktop.
Save BK1031/0a81ca551f9580682ffce901f4f05605 to your computer and use it in GitHub Desktop.
singlestore-go-bookstore UpdateOrder
func UpdateOrder(order model.Order) (model.Order, error) {
if len(order.Items) == 0 {
return model.Order{}, errors.New("order items are required")
}
for i, item := range order.Items {
if item.Quantity <= 0 {
return model.Order{}, errors.New("quantity must be greater than 0")
}
book, err := GetBook(item.BookID)
if err != nil {
return model.Order{}, err
}
order.Items[i].Book = book
}
order.CalculateTotal()
database.DB.Model(&order).Updates(order)
for i := range order.Items {
order.Items[i].OrderID = order.ID
}
database.DB.Where("order_id = ?", order.ID).Delete(&model.OrderItem{})
database.DB.Model(&model.OrderItem{}).Updates(order.Items)
return order, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment