diff --git a/routes/todos.js b/routes/todos.js index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..49a3ea1b9d1095ee27014f0bf20d6505b62fbc13 100644 --- a/routes/todos.js +++ b/routes/todos.js @@ -0,0 +1,33 @@ +const express = require('express'); +const router = express.Router(); +const Todo = require('../models/todo'); + +// Get all TODOs for the logged-in user +router.get('/', async (req, res) => { + const todos = await Todo.find({ userId: req.user.id }); + res.json(todos); +}); + +// Add a new TODO +router.post('/', async (req, res) => { + const { description } = req.body; + const todo = new Todo({ description, userId: req.user.id }); + await todo.save(); + res.json(todo); +}); + +// Mark TODO as done +router.put('/:id', async (req, res) => { + const todo = await Todo.findById(req.params.id); + todo.isDone = true; + await todo.save(); + res.json(todo); +}); + +// Delete a TODO +router.delete('/:id', async (req, res) => { + await Todo.findByIdAndDelete(req.params.id); + res.json({ success: true }); +}); + +module.exports = router; diff --git a/server.js b/server.js index e251644d2a35062b1dd19f9fbeb8eefa0a75bbf7..a48072d2b823f2ed8ac3d737526f4f331702ee32 100644 --- a/server.js +++ b/server.js @@ -1,13 +1,21 @@ +// server.js const express = require('express'); +const path = require('path'); +const connectDB = require('./mongodb'); // Import the MongoDB connection + const app = express(); -// Basic route for testing -app.get('/', (req, res) => { - res.send('Server is running!'); -}); +// Middleware +app.use(express.json()); // Use express' built-in body-parser for JSON +app.use(express.static(path.join(__dirname, 'public'))); // Serve static files + +// Connect to MongoDB +connectDB(); // Call the function to establish the database connection + +// Routes +app.use('/api/todos', require('./routes/todos')); +app.use('/api/users', require('./routes/users')); // Start the server const PORT = process.env.PORT || 3000; -app.listen(PORT, () => { - console.log(`Server is running on port ${PORT}`); -}); +app.listen(PORT, () => console.log(`Server running on port ${PORT}`));