🎉 init: 小龙的工作空间
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
// Auto-generated OrderItem service
|
||||
import { queryAll, queryOne, execute } from "../db/client.js";
|
||||
import type { OrderItem, CreateOrderItemInput, UpdateOrderItemInput } from "../types/index.js";
|
||||
|
||||
export class OrderItemService {
|
||||
/** List all order_items */
|
||||
list(): OrderItem[] {
|
||||
return queryAll<OrderItem>("SELECT * FROM order_items ORDER BY created_at DESC");
|
||||
}
|
||||
|
||||
/** Get by ID */
|
||||
getById(id: string): OrderItem | undefined {
|
||||
return queryOne<OrderItem>("SELECT * FROM order_items WHERE id = ?", [id]);
|
||||
}
|
||||
|
||||
/** Create */
|
||||
create(input: CreateOrderItemInput): OrderItem {
|
||||
const id = crypto.randomUUID();
|
||||
const now = new Date().toISOString();
|
||||
const hasCreatedAt = false;
|
||||
const hasUpdatedAt = false;
|
||||
const cols = ["id", "order_id", "product_id", "quantity", "unit_price"];
|
||||
const placeholders = cols.map(() => "?").join(", ");
|
||||
const values = [id, input.orderId ?? null, input.productId ?? null, input.quantity ?? null, input.unitPrice ?? null];
|
||||
|
||||
execute(`INSERT INTO order_items (${cols.join(", ")}) VALUES (${placeholders})`, values);
|
||||
return this.getById(id)!;
|
||||
}
|
||||
|
||||
/** Update */
|
||||
update(id: string, input: UpdateOrderItemInput): OrderItem | undefined {
|
||||
const existing = this.getById(id);
|
||||
if (!existing) return undefined;
|
||||
|
||||
const sets: string[] = [];
|
||||
const values: unknown[] = [];
|
||||
if (input.orderId !== undefined) { sets.push("order_id = ?"); values.push(input.orderId); }
|
||||
if (input.productId !== undefined) { sets.push("product_id = ?"); values.push(input.productId); }
|
||||
if (input.quantity !== undefined) { sets.push("quantity = ?"); values.push(input.quantity); }
|
||||
if (input.unitPrice !== undefined) { sets.push("unit_price = ?"); values.push(input.unitPrice); }
|
||||
|
||||
if (sets.length === 0) return existing;
|
||||
|
||||
|
||||
|
||||
values.push(id);
|
||||
execute(`UPDATE order_items SET ${sets.join(", ")} WHERE id = ?`, values);
|
||||
return this.getById(id)!;
|
||||
}
|
||||
|
||||
/** Delete */
|
||||
delete(id: string): boolean {
|
||||
const result = execute("DELETE FROM order_items WHERE id = ?", [id]);
|
||||
return result.changes > 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user