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 TestCreateBook(t *testing.T) { | |
// Arrange | |
book := model.Book{ | |
Title: "The Great Gatsby", | |
Author: "F. Scott Fitzgerald", | |
Price: 29.99, | |
Genre: "Fiction", | |
} | |
reqBody, _ := json.Marshal(book) | |
t.Run("Success", func(t *testing.T) { |
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
package api | |
import ( | |
"bookstore/config" | |
"bookstore/database" | |
"context" | |
"log" | |
"os" | |
"testing" |
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 main() { | |
fmt.Println("Hello, World!") | |
database.InitializeDB() | |
api.StartServer() // add this | |
} |
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
package api | |
import ( | |
"bookstore/config" | |
"net/http" | |
"github.com/gin-gonic/gin" | |
) | |
func StartServer() { |
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 GetOrder(id int) (model.Order, error) { | |
var order model.Order | |
result := database.DB.Where("id = ?", id).First(&order) | |
if result.Error != nil { | |
return model.Order{}, result.Error | |
} | |
database.DB.Model(&model.OrderItem{}).Where("order_id = ?", order.ID).Find(&order.Items) | |
return order, nil | |
} |
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 TestGetOrder(t *testing.T) { | |
// Arrange | |
ResetOrderTable() | |
ResetBookTable() | |
// Arrange | |
gatsby, _ := CreateBook(model.Book{ | |
Title: "The Great Gatsby", | |
Author: "F. Scott Fitzgerald", | |
Price: 29.99, | |
Genre: "Fiction", |
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 DeleteOrder(order model.Order) error { | |
result := database.DB.Delete(&order) | |
if result.Error != nil { | |
return result.Error | |
} | |
return nil | |
} |
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 TestDeleteOrder(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 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 { |
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 TestUpdateOrder(t *testing.T) { | |
ResetOrderTable() | |
ResetBookTable() | |
// Arrange | |
gatsby, _ := CreateBook(model.Book{ | |
Title: "The Great Gatsby", | |
Author: "F. Scott Fitzgerald", | |
Price: 29.99, | |
Genre: "Fiction", | |
}) |
NewerOlder