This file contains hidden or 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 TestCreateOrder(t *testing.T) { | |
| ResetOrderTable() | |
| ResetBookTable() | |
| // Arrange | |
| gatsby, _ := CreateBook(model.Book{ | |
| Title: "The Great Gatsby", | |
| Author: "F. Scott Fitzgerald", | |
| Price: 29.99, | |
| Genre: "Fiction", | |
| }) |
This file contains hidden or 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 CreateOrder(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 { |
This file contains hidden or 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 (o *Order) CalculateTotal() { | |
| o.Total = 0 | |
| for _, item := range o.Items { | |
| o.Total += item.Book.Price * float64(item.Quantity) | |
| } | |
| } |
This file contains hidden or 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 ResetBookTable() { | |
| database.DB.Where("1 = 1").Delete(&model.Book{}) | |
| } | |
| func ResetOrderTable() { | |
| database.DB.Where("1 = 1").Delete(&model.Order{}) | |
| } |
This file contains hidden or 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
| package service | |
| import "bookstore/model" | |
| func CreateOrder(order model.Order) (model.Order, error) { | |
| return model.Order{}, nil | |
| } | |
| func UpdateOrder(order model.Order) (model.Order, error) { | |
| return model.Order{}, nil |
This file contains hidden or 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 TestUpdateBook(t *testing.T) { | |
| ResetBookTable() | |
| t.Run("Success", func(t *testing.T) { | |
| // Arrange | |
| book := model.Book{ | |
| Title: "The Great Gatsby", | |
| Author: "F. Scott Fitzgerald", | |
| Price: 29.99, | |
| Genre: "Fiction", | |
| } |
This file contains hidden or 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 TestCreateBook(t *testing.T) { | |
| ResetBookTable() | |
| createdID := 0 | |
| t.Run("Success", func(t *testing.T) { | |
| // Arrange | |
| book := model.Book{ | |
| Title: "The Great Gatsby", | |
| Author: "F. Scott Fitzgerald", | |
| Price: 29.99, | |
| Genre: "Fiction", |
This file contains hidden or 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
| package service | |
| import ( | |
| "bookstore/database" | |
| "bookstore/model" | |
| ) | |
| func CreateBook(book model.Book) (model.Book, error) { | |
| result := database.DB.Create(&book) | |
| if result.Error != nil { |
This file contains hidden or 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
| package service | |
| import ( | |
| "bookstore/database" | |
| "bookstore/model" | |
| "testing" | |
| ) | |
| // ===== add this function ===== | |
| func ResetBookTable() { |
This file contains hidden or 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
| package service | |
| import ( | |
| "bookstore/database" | |
| "bookstore/model" | |
| ) | |
| func CreateBook(book model.Book) (model.Book, error) { | |
| result := database.DB.Create(&book) | |
| if result.Error != nil { |