141 lines
4.6 KiB
JavaScript
141 lines
4.6 KiB
JavaScript
const express = require('express');
|
||
const cors = require('cors');
|
||
const bodyParser = require('body-parser');
|
||
const sqlite3 = require('sqlite3').verbose();
|
||
|
||
const app = express();
|
||
app.use(cors());
|
||
app.use(bodyParser.json());
|
||
|
||
const PORT = 4000;
|
||
const ADMIN_LOGIN = 'admin';
|
||
const ADMIN_PASSWORD = 'admin123';
|
||
|
||
const db = new sqlite3.Database('./donations.db', (err) => {
|
||
if (err) {
|
||
console.error('Błąd otwarcia bazy danych', err);
|
||
} else {
|
||
console.log('Połączono z bazą SQLite.');
|
||
}
|
||
});
|
||
|
||
// Tworzenie tabel: campaigns oraz donations
|
||
db.serialize(() => {
|
||
db.run(`
|
||
CREATE TABLE IF NOT EXISTS campaigns (
|
||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||
title TEXT NOT NULL,
|
||
description TEXT NOT NULL,
|
||
target REAL NOT NULL
|
||
)
|
||
`);
|
||
|
||
db.run(`
|
||
CREATE TABLE IF NOT EXISTS donations (
|
||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||
campaign_id INTEGER NOT NULL,
|
||
amount REAL NOT NULL,
|
||
description TEXT NOT NULL,
|
||
date TEXT NOT NULL,
|
||
FOREIGN KEY (campaign_id) REFERENCES campaigns(id)
|
||
)
|
||
`);
|
||
});
|
||
|
||
// -----------------------
|
||
// Endpointy publiczne
|
||
// -----------------------
|
||
|
||
// Lista wszystkich kampanii
|
||
app.get('/api/campaigns', (req, res) => {
|
||
db.all("SELECT * FROM campaigns", (err, rows) => {
|
||
if (err) return res.status(500).json({ error: 'Błąd bazy danych' });
|
||
res.json(rows);
|
||
});
|
||
});
|
||
|
||
// Pobranie szczegółów kampanii (razem z sumą wpłat)
|
||
app.get('/api/campaigns/:id', (req, res) => {
|
||
const campaignId = req.params.id;
|
||
db.get("SELECT * FROM campaigns WHERE id = ?", [campaignId], (err, campaign) => {
|
||
if (err) return res.status(500).json({ error: 'Błąd bazy danych' });
|
||
if (!campaign) return res.status(404).json({ error: 'Kampania nie znaleziona' });
|
||
|
||
db.get(
|
||
"SELECT SUM(amount) as totalDonations FROM donations WHERE campaign_id = ?",
|
||
[campaignId],
|
||
(err, row) => {
|
||
if (err) return res.status(500).json({ error: 'Błąd bazy danych' });
|
||
const totalDonations = row.totalDonations || 0;
|
||
res.json({ ...campaign, totalDonations });
|
||
}
|
||
);
|
||
});
|
||
});
|
||
|
||
// Lista wpłat dla kampanii
|
||
app.get('/api/campaigns/:id/donations', (req, res) => {
|
||
const campaignId = req.params.id;
|
||
db.all("SELECT * FROM donations WHERE campaign_id = ? ORDER BY date DESC", [campaignId], (err, rows) => {
|
||
if (err) return res.status(500).json({ error: 'Błąd bazy danych' });
|
||
res.json(rows);
|
||
});
|
||
});
|
||
|
||
// -----------------------
|
||
// Endpointy chronione – panel administratora
|
||
// -----------------------
|
||
|
||
// Logowanie – bardzo uproszczone
|
||
app.post('/api/login', (req, res) => {
|
||
const { login, password } = req.body;
|
||
if (login === ADMIN_LOGIN && password === ADMIN_PASSWORD) {
|
||
res.json({ token: 'admin-session-token-abc123' });
|
||
} else {
|
||
res.status(401).json({ error: 'Błędne dane logowania' });
|
||
}
|
||
});
|
||
|
||
// Tworzenie nowej kampanii (admin)
|
||
app.post('/api/campaigns', (req, res) => {
|
||
const { title, description, target, token } = req.body;
|
||
if (!token) return res.status(401).json({ error: 'Brak uprawnień' });
|
||
const stmt = db.prepare("INSERT INTO campaigns (title, description, target) VALUES (?, ?, ?)");
|
||
stmt.run(title, description, target, function(err) {
|
||
if (err) return res.status(500).json({ error: 'Błąd bazy danych' });
|
||
const newCampaign = { id: this.lastID, title, description, target };
|
||
res.status(201).json(newCampaign);
|
||
});
|
||
stmt.finalize();
|
||
});
|
||
|
||
// Aktualizacja celu kampanii (admin)
|
||
app.post('/api/campaigns/:id/target', (req, res) => {
|
||
const campaignId = req.params.id;
|
||
const { newTarget, token } = req.body;
|
||
if (!token) return res.status(401).json({ error: 'Brak uprawnień' });
|
||
db.run("UPDATE campaigns SET target = ? WHERE id = ?", [newTarget, campaignId], function(err) {
|
||
if (err) return res.status(500).json({ error: 'Błąd bazy danych' });
|
||
res.json({ target: newTarget });
|
||
});
|
||
});
|
||
|
||
// Dodawanie wpłaty do kampanii (admin)
|
||
app.post('/api/campaigns/:id/donations', (req, res) => {
|
||
const campaignId = req.params.id;
|
||
const { amount, description, token } = req.body;
|
||
if (!token) return res.status(401).json({ error: 'Brak uprawnień' });
|
||
const date = new Date().toISOString();
|
||
const stmt = db.prepare("INSERT INTO donations (campaign_id, amount, description, date) VALUES (?, ?, ?, ?)");
|
||
stmt.run(campaignId, amount, description, date, function(err) {
|
||
if (err) return res.status(500).json({ error: 'Błąd bazy danych' });
|
||
const newDonation = { id: this.lastID, campaign_id: campaignId, amount, description, date };
|
||
res.status(201).json(newDonation);
|
||
});
|
||
stmt.finalize();
|
||
});
|
||
|
||
app.listen(PORT, () => {
|
||
console.log(`Server działa na porcie ${PORT}`);
|
||
});
|