SaurabhRKarale / project

0 stars 0 forks source link

project #1

Open SaurabhRKarale opened 4 weeks ago

SaurabhRKarale commented 4 weeks ago

const express = require('express'); const axios = require('axios'); const path = require('path'); const app = express(); const port = 3000;

// Database (using in-memory array for simplicity) let transactions = [];

// Function to fetch and initialize database async function initializeDatabase() { try { const response = await axios.get('https://s3.amazonaws.com/roxiler.com/product_transaction.json'); transactions = response.data; } catch (error) { console.error('Error fetching seed data:', error); } }

// Serve static HTML file app.get('/', (req, res) => { res.send(` <!DOCTYPE html>

Transaction Dashboard

Transaction Dashboard

ID Title Description Price Category Sold Image

Page 1 of 1

`); });

// GET - List all transactions with search and pagination app.get('/transactions', (req, res) => { let { month, search, page = 1, perPage = 10 } = req.query; page = parseInt(page, 10); perPage = parseInt(perPage, 10);

let filteredTransactions = transactions;

// Filter by month if (month && month !== 'all') { filteredTransactions = filteredTransactions.filter(transaction => { const saleDate = new Date(transaction.dateOfSale); return saleDate.getMonth() + 1 === parseInt(month, 10); }); }

// Filter by search term if (search) { search = search.toLowerCase(); filteredTransactions = filteredTransactions.filter(transaction => { return ( transaction.title.toLowerCase().includes(search) || transaction.description.toLowerCase().includes(search) || transaction.price.toString().includes(search) ); }); }

// Apply pagination const startIndex = (page - 1) * perPage; const endIndex = startIndex + perPage; const paginatedTransactions = filteredTransactions.slice(startIndex, endIndex);

res.json({ transactions: paginatedTransactions, currentPage: page, totalPages: Math.ceil(filteredTransactions.length / perPage), }); });

// GET - Statistics app.get('/statistics', (req, res) => { const { month } = req.query;

const filteredTransactions = transactions.filter(transaction => { const saleDate = new Date(transaction.dateOfSale); return saleDate.getMonth() + 1 === parseInt(month, 10); });

const totalSaleAmount = filteredTransactions.reduce((sum, transaction) => { return sum + parseFloat(transaction.price); }, 0);

const totalSoldItems = filteredTransactions.filter(transaction => transaction.sold).length; const totalNotSoldItems = filteredTransactions.filter(transaction => !transaction.sold).length;

res.json({ totalSaleAmount, totalSoldItems, totalNotSoldItems, }); });

// GET - Bar Chart Data app.get('/bar-chart', (req, res) => { const { month } = req.query;

const filteredTransactions = transactions.filter(transaction => { const saleDate = new Date(transaction.dateOfSale); return saleDate.getMonth() + 1 === parseInt(month, 10); });

const priceRanges = { '0-100': 0, '101-200': 0, '201-300': 0, '301-400': 0, '401-500': 0, '501-600': 0, '601-700': 0, '701-800': 0, '801-900': 0, '901-above': 0, };

filteredTransactions.forEach(transaction => { const price = parseFloat(transaction.price); if (price >= 0 && price <= 100) { priceRanges['0-100']++; } else if (price >= 101 && price <= 200) { priceRanges['101-200']++; } else if (price >= 201 && price <= 300) { priceRanges['201-300']++; } else if (price >= 301 && price <= 400) { priceRanges['301-400']++; } else if (price >= 401 && price <= 500) { priceRanges['401-500']++; } else if (price >= 501 && price <= 600) { priceRanges['501-600']++; } else if (price >= 601 && price <= 700) { priceRanges['601-700']++; } else if (price >= 701 && price <= 800) { priceRanges['701-800']++; } else if (price >= 801 && price <= 900) { priceRanges['801-900']++; } else if (price >= 901) { priceRanges['901-above']++; } });

const barChartData = Object.entries(priceRanges).map(([range, count]) => ({ range, count })); res.json(barChartData); });

// Start server after initializing database initializeDatabase().then(() => { app.listen(port, () => { console.log(Server listening at http://localhost:${port}); }); });

SaurabhRKarale commented 4 weeks ago

const express = require('express'); const axios = require('axios'); const path = require('path'); const app = express(); const port = 3000;

// Database (using in-memory array for simplicity) let transactions = [];

// Function to fetch and initialize database async function initializeDatabase() { try { const response = await axios.get('https://s3.amazonaws.com/roxiler.com/product_transaction.json'); transactions = response.data; } catch (error) { console.error('Error fetching seed data:', error); } }

// Serve static HTML file app.get('/', (req, res) => { res.send(` <!DOCTYPE html>

