From 8a547c8437d32296b7d4466a2a43f950411fc4be Mon Sep 17 00:00:00 2001 From: Alix Date: Wed, 6 Aug 2025 06:31:31 +0000 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20server.js?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server.js | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 server.js diff --git a/server.js b/server.js new file mode 100644 index 0000000..fba1be5 --- /dev/null +++ b/server.js @@ -0,0 +1,78 @@ +const express = require('express'); +const bodyParser = require('body-parser'); +const sqlite3 = require('sqlite3').verbose(); +const cors = require('cors'); + +const app = express(); +const PORT = 3000; + +const db = new sqlite3.Database('./database.db'); + +db.serialize(() => { + db.run(`CREATE TABLE IF NOT EXISTS users ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + email TEXT UNIQUE, + password TEXT + )`); + + db.run(`CREATE TABLE IF NOT EXISTS services ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + userId INTEGER, + type TEXT, + target TEXT, + paid INTEGER DEFAULT 0, + FOREIGN KEY(userId) REFERENCES users(id) + )`); +}); + +app.use(cors({ + origin: 'https://ngcd.center', + methods: ['GET', 'POST', 'OPTIONS'], + allowedHeaders: ['Content-Type'] +})); + +app.use(bodyParser.json()); + +app.post('/api/register', (req, res) => { + const { email, password } = req.body; + db.run('INSERT INTO users (email, password) VALUES (?, ?)', [email, password], function (err) { + if (err) return res.status(400).json({ error: 'Пользователь уже существует' }); + res.json({ success: true, userId: this.lastID }); + }); +}); + +app.post('/api/login', (req, res) => { + const { email, password } = req.body; + db.get('SELECT * FROM users WHERE email = ? AND password = ?', [email, password], (err, row) => { + if (row) res.json({ success: true, userId: row.id }); + else res.status(400).json({ error: 'Неверный email или пароль' }); + }); +}); + +app.post('/api/add-service', (req, res) => { + const { userId, type, target } = req.body; + db.run('INSERT INTO services (userId, type, target, paid) VALUES (?, ?, ?, 0)', [userId, type, target], function (err) { + if (err) return res.status(500).json({ error: 'Ошибка при добавлении' }); + res.json({ success: true, serviceId: this.lastID }); + }); +}); + +app.post('/api/pay', (req, res) => { + const { userId } = req.body; + db.run('UPDATE services SET paid = 1 WHERE userId = ? AND paid = 0', [userId], function (err) { + if (err) return res.status(500).json({ error: 'Ошибка при оплате' }); + res.json({ success: true }); + }); +}); + +app.get('/api/services/:userId', (req, res) => { + const userId = req.params.userId; + db.all('SELECT * FROM services WHERE userId = ? AND paid = 1', [userId], (err, rows) => { + if (err) return res.status(500).json({ error: 'Ошибка при получении услуг' }); + res.json(rows); + }); +}); + +app.listen(PORT, () => { + console.log(`✅ Server running at http://localhost:${PORT}`); +});