41 lines
1,017 B
TypeScript
41 lines
1,017 B
TypeScript
import { db } from '../db/db_index';
|
|
import { productInfo, productReviews } from '../db/schema';
|
|
import { eq, desc } from 'drizzle-orm';
|
|
|
|
export async function getAllProducts(): Promise<any[]> {
|
|
return await db.query.productInfo.findMany({
|
|
with: {
|
|
unit: true,
|
|
store: true,
|
|
specialDeals: true,
|
|
},
|
|
orderBy: productInfo.name,
|
|
});
|
|
}
|
|
|
|
export async function getProductById(id: number): Promise<any | null> {
|
|
return await db.query.productInfo.findFirst({
|
|
where: eq(productInfo.id, id),
|
|
with: {
|
|
unit: true,
|
|
store: true,
|
|
specialDeals: true,
|
|
productReviews: {
|
|
with: {
|
|
user: true,
|
|
},
|
|
orderBy: desc(productReviews.createdAt),
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function createProductReview(userId: number, productId: number, rating: number, comment?: string): Promise<any> {
|
|
const [review] = await db.insert(productReviews).values({
|
|
userId,
|
|
productId,
|
|
rating,
|
|
comment,
|
|
}).returning();
|
|
return review;
|
|
}
|