scores 主键改用 UUID v7
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -102,7 +102,7 @@ def list_scores(
|
|||||||
|
|
||||||
|
|
||||||
@app.get("/api/v1/scores/{score_id}")
|
@app.get("/api/v1/scores/{score_id}")
|
||||||
def get_score(score_id: int):
|
def get_score(score_id: str):
|
||||||
conn = storage._get_conn()
|
conn = storage._get_conn()
|
||||||
try:
|
try:
|
||||||
with conn.cursor() as cur:
|
with conn.cursor() as cur:
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ def init_db(db_url: str = DEFAULT_DB_URL):
|
|||||||
""")
|
""")
|
||||||
cur.execute("""
|
cur.execute("""
|
||||||
CREATE TABLE IF NOT EXISTS scores (
|
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,
|
ts_code TEXT NOT NULL,
|
||||||
trade_date TEXT NOT NULL,
|
trade_date TEXT NOT NULL,
|
||||||
close REAL,
|
close REAL,
|
||||||
|
|||||||
@@ -32,12 +32,7 @@ func (d *Deps) ListScores(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (d *Deps) GetScore(w http.ResponseWriter, r *http.Request) {
|
func (d *Deps) GetScore(w http.ResponseWriter, r *http.Request) {
|
||||||
idStr := chi.URLParam(r, "id")
|
id := chi.URLParam(r, "id")
|
||||||
id, err := strconv.ParseInt(idStr, 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
writeErr(w, http.StatusBadRequest, "invalid id")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
row, err := d.Futures.GetScore(id)
|
row, err := d.Futures.GetScore(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeErr(w, http.StatusNotFound, err.Error())
|
writeErr(w, http.StatusNotFound, err.Error())
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ func OpenFutures(databaseURL string) (*FuturesStore, error) {
|
|||||||
func (s *FuturesStore) Close() error { return s.db.Close() }
|
func (s *FuturesStore) Close() error { return s.db.Close() }
|
||||||
|
|
||||||
type Score struct {
|
type Score struct {
|
||||||
ID int64 `json:"id"`
|
ID string `json:"id"`
|
||||||
TsCode string `json:"ts_code"`
|
TsCode string `json:"ts_code"`
|
||||||
TradeDate string `json:"trade_date"`
|
TradeDate string `json:"trade_date"`
|
||||||
Close float64 `json:"close"`
|
Close float64 `json:"close"`
|
||||||
@@ -91,7 +91,7 @@ func (s *FuturesStore) ListScores(f ScoreFilter) ([]Score, error) {
|
|||||||
return out, rows.Err()
|
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,
|
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)
|
long_term, composite, signal, detail_json, created_at FROM scores WHERE id = ?`, id)
|
||||||
var x Score
|
var x Score
|
||||||
|
|||||||
Reference in New Issue
Block a user