Mhamdans17 commited on
Commit ·
97344b4
1
Parent(s): 6c773a6
perf: optimize ListUsers and ListCompanies with database-level pagination and search
Browse files
controllers/auth_controller.go
CHANGED
|
@@ -399,8 +399,32 @@ func GetMe(c *gin.Context) {
|
|
| 399 |
}
|
| 400 |
|
| 401 |
func ListUsers(c *gin.Context) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 402 |
var users []models.User
|
| 403 |
-
if err :=
|
| 404 |
c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal mengambil data user"})
|
| 405 |
return
|
| 406 |
}
|
|
|
|
| 399 |
}
|
| 400 |
|
| 401 |
func ListUsers(c *gin.Context) {
|
| 402 |
+
limitStr := c.Query("limit")
|
| 403 |
+
skipStr := c.Query("skip")
|
| 404 |
+
searchQuery := c.Query("search")
|
| 405 |
+
|
| 406 |
+
query := config.DB.Preload("Company").Preload("Branch")
|
| 407 |
+
|
| 408 |
+
if searchQuery != "" {
|
| 409 |
+
query = query.Where("name ILIKE ? OR email ILIKE ? OR phone ILIKE ?",
|
| 410 |
+
"%"+searchQuery+"%",
|
| 411 |
+
"%"+searchQuery+"%",
|
| 412 |
+
"%"+searchQuery+"%")
|
| 413 |
+
}
|
| 414 |
+
|
| 415 |
+
if limitStr != "" {
|
| 416 |
+
if limit, err := strconv.Atoi(limitStr); err == nil {
|
| 417 |
+
query = query.Limit(limit)
|
| 418 |
+
}
|
| 419 |
+
}
|
| 420 |
+
if skipStr != "" {
|
| 421 |
+
if skip, err := strconv.Atoi(skipStr); err == nil {
|
| 422 |
+
query = query.Offset(skip)
|
| 423 |
+
}
|
| 424 |
+
}
|
| 425 |
+
|
| 426 |
var users []models.User
|
| 427 |
+
if err := query.Find(&users).Error; err != nil {
|
| 428 |
c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal mengambil data user"})
|
| 429 |
return
|
| 430 |
}
|
controllers/company_controller.go
CHANGED
|
@@ -72,8 +72,33 @@ type BranchProductResponse struct {
|
|
| 72 |
// ==================== COMPANY CONTROLLER ====================
|
| 73 |
|
| 74 |
func ListCompanies(c *gin.Context) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
var companies []models.Company
|
| 76 |
-
if err :=
|
| 77 |
c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal mengambil data perusahaan"})
|
| 78 |
return
|
| 79 |
}
|
|
|
|
| 72 |
// ==================== COMPANY CONTROLLER ====================
|
| 73 |
|
| 74 |
func ListCompanies(c *gin.Context) {
|
| 75 |
+
limitStr := c.Query("limit")
|
| 76 |
+
skipStr := c.Query("skip")
|
| 77 |
+
searchQuery := c.Query("search")
|
| 78 |
+
|
| 79 |
+
query := config.DB.Model(&models.Company{})
|
| 80 |
+
|
| 81 |
+
if searchQuery != "" {
|
| 82 |
+
query = query.Where("name ILIKE ? OR email ILIKE ? OR phone ILIKE ? OR address ILIKE ?",
|
| 83 |
+
"%"+searchQuery+"%",
|
| 84 |
+
"%"+searchQuery+"%",
|
| 85 |
+
"%"+searchQuery+"%",
|
| 86 |
+
"%"+searchQuery+"%")
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
if limitStr != "" {
|
| 90 |
+
if limit, err := strconv.Atoi(limitStr); err == nil {
|
| 91 |
+
query = query.Limit(limit)
|
| 92 |
+
}
|
| 93 |
+
}
|
| 94 |
+
if skipStr != "" {
|
| 95 |
+
if skip, err := strconv.Atoi(skipStr); err == nil {
|
| 96 |
+
query = query.Offset(skip)
|
| 97 |
+
}
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
var companies []models.Company
|
| 101 |
+
if err := query.Find(&companies).Error; err != nil {
|
| 102 |
c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal mengambil data perusahaan"})
|
| 103 |
return
|
| 104 |
}
|