server.js

 avatar
unknown
javascript
10 months ago
5.8 kB
13
Indexable
const express = require('express');
const cors = require('cors');
const app = express();

// Middleware
app.use(cors());
app.use(express.json());
app.use(express.static('public'));

// In-memory storage (will reset on restart - use Cloud Firestore for persistence)
let teacherData = {
    1: [],
    2: [],
    3: [],
    4: []
};

// Initialize with sample data
function initializeSampleData() {
    const blockBNames = ['Abay', 'Candle', 'David', 'Guoyue', 'Huy-Anh', 'Jay', 'Joshua', 'Joyce', 'Junho', 'Mike', 'Misa', 'Quang', 'Sangeun', 'Se-A', 'Tommy', 'Vanessa', 'Yunho'];
    teacherData[1] = blockBNames.map((name, index) => ({
        id: 1000 + index,
        name: name,
        currentPhase: 0,
        notes: ''
    }));

    const blockENames = ['Alex', 'Athu', 'Bond', 'Dhishana', 'Emma', 'Jonathan', 'Minjun', 'Nam N', 'Nam V', 'Patrick'];
    teacherData[2] = blockENames.map((name, index) => ({
        id: 2000 + index,
        name: name,
        currentPhase: 0,
        notes: ''
    }));

    const blockFNames = ['Dev', 'Ethan', 'Gordon', 'Hyunwoo', 'Jaejun', 'James', 'Jisnu', 'John', 'Kenny', 'Michael', 'Mya', 'Quoc', 'Roy', 'Steven', 'Tom', 'Tri'];
    teacherData[3] = blockFNames.map((name, index) => ({
        id: 3000 + index,
        name: name,
        currentPhase: 0,
        notes: ''
    }));

    const blockHNames = ['Aaron', 'Adam', 'Angela', 'Bora', 'Emma', 'Jack', 'Jenny', 'Kai', 'Melanie', 'Pari', 'Sandy', 'Suri', 'Thien'];
    teacherData[4] = blockHNames.map((name, index) => ({
        id: 4000 + index,
        name: name,
        currentPhase: 0,
        notes: ''
    }));
}

// Initialize data on startup
initializeSampleData();

// API Routes

// Get all data for teacher view
app.get('/api/teacher-data', (req, res) => {
    res.json(teacherData);
});

// Get data for a specific block
app.get('/api/block/:blockNumber', (req, res) => {
    const blockNumber = parseInt(req.params.blockNumber);
    if (teacherData[blockNumber]) {
        res.json(teacherData[blockNumber]);
    } else {
        res.status(404).json({ error: 'Block not found' });
    }
});

// Get data for a specific student
app.get('/api/student/:blockNumber/:studentName', (req, res) => {
    const blockNumber = parseInt(req.params.blockNumber);
    const studentName = req.params.studentName;
    
    if (!teacherData[blockNumber]) {
        return res.status(404).json({ error: 'Block not found' });
    }
    
    const student = teacherData[blockNumber].find(
        s => s.name.toLowerCase() === studentName.toLowerCase()
    );
    
    if (student) {
        res.json(student);
    } else {
        res.status(404).json({ error: 'Student not found' });
    }
});

// Update student data
app.post('/api/student/:blockNumber/:studentName', (req, res) => {
    const blockNumber = parseInt(req.params.blockNumber);
    const studentName = req.params.studentName;
    const { currentPhase, notes } = req.body;
    
    if (!teacherData[blockNumber]) {
        return res.status(404).json({ error: 'Block not found' });
    }
    
    const student = teacherData[blockNumber].find(
        s => s.name.toLowerCase() === studentName.toLowerCase()
    );
    
    if (student) {
        if (currentPhase !== undefined) student.currentPhase = currentPhase;
        if (notes !== undefined) student.notes = notes;
        res.json({ success: true, student });
    } else {
        res.status(404).json({ error: 'Student not found' });
    }
});

// Add new student
app.post('/api/student/:blockNumber', (req, res) => {
    const blockNumber = parseInt(req.params.blockNumber);
    const { name } = req.body;
    
    if (!teacherData[blockNumber]) {
        return res.status(404).json({ error: 'Block not found' });
    }
    
    const newStudent = {
        id: Date.now(),
        name: name,
        currentPhase: 0,
        notes: ''
    };
    
    teacherData[blockNumber].push(newStudent);
    res.json({ success: true, student: newStudent });
});

// Delete student
app.delete('/api/student/:blockNumber/:studentId', (req, res) => {
    const blockNumber = parseInt(req.params.blockNumber);
    const studentId = parseInt(req.params.studentId);
    
    if (!teacherData[blockNumber]) {
        return res.status(404).json({ error: 'Block not found' });
    }
    
    teacherData[blockNumber] = teacherData[blockNumber].filter(s => s.id !== studentId);
    res.json({ success: true });
});

// Bulk add students
app.post('/api/bulk-add/:blockNumber', (req, res) => {
    const blockNumber = parseInt(req.params.blockNumber);
    const { names } = req.body;
    
    if (!teacherData[blockNumber]) {
        return res.status(404).json({ error: 'Block not found' });
    }
    
    let addedCount = 0;
    names.forEach(name => {
        const exists = teacherData[blockNumber].some(
            s => s.name.toLowerCase() === name.toLowerCase()
        );
        
        if (!exists) {
            const newStudent = {
                id: Date.now() + Math.random(),
                name: name,
                currentPhase: 0,
                notes: ''
            };
            teacherData[blockNumber].push(newStudent);
            addedCount++;
        }
    });
    
    res.json({ success: true, addedCount });
});

// Clear all students in a block
app.delete('/api/block/:blockNumber', (req, res) => {
    const blockNumber = parseInt(req.params.blockNumber);
    
    if (!teacherData[blockNumber]) {
        return res.status(404).json({ error: 'Block not found' });
    }
    
    teacherData[blockNumber] = [];
    res.json({ success: true });
});

// Health check endpoint
app.get('/health', (req, res) => {
    res.json({ status: 'healthy' });
});

const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
    console.log(`Server running on port ${PORT}`);
});
Editor is loading...
Leave a Comment