ilk
This commit is contained in:
89
svc/routes/orderinventory.go
Normal file
89
svc/routes/orderinventory.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"bssapp-backend/db"
|
||||
"bssapp-backend/queries"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ✅ GET /api/order-inventory?code=...&color=...&color2=...
|
||||
func GetOrderInventoryHandler(w http.ResponseWriter, r *http.Request) {
|
||||
code := r.URL.Query().Get("code")
|
||||
color := r.URL.Query().Get("color")
|
||||
color2 := r.URL.Query().Get("color2")
|
||||
|
||||
if code == "" {
|
||||
http.Error(w, "Eksik parametre: code gerekli", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// 🔧 Normalize eksik renk parametreleri
|
||||
if color == "" {
|
||||
color = " "
|
||||
}
|
||||
if color2 == "" {
|
||||
color2 = " "
|
||||
}
|
||||
|
||||
log.Printf("🎨 [ORDERINV] İstek alındı -> code=%q | color=%q | color2=%q", code, color, color2)
|
||||
|
||||
// ✅ MSSQL bağlantısını ve timeout'u oluştur
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||||
defer cancel()
|
||||
|
||||
rows, err := db.MssqlDB.QueryContext(ctx, queries.GetOrderInventory, code, color, color2)
|
||||
if err != nil {
|
||||
log.Printf("❌ [ORDERINV] SQL çalıştırılamadı: %v", err)
|
||||
http.Error(w, "SQL hatası: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
if cerr := rows.Close(); cerr != nil {
|
||||
log.Printf("⚠️ [ORDERINV] rows.Close() hatası: %v", cerr)
|
||||
}
|
||||
}()
|
||||
|
||||
type Row struct {
|
||||
UrunKodu string `json:"UrunKodu"`
|
||||
RenkKodu string `json:"RenkKodu"`
|
||||
RenkAciklamasi string `json:"RenkAciklamasi"`
|
||||
Beden string `json:"Beden"`
|
||||
Yaka string `json:"Yaka"`
|
||||
KullanilabilirAdet float64 `json:"KullanilabilirAdet"`
|
||||
}
|
||||
|
||||
var list []Row
|
||||
for rows.Next() {
|
||||
var r Row
|
||||
if err := rows.Scan(
|
||||
&r.UrunKodu,
|
||||
&r.RenkKodu,
|
||||
&r.RenkAciklamasi,
|
||||
&r.Beden,
|
||||
&r.Yaka,
|
||||
&r.KullanilabilirAdet,
|
||||
); err != nil {
|
||||
log.Printf("⚠️ [ORDERINV] Scan hatası: %v", err)
|
||||
continue
|
||||
}
|
||||
list = append(list, r)
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
log.Printf("❌ [ORDERINV] Rows hatası: %v", err)
|
||||
http.Error(w, "Veri okuma hatası: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("✅ [ORDERINV] %s / %s / %s -> %d kayıt döndü", code, color, color2, len(list))
|
||||
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
if err := json.NewEncoder(w).Encode(list); err != nil {
|
||||
log.Printf("❌ [ORDERINV] JSON encode hatası: %v", err)
|
||||
http.Error(w, "JSON encode başarısız", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user