Virtual Meeting Scheduler JavaScript, API

👤 Sharing: AI
```javascript
// Virtual Meeting Scheduler using JavaScript (Node.js) with Express.js and a simple in-memory store

const express = require('express');
const { v4: uuidv4 } = require('uuid'); // For generating unique meeting IDs
const bodyParser = require('body-parser');

const app = express();
const port = process.env.PORT || 3000;

app.use(bodyParser.json());

// In-memory store for meetings (replace with a database for persistence)
let meetings = {};

// API Endpoints

// 1. Create a New Meeting
app.post('/meetings', (req, res) => {
    const { title, description, startTime, endTime, participants } = req.body;

    if (!title || !startTime || !endTime || !participants || !Array.isArray(participants) || participants.length === 0) {
        return res.status(400).json({ error: 'Missing required fields or invalid participants.' });
    }

    const meetingId = uuidv4();
    const newMeeting = {
        id: meetingId,
        title,
        description: description || '', // Optional description
        startTime,
        endTime,
        participants,
        createdAt: new Date().toISOString(),
        updatedAt: new Date().toISOString(),
        status: 'scheduled' // Or 'cancelled', 'completed'
    };

    meetings[meetingId] = newMeeting;

    res.status(201).json(newMeeting); // Return the created meeting with its ID
});


// 2. Get a Meeting by ID
app.get('/meetings/:id', (req, res) => {
    const meetingId = req.params.id;
    const meeting = meetings[meetingId];

    if (!meeting) {
        return res.status(404).json({ error: 'Meeting not found.' });
    }

    res.json(meeting);
});

// 3. Update a Meeting
app.put('/meetings/:id', (req, res) => {
    const meetingId = req.params.id;
    const meeting = meetings[meetingId];

    if (!meeting) {
        return res.status(404).json({ error: 'Meeting not found.' });
    }

    const { title, description, startTime, endTime, participants, status } = req.body;

    // Update the meeting properties if they are provided in the request
    if (title) meeting.title = title;
    if (description) meeting.description = description;
    if (startTime) meeting.startTime = startTime;
    if (endTime) meeting.endTime = endTime;
    if (participants) {
      if (!Array.isArray(participants)) {
        return res.status(400).json({error: "Participants must be an array"});
      }
      meeting.participants = participants;
    }
    if (status) meeting.status = status;

    meeting.updatedAt = new Date().toISOString();
    meetings[meetingId] = meeting;

    res.json(meeting);
});


// 4. Delete a Meeting
app.delete('/meetings/:id', (req, res) => {
    const meetingId = req.params.id;
    const meeting = meetings[meetingId];

    if (!meeting) {
        return res.status(404).json({ error: 'Meeting not found.' });
    }

    delete meetings[meetingId];
    res.status(204).send(); // No content (successful deletion)
});


// 5. List All Meetings (Optional - can be filtered/paginated in a real application)
app.get('/meetings', (req, res) => {
    const meetingList = Object.values(meetings); // Convert the object to an array
    res.json(meetingList);
});

// Error handling middleware
app.use((err, req, res, next) => {
  console.error(err.stack);
  res.status(500).send('Something broke!');
});


// Start the server
app.listen(port, () => {
    console.log(`Server is running on port ${port}`);
});
```
👁️ Viewed: 9

Comments