Mhamdans17 commited on
Commit
d3525b4
·
1 Parent(s): 4a28cd8

perf: optimize ListProducts, ListCategories, and ListOrders GORM queries with select-specific-columns and custom nested preload column selects

Browse files
controllers/order_controller.go CHANGED
@@ -248,7 +248,16 @@ func ListOrders(c *gin.Context) {
248
  skip, _ := strconv.Atoi(skipStr)
249
  limit, _ := strconv.Atoi(limitStr)
250
 
251
- query := config.DB.Preload("Branch").Preload("Items.Product").
 
 
 
 
 
 
 
 
 
252
  Where("company_id = ?", *user.CompanyID).
253
  Order("created_at desc")
254
 
 
248
  skip, _ := strconv.Atoi(skipStr)
249
  limit, _ := strconv.Atoi(limitStr)
250
 
251
+ query := config.DB.Select("id, company_id, branch_id, order_number, total_amount, paid_amount, change_amount, payment_method, payment_status, midtrans_order_id, midtrans_token, created_at").
252
+ Preload("Branch", func(db *gorm.DB) *gorm.DB {
253
+ return db.Select("id, name")
254
+ }).
255
+ Preload("Items", func(db *gorm.DB) *gorm.DB {
256
+ return db.Select("id, order_id, product_id, quantity, unit_price, subtotal")
257
+ }).
258
+ Preload("Items.Product", func(db *gorm.DB) *gorm.DB {
259
+ return db.Select("id, name")
260
+ }).
261
  Where("company_id = ?", *user.CompanyID).
262
  Order("created_at desc")
263
 
controllers/product_controller.go CHANGED
@@ -11,6 +11,7 @@ import (
11
  "service-warungpos-go/services"
12
 
13
  "github.com/gin-gonic/gin"
 
14
  )
15
 
16
  // DTOs untuk Kategori & Produk
@@ -99,7 +100,7 @@ func ListCategories(c *gin.Context) {
99
  }
100
 
101
  var allCategories []models.Category
102
- if err := config.DB.Where("company_id = ?", *user.CompanyID).Find(&allCategories).Error; err != nil {
103
  c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal mengambil data kategori"})
104
  return
105
  }
@@ -297,7 +298,11 @@ func ListProducts(c *gin.Context) {
297
  limitStr := c.Query("limit")
298
  skipStr := c.Query("skip")
299
 
300
- query := config.DB.Preload("Category").Where("company_id = ?", *user.CompanyID)
 
 
 
 
301
 
302
  if activeOnly {
303
  query = query.Where("is_active = ?", true)
 
11
  "service-warungpos-go/services"
12
 
13
  "github.com/gin-gonic/gin"
14
+ "gorm.io/gorm"
15
  )
16
 
17
  // DTOs untuk Kategori & Produk
 
100
  }
101
 
102
  var allCategories []models.Category
103
+ if err := config.DB.Select("id, company_id, name, description, created_at").Where("company_id = ?", *user.CompanyID).Find(&allCategories).Error; err != nil {
104
  c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal mengambil data kategori"})
105
  return
106
  }
 
298
  limitStr := c.Query("limit")
299
  skipStr := c.Query("skip")
300
 
301
+ query := config.DB.Select("id, company_id, category_id, name, sku, price, stock, is_unlimited_stock, image_url, is_active, created_at, updated_at").
302
+ Preload("Category", func(db *gorm.DB) *gorm.DB {
303
+ return db.Select("id, name")
304
+ }).
305
+ Where("company_id = ?", *user.CompanyID)
306
 
307
  if activeOnly {
308
  query = query.Where("is_active = ?", true)