Mhamdans17 commited on
Commit
4ffd896
·
1 Parent(s): 44f1ca9

perf: completely avoid SELECT * in CreateProduct and UpdateProduct by selecting Category directly instead of First product load

Browse files
Files changed (1) hide show
  1. controllers/product_controller.go +6 -3
controllers/product_controller.go CHANGED
@@ -407,8 +407,9 @@ func CreateProduct(c *gin.Context) {
407
  return
408
  }
409
 
410
- // Preload Category sebelum me-return response agar response-nya lengkap
411
- config.DB.Preload("Category").First(&newProduct, newProduct.ID)
 
412
 
413
  c.JSON(http.StatusCreated, newProduct)
414
  }
@@ -470,7 +471,9 @@ func UpdateProduct(c *gin.Context) {
470
  return
471
  }
472
 
473
- config.DB.Preload("Category").First(&product, product.ID)
 
 
474
 
475
  c.JSON(http.StatusOK, product)
476
  }
 
407
  return
408
  }
409
 
410
+ if newProduct.CategoryID != nil {
411
+ config.DB.Select("id, name").First(&newProduct.Category, *newProduct.CategoryID)
412
+ }
413
 
414
  c.JSON(http.StatusCreated, newProduct)
415
  }
 
471
  return
472
  }
473
 
474
+ if product.CategoryID != nil {
475
+ config.DB.Select("id, name").First(&product.Category, *product.CategoryID)
476
+ }
477
 
478
  c.JSON(http.StatusOK, product)
479
  }