Created
July 16, 2024 16:19
-
-
Save BK1031/0a81ca551f9580682ffce901f4f05605 to your computer and use it in GitHub Desktop.
singlestore-go-bookstore UpdateOrder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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