107 lines
2.4 KiB
Go
107 lines
2.4 KiB
Go
package api
|
||
|
||
import (
|
||
"encoding/json"
|
||
"net/http"
|
||
"pokeval/db"
|
||
"pokeval/logic"
|
||
"strconv"
|
||
|
||
"github.com/gorilla/mux"
|
||
)
|
||
|
||
// GET /api/v1/value/{auction_id}
|
||
func GetCardValue(w http.ResponseWriter, r *http.Request) {
|
||
vars := mux.Vars(r)
|
||
auctionID, err := strconv.Atoi(vars["auction_id"])
|
||
if err != nil {
|
||
http.Error(w, "Ungültige Auktions-ID", http.StatusBadRequest)
|
||
return
|
||
}
|
||
|
||
// 1️⃣ Hole die Basis-Auktionsdaten
|
||
var cardID int
|
||
query := `SELECT card_id FROM auctions WHERE id = ?`
|
||
err = db.DB.QueryRow(query, auctionID).Scan(&cardID)
|
||
if err != nil {
|
||
http.Error(w, "Auktion nicht gefunden", http.StatusNotFound)
|
||
return
|
||
}
|
||
|
||
// 2️⃣ Base Sales: Alle Verkäufe dieser Karte
|
||
baseSales := fetchBaseSales(cardID)
|
||
|
||
// 3️⃣ Exact Match Sales: Verkäufe mit gleichem Grading, Autograph, Misprint
|
||
exactSales := fetchExactMatchSales(auctionID)
|
||
|
||
// 4️⃣ Bewertungsfaktoren (Dummy-Werte oder aus DB/Logik ableiten)
|
||
factors := map[string]float64{
|
||
"GF": 1.6,
|
||
"RF": 2.0,
|
||
"EF": 2.0,
|
||
"ST": 3.5,
|
||
"DF": 1.4,
|
||
"AF": 2.0,
|
||
"MF": 1.5,
|
||
"TAF": 200,
|
||
}
|
||
|
||
// 5️⃣ Wert berechnen
|
||
value := logic.CalculateCardValue(baseSales, exactSales, factors)
|
||
|
||
// 6️⃣ Response
|
||
resp := map[string]interface{}{
|
||
"auction_id": auctionID,
|
||
"calculated_value": value,
|
||
"currency": "EUR",
|
||
"factors": factors,
|
||
}
|
||
|
||
w.Header().Set("Content-Type", "application/json")
|
||
json.NewEncoder(w).Encode(resp)
|
||
}
|
||
|
||
// Holt alle Preise der Basisverkäufe (gleiche Karte)
|
||
func fetchBaseSales(cardID int) []float64 {
|
||
rows, err := db.DB.Query("SELECT price FROM auctions WHERE card_id = ?", cardID)
|
||
if err != nil {
|
||
return []float64{}
|
||
}
|
||
defer rows.Close()
|
||
|
||
var prices []float64
|
||
for rows.Next() {
|
||
var price float64
|
||
rows.Scan(&price)
|
||
prices = append(prices, price)
|
||
}
|
||
return prices
|
||
}
|
||
|
||
// Holt alle Exact Match Verkäufe (vereinfachte Logik)
|
||
func fetchExactMatchSales(auctionID int) []float64 {
|
||
// Beispielhafte Logik: Verkäufe mit gleichem Grading, Autograph & Misprint
|
||
query := `
|
||
SELECT a.price
|
||
FROM auctions a
|
||
JOIN auction_gradings ag ON ag.auction_id = a.id
|
||
WHERE a.id != ? AND ag.grading_id IN (
|
||
SELECT grading_id FROM auction_gradings WHERE auction_id = ?
|
||
)
|
||
`
|
||
|
||
rows, err := db.DB.Query(query, auctionID, auctionID)
|
||
if err != nil {
|
||
return []float64{}
|
||
}
|
||
defer rows.Close()
|
||
|
||
var prices []float64
|
||
for rows.Next() {
|
||
var price float64
|
||
rows.Scan(&price)
|
||
prices = append(prices, price)
|
||
}
|
||
return prices
|
||
}
|