Sai*_*hen 5 go go-gorm go-fiber
23:28:45 | 200 | 20.294s | 127.0.0.1 | GET | /api/user/cart/product/all
Run Code Online (Sandbox Code Playgroud)
从购物车产品中,我检索带有购物车产品的购物车。尽管购物车只有一辆购物车和一种产品。但检索起来需要太多时间。
var cart model.Cart
if err := model.DB.Select([]string{"id"}).Preload("CartProduct", func(db *gorm.DB) *gorm.DB {
return db.Select([]string{"cart_id", "id", "quantity", "seller_product_id", "seller_product_variation_id"})
}).Preload("CartProduct.SellerProduct", func(db *gorm.DB) *gorm.DB {
return db.Select([]string{"id", "name", "slug", "selling_price", "product_price", "offer_price", "offer_price_start", "offer_price_end", "quantity", "next_stock", "description"})
}).Preload("CartProduct.SellerProduct.SellerProductImage", func(db *gorm.DB) *gorm.DB {
return db.Select([]string{"image", "seller_product_id"}).Where("display = ?", true)
}).Preload("CartProduct.SellerProductVariation", func(db *gorm.DB) *gorm.DB {
return db.Select([]string{"id", "image", "product_price", "selling_price", "quantity", "seller_product_id"})
}).Preload("CartProduct.SellerProductVariation.SellerProductVariationValues", func(db *gorm.DB) *gorm.DB {
return db.Select([]string{"id", "name", "description", "attribute_id", "seller_product_variation_id"})
}).Preload("CartProduct.SellerProductVariation.SellerProductVariationValues.Attribute", func(db *gorm.DB) *gorm.DB { return db.Select([]string{"id", "name"}) }).Where("user_id = ?", c.Locals("user_id")).First(&cart); err.Error != nil {
return c.SendStatus(fiber.StatusNoContent)
}
return c.JSON(cart)
Run Code Online (Sandbox Code Playgroud)
这是我的连接
dsn := "user=postgres password=123456 dbname=bongobitan port=5432 sslmode=disable TimeZone=Asia/Dhaka"
DB, err = gorm.Open(postgres.Open(dsn), &gorm.Config{
SkipDefaultTransaction: true,
})
if err != nil {
panic("Failed to connect to database")
}
Run Code Online (Sandbox Code Playgroud)
如何加快我的代码速度?
| 归档时间: |
|
| 查看次数: |
257 次 |
| 最近记录: |