23:28:45 | 200 | 20.294s | 127.0.0.1 | GET | /api/user/cart/product/all
From Cart product, I retrieve cart with Cart products. Though Cart has only one Cart with One product. But it takes too much time to retrieve.
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)
Here is my connection
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")
}
How can I speed up my code?