Files
bssapp/svc/routes/user_list.go
2026-02-11 17:46:22 +03:00

82 lines
2.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package routes
import (
"bssapp-backend/models"
"bssapp-backend/queries"
"database/sql"
"encoding/json"
"log"
"net/http"
)
// ======================================================
// 📌 UserListRoute — Kullanıcı Listeleme API (FINAL)
// ======================================================
func UserListRoute(pg *sql.DB) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
// --------------------------------------------------
// 🔍 Query Param
// --------------------------------------------------
search := r.URL.Query().Get("search")
log.Printf("📥 /api/users/list çağrıldı | search='%s'", search)
// --------------------------------------------------
// 🗄️ SQL CALL
// --------------------------------------------------
rows, err := queries.GetUserList(pg, search)
if err != nil {
log.Printf("❌ SQL sorgu hatası (GetUserList): %v", err)
http.Error(w, "Veritabanı hatası", http.StatusInternalServerError)
return
}
defer rows.Close()
// --------------------------------------------------
// 📦 Sonuç Listesi
// --------------------------------------------------
list := make([]models.UserListRow, 0, 100)
count := 0
// ==================================================
// 🧠 SCAN — SQL SELECT ile BİRE BİR (8 kolon)
// ==================================================
for rows.Next() {
var u models.UserListRow
err = rows.Scan(
&u.ID, // 1
&u.Code, // 2
&u.IsActive, // 3
&u.NebimUsername, // 4 (nullable)
&u.UserGroupCode, // 5 (nullable)
&u.RoleNames, // 6
&u.DepartmentNames, // 7
&u.PiyasaNames, // 8
)
if err != nil {
log.Printf("⚠️ Satır atlandı (SCAN hatası): %v", err)
continue
}
list = append(list, u)
count++
}
if err := rows.Err(); err != nil {
log.Printf("⚠️ rows.Err(): %v", err)
}
log.Printf("✅ User listesi tamamlandı — %d kayıt gönderildi.", count)
// --------------------------------------------------
// 📤 JSON OUTPUT
// --------------------------------------------------
_ = json.NewEncoder(w).Encode(list)
})
}