45 lines
1.0 KiB
Go
45 lines
1.0 KiB
Go
package handlers
|
|
|
|
import (
|
|
"Portifolio/internal/database"
|
|
"Portifolio/internal/model"
|
|
"database/sql"
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
)
|
|
|
|
func AddTradeHandler(db *sql.DB) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
var req model.AddTradeRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
http.Error(w, "invalid json", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
err := req.Validate()
|
|
if err != nil {
|
|
http.Error(w, "invalid json", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
func GetTradeListHandler(db *sql.DB) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
tradeList, err := database.GetTrades(db)
|
|
if err != nil {
|
|
http.Error(w, "failed to fetch trades", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
if err := json.NewEncoder(w).Encode(tradeList); err != nil {
|
|
http.Error(w, "failed to encode trades", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}
|
|
}
|