diff --git a/tushare/src/api.py b/tushare/src/api.py index 5a33c2c..ad92415 100644 --- a/tushare/src/api.py +++ b/tushare/src/api.py @@ -102,7 +102,7 @@ def list_scores( @app.get("/api/v1/scores/{score_id}") -def get_score(score_id: int): +def get_score(score_id: str): conn = storage._get_conn() try: with conn.cursor() as cur: diff --git a/tushare/src/storage.py b/tushare/src/storage.py index 0b10e66..1fb2435 100644 --- a/tushare/src/storage.py +++ b/tushare/src/storage.py @@ -38,7 +38,7 @@ def init_db(db_url: str = DEFAULT_DB_URL): """) cur.execute(""" CREATE TABLE IF NOT EXISTS scores ( - id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY, + id UUID DEFAULT gen_random_uuid_v7() PRIMARY KEY, ts_code TEXT NOT NULL, trade_date TEXT NOT NULL, close REAL, diff --git a/web/backend/internal/handlers/scores.go b/web/backend/internal/handlers/scores.go index 60a0933..4b562d3 100644 --- a/web/backend/internal/handlers/scores.go +++ b/web/backend/internal/handlers/scores.go @@ -32,12 +32,7 @@ func (d *Deps) ListScores(w http.ResponseWriter, r *http.Request) { } func (d *Deps) GetScore(w http.ResponseWriter, r *http.Request) { - idStr := chi.URLParam(r, "id") - id, err := strconv.ParseInt(idStr, 10, 64) - if err != nil { - writeErr(w, http.StatusBadRequest, "invalid id") - return - } + id := chi.URLParam(r, "id") row, err := d.Futures.GetScore(id) if err != nil { writeErr(w, http.StatusNotFound, err.Error()) diff --git a/web/backend/internal/store/futures.go b/web/backend/internal/store/futures.go index 3059da2..9a1c16a 100644 --- a/web/backend/internal/store/futures.go +++ b/web/backend/internal/store/futures.go @@ -29,7 +29,7 @@ func OpenFutures(databaseURL string) (*FuturesStore, error) { func (s *FuturesStore) Close() error { return s.db.Close() } type Score struct { - ID int64 `json:"id"` + ID string `json:"id"` TsCode string `json:"ts_code"` TradeDate string `json:"trade_date"` Close float64 `json:"close"` @@ -91,7 +91,7 @@ func (s *FuturesStore) ListScores(f ScoreFilter) ([]Score, error) { return out, rows.Err() } -func (s *FuturesStore) GetScore(id int64) (*Score, error) { +func (s *FuturesStore) GetScore(id string) (*Score, error) { row := s.db.QueryRow(`SELECT id, ts_code, trade_date, close, oi, oi_chg, short_term, medium_term, long_term, composite, signal, detail_json, created_at FROM scores WHERE id = ?`, id) var x Score