Skip to content

Commit 6d11f7a

Browse files
夏一飞夏一飞
authored andcommitted
refactor: move updateModelPrice to db/client.ts
1 parent d34ad26 commit 6d11f7a

File tree

2 files changed

+5
-36
lines changed

2 files changed

+5
-36
lines changed

lib/db/client.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -404,15 +404,15 @@ export async function updateModelPrice(
404404
output_price = CAST($3 AS NUMERIC(10,6)),
405405
per_msg_price = CAST($4 AS NUMERIC(10,6)),
406406
updated_at = CURRENT_TIMESTAMP
407-
WHERE id = $1
407+
WHERE model_id = $1
408408
RETURNING *`,
409409
[id, input_price, output_price, per_msg_price]
410410
);
411411

412412
if (result.rows[0]) {
413413
return {
414-
id: result.rows[0].id,
415-
name: result.rows[0].name,
414+
id: result.rows[0].model_id,
415+
name: result.rows[0].model_name,
416416
input_price: Number(result.rows[0].input_price),
417417
output_price: Number(result.rows[0].output_price),
418418
per_msg_price: Number(result.rows[0].per_msg_price),
@@ -421,7 +421,7 @@ export async function updateModelPrice(
421421
}
422422
return null;
423423
} catch (error) {
424-
console.error(`Failed to update ${id} price:`, error);
424+
console.error("Error updating model price:", error);
425425
throw error;
426426
}
427427
}

lib/db/index.ts

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { query } from "./client";
22
import { ensureUserTableExists } from "./users";
3-
import { ModelPrice } from "../db";
3+
import { ModelPrice, updateModelPrice } from "./client";
44

55
async function ensureModelPricesTableExists() {
66
const defaultInputPrice = parseFloat(
@@ -81,37 +81,6 @@ export async function getOrCreateModelPrice(
8181
}
8282
}
8383

84-
export async function updateModelPrice(
85-
modelId: string,
86-
input_price: number,
87-
output_price: number,
88-
per_msg_price: number
89-
): Promise<ModelPrice | null> {
90-
const result = await query(
91-
`UPDATE model_prices
92-
SET
93-
input_price = CAST($2 AS DECIMAL(10,6)),
94-
output_price = CAST($3 AS DECIMAL(10,6)),
95-
per_msg_price = CAST($4 AS DECIMAL(10,6)),
96-
updated_at = CURRENT_TIMESTAMP
97-
WHERE model_id = $1
98-
RETURNING *;`,
99-
[modelId, input_price, output_price, per_msg_price]
100-
);
101-
102-
if (result.rows.length === 0) {
103-
return null;
104-
}
105-
106-
return {
107-
id: result.rows[0].model_id,
108-
name: result.rows[0].model_name,
109-
input_price: Number(result.rows[0].input_price),
110-
output_price: Number(result.rows[0].output_price),
111-
per_msg_price: Number(result.rows[0].per_msg_price),
112-
updated_at: result.rows[0].updated_at,
113-
};
114-
}
11584
export {
11685
getUsers,
11786
getOrCreateUser,

0 commit comments

Comments
 (0)