Transaction Dashboard

Transaction Dashboard

ID Title Description Price Category Sold Image

Page 1 of 1

`); });

// GET - List all transactions with search and pagination app.get('/transactions', (req, res) => { let { month, search, page = 1, perPage = 10 } = req.query; page = parseInt(page, 10); perPage = parseInt(perPage, 10);

let filteredTransactions = transactions;

// Filter by month if (month && month !== 'all') { filteredTransactions = filteredTransactions.filter(transaction => { const saleDate = new Date(transaction.dateOfSale); return saleDate.getMonth() + 1 === parseInt(month, 10); }); }

// Filter by search term if (search) { search = search.toLowerCase(); filteredTransactions = filteredTransactions.filter(transaction => { return ( transaction.title.toLowerCase().includes(search) || transaction.description.toLowerCase().includes(search) || transaction.price.toString().includes(search) ); }); }

// Apply pagination const startIndex = (page - 1) * perPage; const endIndex = startIndex + perPage; const paginatedTransactions = filteredTransactions.slice(startIndex, endIndex);

res.json({ transactions: paginatedTransactions, currentPage: page, totalPages: Math.ceil(filteredTransactions.length / perPage), }); });

// GET - Statistics app.get('/statistics', (req, res) => { const { month } = req.query;

const filteredTransactions = transactions.filter(transaction => { const saleDate = new Date(transaction.dateOfSale); return saleDate.getMonth() + 1 === parseInt(month, 10); });

const totalSaleAmount = filteredTransactions.reduce((sum, transaction) => { return sum + parseFloat(transaction.price); }, 0);

const totalSoldItems = filteredTransactions.filter(transaction => transaction.sold).length; const totalNotSoldItems = filteredTransactions.filter(transaction => !transaction.sold).length;

res.json({ totalSaleAmount, totalSoldItems, totalNotSoldItems, }); });

// GET - Bar Chart Data app.get('/bar-chart', (req, res) => { const { month } = req.query;

const filteredTransactions = transactions.filter(transaction => { const saleDate = new Date(transaction.dateOfSale); return saleDate.getMonth() + 1 === parseInt(month, 10); });

const priceRanges = { '0-100': 0, '101-200': 0, '201-300': 0, '301-400': 0, '401-500': 0, '501-600': 0, '601-700': 0, '701-800': 0, '801-900': 0, '901-above': 0, };

filteredTransactions.forEach(transaction => { const price = parseFloat(transaction.price); if (price >= 0 && price <= 100) { priceRanges['0-100']++; } else if (price >= 101 && price <= 200) { priceRanges['101-200']++; } else if (price >= 201 && price <= 300) { priceRanges['201-300']++; } else if (price >= 301 && price <= 400) { priceRanges['301-400']++; } else if (price >= 401 && price <= 500) { priceRanges['401-500']++; } else if (price >= 501 && price <= 600) { priceRanges['501-600']++; } else if (price >= 601 && price <= 700) { priceRanges['601-700']++; } else if (price >= 701 && price <= 800) { priceRanges['701-800']++; } else if (price >= 801 && price <= 900) { priceRanges['801-900']++; } else if (price >= 901) { priceRanges['901-above']++; } });

const barChartData = Object.entries(priceRanges).map(([range, count]) => ({ range, count })); res.json(barChartData); });

// Start server after initializing database initializeDatabase().then(() => { app.listen(port, () => { console.log(Server listening at http://localhost:${port}); }); });