Bug update function
This commit is contained in:
@@ -16,7 +16,16 @@ class CategoryService {
|
||||
}
|
||||
|
||||
static async getCategoryById(categoryId) {
|
||||
return await db.getAsync('SELECT id, name FROM categories WHERE id = ?', [categoryId]);
|
||||
try {
|
||||
const category = await db.getAsync(
|
||||
'SELECT * FROM categories WHERE id = ?',
|
||||
[categoryId]
|
||||
);
|
||||
return category;
|
||||
} catch (error) {
|
||||
console.error('Error fetching category by ID:', error);
|
||||
throw new Error('Failed to fetch category');
|
||||
}
|
||||
}
|
||||
|
||||
static async getSubcategoryById(subcategoryId) {
|
||||
|
||||
@@ -27,10 +27,16 @@ class LocationService {
|
||||
}
|
||||
|
||||
static async getLocationById(locationId) {
|
||||
return await db.getAsync(
|
||||
'SELECT country, city, district FROM locations WHERE id = ?',
|
||||
[locationId]
|
||||
);
|
||||
try {
|
||||
const location = await db.getAsync(
|
||||
'SELECT * FROM locations WHERE id = ?',
|
||||
[locationId]
|
||||
);
|
||||
return location;
|
||||
} catch (error) {
|
||||
console.error('Error fetching location by ID:', error);
|
||||
throw new Error('Failed to fetch location');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -42,6 +42,17 @@ class ProductService {
|
||||
);
|
||||
}
|
||||
|
||||
static async decreaseProductQuantity(productId, quantity) {
|
||||
try {
|
||||
await db.runAsync(
|
||||
'UPDATE products SET quantity_in_stock = quantity_in_stock - ? WHERE id = ?',
|
||||
[quantity, productId]
|
||||
);
|
||||
} catch (error) {
|
||||
console.error('Error decreasing product quantity:', error);
|
||||
throw new Error('Failed to update product quantity');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default ProductService
|
||||
@@ -1,5 +1,5 @@
|
||||
import db from "../config/database.js";
|
||||
|
||||
import CryptoJS from "crypto-js"; // Импортируем библиотеку crypto-js
|
||||
class PurchaseService {
|
||||
static async getPurchasesByUserId(userId, limit, offset) {
|
||||
try {
|
||||
@@ -28,31 +28,59 @@ class PurchaseService {
|
||||
|
||||
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]);
|
||||
return await db.getAsync(
|
||||
`SELECT * FROM purchases WHERE id = ?`,
|
||||
[purchaseId]
|
||||
);
|
||||
} catch (error) {
|
||||
console.error('Error get purchase:', error);
|
||||
console.error('Error getting purchase by ID:', 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]
|
||||
);
|
||||
try {
|
||||
// Генерируем MD5-хеш для tx_hash
|
||||
const txHash = CryptoJS.MD5(Date.now().toString()).toString();
|
||||
|
||||
// Вставка новой покупки в базу данных
|
||||
const result = await db.runAsync(
|
||||
`INSERT INTO purchases (user_id, product_id, wallet_type, quantity, total_price, purchase_date, tx_hash)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)`,
|
||||
[userId, productId, walletType, quantity, totalPrice, new Date().toISOString(), txHash]
|
||||
);
|
||||
|
||||
// Возвращаем ID новой покупки
|
||||
return result.lastID;
|
||||
} catch (error) {
|
||||
console.error('Error creating purchase:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async updatePurchaseStatus(purchaseId, status) {
|
||||
try {
|
||||
await db.runAsync(
|
||||
'UPDATE purchases SET status = ? WHERE id = ?',
|
||||
[status, purchaseId]
|
||||
);
|
||||
} catch (error) {
|
||||
console.error('Error updating purchase status:', error);
|
||||
throw new Error('Failed to update purchase status');
|
||||
}
|
||||
}
|
||||
|
||||
static async getTotalPurchasesByUserId(userId) {
|
||||
try {
|
||||
const total = await db.getAsync(
|
||||
`SELECT COUNT(*) AS total FROM purchases WHERE user_id = ?`,
|
||||
[userId]
|
||||
);
|
||||
return total.total;
|
||||
} catch (error) {
|
||||
console.error('Error fetching total purchases by user ID:', error);
|
||||
throw new Error('Failed to fetch total purchases');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -135,6 +135,27 @@ class UserService {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
static async getUserBalance(userId) {
|
||||
try {
|
||||
const user = await db.getAsync(
|
||||
`SELECT total_balance, bonus_balance
|
||||
FROM users
|
||||
WHERE id = ?`,
|
||||
[userId]
|
||||
);
|
||||
|
||||
if (!user) {
|
||||
throw new Error('User not found');
|
||||
}
|
||||
|
||||
// Возвращаем сумму основного и бонусного баланса
|
||||
return user.total_balance + user.bonus_balance;
|
||||
} catch (error) {
|
||||
console.error('Error getting user balance:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default UserService;
|
||||
Reference in New Issue
Block a user