prima prova
This commit is contained in:
parent
4009b1a0be
commit
1a59bcf2b0
9 changed files with 1387 additions and 0 deletions
118
server2.js
Normal file
118
server2.js
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
const express = require('express');
|
||||
const bodyParser = require('body-parser');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const app = express();
|
||||
const PORT = 3000;
|
||||
|
||||
// Middleware per servire file statici
|
||||
app.use(express.static(path.join(__dirname, 'public')));
|
||||
app.use(bodyParser.json());
|
||||
|
||||
// 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<67>
|
||||
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);
|
||||
|
||||
// Variabili per tenere traccia dei dati aggregati
|
||||
let utenti = {};
|
||||
let prodotti_venduti = {};
|
||||
let metodi_pagamento = {
|
||||
Cash: 0,
|
||||
PayPal: 0
|
||||
};
|
||||
let totale_vendite = 0;
|
||||
|
||||
// Per ogni file utente, carica e aggrega i dati
|
||||
files.forEach(file => {
|
||||
const filePath = path.join(dataDir, file);
|
||||
const userData = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
||||
|
||||
// Verifica se userData <20> 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 <20> un array`);
|
||||
}
|
||||
});
|
||||
|
||||
// Rispondi con le statistiche aggregate
|
||||
res.json({
|
||||
utenti,
|
||||
metodi_pagamento,
|
||||
prodotti_venduti,
|
||||
totale_vendite
|
||||
});
|
||||
});
|
||||
|
||||
// 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