Skip to content

Instantly share code, notes, and snippets.

View BK1031's full-sized avatar

Bharat Kathi BK1031

View GitHub Profile
@BK1031
BK1031 / book_test.go
Created July 16, 2024 18:02
singlestore-go-bookstore testcreatebook api
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) {
@BK1031
BK1031 / main_test.go
Last active July 16, 2024 17:27
singlestore-go-bookstore api test setup
package api
import (
"bookstore/config"
"bookstore/database"
"context"
"log"
"os"
"testing"
@BK1031
BK1031 / main.go
Created July 16, 2024 16:53
singlestore-go-bookstore start server
func main() {
fmt.Println("Hello, World!")
database.InitializeDB()
api.StartServer() // add this
}
@BK1031
BK1031 / api.go
Created July 16, 2024 16:45
singlestore-go-bookstore start server
package api
import (
"bookstore/config"
"net/http"
"github.com/gin-gonic/gin"
)
func StartServer() {
@BK1031
BK1031 / order.go
Created July 16, 2024 16:27
singlestore-go-bookstore GetOrder and GetAllOrders
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
}
@BK1031
BK1031 / order_test.go
Created July 16, 2024 16:25
singlestore-go-bookstore TestGetOrder and TestGetAllOrders
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",
@BK1031
BK1031 / order.go
Created July 16, 2024 16:23
singlestore-go-bookstore DeleteOrder
func DeleteOrder(order model.Order) error {
result := database.DB.Delete(&order)
if result.Error != nil {
return result.Error
}
return nil
}
@BK1031
BK1031 / order_test.go
Created July 16, 2024 16:21
singlestore-go-bookstore TestDeleteOrder
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",
})
@BK1031
BK1031 / order.go
Created July 16, 2024 16:19
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 {
@BK1031
BK1031 / order_test.go
Created July 16, 2024 09:10
singlestore-go-bookstore
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",
})