last updates
This commit is contained in:
parent
1a59bcf2b0
commit
605fc89177
6 changed files with 606 additions and 199 deletions
144
server3.js
Normal file
144
server3.js
Normal file
|
|
@ -0,0 +1,144 @@
|
|||
const express = require('express');
|
||||
const bodyParser = require('body-parser');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const app = express();
|
||||
const PORT = 3000;
|
||||
|
||||
// Percorso del file delle statistiche
|
||||
const statisticsFilePath = path.join(__dirname, 'data', 'statistiche.json');
|
||||
|
||||
// Middleware per servire file statici
|
||||
app.use(express.static(path.join(__dirname, 'public')));
|
||||
app.use(bodyParser.json());
|
||||
|
||||
// Funzione per salvare le statistiche in un file
|
||||
const saveStatistics = (stats) => {
|
||||
fs.writeFileSync(statisticsFilePath, JSON.stringify(stats, null, 2));
|
||||
};
|
||||
|
||||
// Funzione per caricare le statistiche da un file
|
||||
const loadStatistics = () => {
|
||||
if (fs.existsSync(statisticsFilePath)) {
|
||||
return JSON.parse(fs.readFileSync(statisticsFilePath, 'utf8'));
|
||||
} else {
|
||||
return {
|
||||
utenti: {},
|
||||
metodi_pagamento: {
|
||||
Cash: 0,
|
||||
PayPal: 0
|
||||
},
|
||||
prodotti_venduti: {},
|
||||
totale_vendite: 0
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
// Route per gestire gli ordini (POST)
|
||||
app.post('/ordini', (req, res) => {
|
||||
const { id_utente, tipo_pagamento, prodotti_acquistati, prezzo_totale, data } = req.body;
|
||||
|
||||
const order = {
|
||||
id_utente,
|
||||
tipo_pagamento,
|
||||
prodotti_acquistati,
|
||||
prezzo_totale,
|
||||
data
|
||||
};
|
||||
|
||||
const filePath = path.join(__dirname, 'data', `${id_utente}.json`);
|
||||
|
||||
// Crea la cartella "data" se non esiste
|
||||
if (!fs.existsSync('data')) {
|
||||
fs.mkdirSync('data');
|
||||
}
|
||||
|
||||
// Verifica se il file esiste già
|
||||
if (fs.existsSync(filePath)) {
|
||||
// Se esiste, aggiungi l'ordine all'array di ordini
|
||||
const userData = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
||||
userData.push(order);
|
||||
fs.writeFileSync(filePath, JSON.stringify(userData, null, 2));
|
||||
} else {
|
||||
// Se non esiste, crea un nuovo array di ordini
|
||||
fs.writeFileSync(filePath, JSON.stringify([order], null, 2));
|
||||
}
|
||||
|
||||
res.status(200).send({ message: 'Ordine salvato' });
|
||||
});
|
||||
|
||||
// Route per le statistiche
|
||||
app.get('/statistiche', (req, res) => {
|
||||
const dataDir = path.join(__dirname, 'data');
|
||||
const files = fs.readdirSync(dataDir);
|
||||
|
||||
// Carica le statistiche esistenti o inizializzale se non esistono
|
||||
let statistics = loadStatistics();
|
||||
|
||||
// Variabili per tenere traccia dei dati aggregati
|
||||
let utenti = statistics.utenti;
|
||||
let prodotti_venduti = statistics.prodotti_venduti;
|
||||
let metodi_pagamento = statistics.metodi_pagamento;
|
||||
let totale_vendite = statistics.totale_vendite;
|
||||
|
||||
// Per ogni file utente, carica e aggrega i dati
|
||||
files.forEach(file => {
|
||||
if (file !== 'statistiche.json') {
|
||||
const filePath = path.join(dataDir, file);
|
||||
const userData = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
||||
|
||||
// Verifica se userData è un array
|
||||
if (Array.isArray(userData)) {
|
||||
userData.forEach(order => {
|
||||
const { id_utente, tipo_pagamento, prodotti_acquistati, prezzo_totale } = order;
|
||||
|
||||
// Aggiungi il totale per utente e per metodo di pagamento
|
||||
if (!utenti[id_utente]) {
|
||||
utenti[id_utente] = {
|
||||
totale_cash: 0,
|
||||
totale_paypal: 0,
|
||||
totale: 0
|
||||
};
|
||||
}
|
||||
|
||||
// Aggiungi il totale per metodo di pagamento
|
||||
if (tipo_pagamento === 'Cash') {
|
||||
utenti[id_utente].totale_cash += prezzo_totale;
|
||||
metodi_pagamento.Cash += prezzo_totale;
|
||||
} else if (tipo_pagamento === 'PayPal') {
|
||||
utenti[id_utente].totale_paypal += prezzo_totale;
|
||||
metodi_pagamento.PayPal += prezzo_totale;
|
||||
}
|
||||
|
||||
utenti[id_utente].totale += prezzo_totale;
|
||||
totale_vendite += prezzo_totale;
|
||||
|
||||
// Aggiungi i prodotti venduti
|
||||
prodotti_acquistati.forEach(item => {
|
||||
const { nome, prezzo, quantita } = item;
|
||||
if (!prodotti_venduti[nome]) {
|
||||
prodotti_venduti[nome] = { quantita: 0, totale: 0 };
|
||||
}
|
||||
prodotti_venduti[nome].quantita += quantita;
|
||||
prodotti_venduti[nome].totale += prezzo * quantita;
|
||||
});
|
||||
});
|
||||
} else {
|
||||
console.error(`Dati errati nel file ${file}: non è un array`);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Salva le statistiche dopo l'aggregazione
|
||||
statistics = { utenti, metodi_pagamento, prodotti_venduti, totale_vendite };
|
||||
saveStatistics(statistics);
|
||||
|
||||
// Rispondi con le statistiche aggregate
|
||||
res.json(statistics);
|
||||
});
|
||||
|
||||
// Avvio del server
|
||||
app.listen(PORT, () => {
|
||||
console.log(`Server in esecuzione su http://localhost:${PORT}`);
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue