refactoring
This commit is contained in:
59
src/services/purchaseService.js
Normal file
59
src/services/purchaseService.js
Normal file
@@ -0,0 +1,59 @@
|
||||
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
|
||||
Reference in New Issue
Block a user