Files
pokeval/api/value_handler.go
2025-04-28 00:04:14 +02:00

107 lines
2.4 KiB
Go
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
}