Files
telegram-shop/src/services/purchaseService.js
Artyom Ashirov 5d4f56e265 refactoring
2024-11-23 05:03:30 +03:00

59 lines
1.8 KiB
JavaScript

import db from "../config/database.js";
class PurchaseService {
static async getPurchasesByUserId(userId, limit, offset) {
try {
return await db.allAsync(`
SELECT
p.*,
pr.name as product_name,
pr.description,
l.country,
l.city,
l.district
FROM purchases p
JOIN products pr ON p.product_id = pr.id
JOIN locations l ON pr.location_id = l.id
WHERE p.user_id = ?
ORDER BY p.purchase_date DESC
LIMIT ?
OFFSET ?
`, [userId, limit, offset]);
} catch (error) {
console.error('Error get purchases:', error);
throw error;
}
}
static async getPurchaseById(purchaseId) {
try {
return await db.getAsync(`
SELECT
p.*,
pr.name as product_name,
pr.description,
l.country,
l.city,
l.district
FROM purchases p
JOIN products pr ON p.product_id = pr.id
JOIN locations l ON pr.location_id = l.id
WHERE p.id = ?
`, [purchaseId]);
} catch (error) {
console.error('Error get purchase:', error);
throw error;
}
}
static async createPurchase(userId, productId, walletType, quantity, totalPrice) {
await db.runAsync(
'INSERT INTO purchases (user_id, product_id, wallet_type, tx_hash, quantity, total_price) VALUES (?, ?, ?, ?, ?, ?)',
[userId, productId, walletType, "null", quantity, totalPrice]
);
}
}
export default PurchaseService