package api import ( "encoding/json" "net/http" "pokeval/db" "strconv" "github.com/gorilla/mux" ) // GET /api/v1/auctions/{id} func GetAuction(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) id, _ := strconv.Atoi(vars["id"]) row := db.DB.QueryRow("SELECT id, card_id, price, date_sold FROM auctions WHERE id = ?", id) var auctionID, cardID int var price float64 var dateSold string err := row.Scan(&auctionID, &cardID, &price, &dateSold) if err != nil { http.Error(w, "Auktion nicht gefunden", http.StatusNotFound) return } resp := map[string]interface{}{ "id": auctionID, "card_id": cardID, "price": price, "date_sold": dateSold, } json.NewEncoder(w).Encode(resp) } // POST /api/v1/auctions func CreateAuction(w http.ResponseWriter, r *http.Request) { var input struct { CardID int `json:"card_id"` PlatformID int `json:"platform_id"` Price float64 `json:"price"` Currency int `json:"currency"` DateSold string `json:"date_sold"` } json.NewDecoder(r.Body).Decode(&input) _, err := db.DB.Exec("INSERT INTO auctions (card_id, platform_id, price, currency, date_sold) VALUES (?, ?, ?, ?, ?)", input.CardID, input.PlatformID, input.Price, input.Currency, input.DateSold) if err != nil { http.Error(w, "Fehler beim Erstellen der Auktion", http.StatusInternalServerError) return } w.WriteHeader(http.StatusCreated) json.NewEncoder(w).Encode(map[string]string{"message": "Auktion erfolgreich erstellt"}) } // GET /api/v1/auctions func ListAuctions(w http.ResponseWriter, r *http.Request) { rows, err := db.DB.Query("SELECT id, card_id, price FROM auctions LIMIT 50") if err != nil { http.Error(w, "Fehler beim Laden der Auktionen", http.StatusInternalServerError) return } defer rows.Close() var auctions []map[string]interface{} for rows.Next() { var id, cardID int var price float64 rows.Scan(&id, &cardID, &price) auctions = append(auctions, map[string]interface{}{ "id": id, "card_id": cardID, "price": price, }) } json.NewEncoder(w).Encode(auctions) }