Mhamdans17 commited on
Commit ·
8afed15
1
Parent(s): 4ffd896
perf: completely avoid SELECT * in DeleteProduct, GetCategory, UpdateCategory, and DeleteCategory handlers by using direct database updates and specific select counts
Browse files
controllers/product_controller.go
CHANGED
|
@@ -157,7 +157,7 @@ func GetCategory(c *gin.Context) {
|
|
| 157 |
}
|
| 158 |
|
| 159 |
var category models.Category
|
| 160 |
-
if err := config.DB.Where("id = ? AND company_id = ?", catID, *user.CompanyID).First(&category).Error; err != nil {
|
| 161 |
c.JSON(http.StatusNotFound, gin.H{"detail": "Kategori tidak ditemukan"})
|
| 162 |
return
|
| 163 |
}
|
|
@@ -215,7 +215,7 @@ func UpdateCategory(c *gin.Context) {
|
|
| 215 |
}
|
| 216 |
|
| 217 |
var category models.Category
|
| 218 |
-
if err := config.DB.Where("id = ? AND company_id = ?", catID, *user.CompanyID).First(&category).Error; err != nil {
|
| 219 |
c.JSON(http.StatusNotFound, gin.H{"detail": "Kategori tidak ditemukan"})
|
| 220 |
return
|
| 221 |
}
|
|
@@ -250,15 +250,16 @@ func DeleteCategory(c *gin.Context) {
|
|
| 250 |
return
|
| 251 |
}
|
| 252 |
|
| 253 |
-
var
|
| 254 |
-
|
| 255 |
-
|
|
|
|
| 256 |
return
|
| 257 |
}
|
| 258 |
|
| 259 |
-
|
| 260 |
-
if
|
| 261 |
-
c.JSON(http.
|
| 262 |
return
|
| 263 |
}
|
| 264 |
|
|
@@ -489,16 +490,15 @@ func DeleteProduct(c *gin.Context) {
|
|
| 489 |
return
|
| 490 |
}
|
| 491 |
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
|
|
|
|
|
|
| 495 |
return
|
| 496 |
}
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
product.IsActive = false
|
| 500 |
-
if err := config.DB.Save(&product).Error; err != nil {
|
| 501 |
-
c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal menghapus produk"})
|
| 502 |
return
|
| 503 |
}
|
| 504 |
|
|
|
|
| 157 |
}
|
| 158 |
|
| 159 |
var category models.Category
|
| 160 |
+
if err := config.DB.Select("id, company_id, name, description, created_at").Where("id = ? AND company_id = ?", catID, *user.CompanyID).First(&category).Error; err != nil {
|
| 161 |
c.JSON(http.StatusNotFound, gin.H{"detail": "Kategori tidak ditemukan"})
|
| 162 |
return
|
| 163 |
}
|
|
|
|
| 215 |
}
|
| 216 |
|
| 217 |
var category models.Category
|
| 218 |
+
if err := config.DB.Select("id, company_id, name, description").Where("id = ? AND company_id = ?", catID, *user.CompanyID).First(&category).Error; err != nil {
|
| 219 |
c.JSON(http.StatusNotFound, gin.H{"detail": "Kategori tidak ditemukan"})
|
| 220 |
return
|
| 221 |
}
|
|
|
|
| 250 |
return
|
| 251 |
}
|
| 252 |
|
| 253 |
+
var productCount int64
|
| 254 |
+
config.DB.Model(&models.Product{}).Where("category_id = ?", catID).Count(&productCount)
|
| 255 |
+
if productCount > 0 {
|
| 256 |
+
c.JSON(http.StatusBadRequest, gin.H{"detail": fmt.Sprintf("Kategori masih memiliki %d produk. Pindahkan produk dulu.", productCount)})
|
| 257 |
return
|
| 258 |
}
|
| 259 |
|
| 260 |
+
var category models.Category
|
| 261 |
+
if err := config.DB.Select("id, name").Where("id = ? AND company_id = ?", catID, *user.CompanyID).First(&category).Error; err != nil {
|
| 262 |
+
c.JSON(http.StatusNotFound, gin.H{"detail": "Kategori tidak ditemukan"})
|
| 263 |
return
|
| 264 |
}
|
| 265 |
|
|
|
|
| 490 |
return
|
| 491 |
}
|
| 492 |
|
| 493 |
+
result := config.DB.Model(&models.Product{}).
|
| 494 |
+
Where("id = ? AND company_id = ?", productID, *user.CompanyID).
|
| 495 |
+
Update("is_active", false)
|
| 496 |
+
if result.Error != nil {
|
| 497 |
+
c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal menghapus produk"})
|
| 498 |
return
|
| 499 |
}
|
| 500 |
+
if result.RowsAffected == 0 {
|
| 501 |
+
c.JSON(http.StatusNotFound, gin.H{"detail": "Produk tidak ditemukan"})
|
|
|
|
|
|
|
|
|
|
| 502 |
return
|
| 503 |
}
|
| 504 |
|