Task Name: "Create Router for Task Controller" Description: "Generate a Node.js router…
- User
Task Name: "Create Router for Task Controller" Description: "Generate a Node.js router for task controller with added parameters and imports." Requirements: 1. Database: 2. Output needed: Return only the router code.
3. Tasks to implement: Create a router for task controller.,Ensure all necessary API endpoints are defined., Add router parameters where needed., Set isImport to true., Add readSupportFile with file set to taskController.js and folder to src/controllers., Import the controller from ../controller/taskController.
4. Supporting dataconst mongoose = require('mongoose'); const Task = require('../models/Task.js'); // Importing necessary modules const mongoose = require('mongoose'); const Task = require('../models/Task.js');
// Function to create a new task exports.createTasks = async (req, res) => { try { // Destructuring the request body const { projectId, title, description, assignedTo, priority, status, deadline } = req.body;
// Validate required fields: projectId, title, and assignedTo if (!projectId || !title || !assignedTo) { return res.status(400).json({ error: 'projectId, title, and assignedTo are required fields.' }); } // Create a new Task instance const task = new Task({ projectId, title, description, assignedTo, priority: priority || 'Medium', // Setting default value if not provided status: status || 'Pending', // Setting default value if not provided deadline }); // Save the task to the database await task.save(); // Respond with success message res.status(201).json({ message: 'Task created successfully', task }); } catch (error) { // Handling errors: database or others console.error('Error creating task:', error); res.status(500).json({ error: 'An error occurred while creating the task' }); }};
// Function to read and filter tasks exports.readAllTasks = async (req, res) => { try { // Extract query parameters const { projectId, status, assignedTo } = req.query;
// Initialize filter object let filter = {}; // Add filters for projectId, status, and assignedTo if they exist if (projectId) filter.projectId = projectId; if (status) filter.status = { $in: ['Pending', 'In Progress', 'Completed'].includes(status) ? status : [] }; if (assignedTo) filter.assignedTo = mongoose.Types.ObjectId.isValid(assignedTo) ? assignedTo : null; // Ensure valid ObjectId for assignedTo if (assignedTo && !mongoose.Types.ObjectId.isValid(assignedTo)) { return res.status(400).json({ error: 'Invalid assignedTo parameter' }); } // Fetch tasks from database with applied filters const tasks = await Task.find(filter).exec(); // Respond with filtered tasks res.status(200).json(tasks); } catch (error) { // Error handling console.error('Error fetching tasks:', error); res.status(500).json({ error: 'An error occurred while fetching tasks' }); } - Assistant
const express = require('express'); const taskController = require('../controllers/taskController');// Create a new express router instance const router = express.Router(); // Route to create a new task router.post('/tasks', taskController.createTasks); // Route to get all tasks with optional filters router.get('/tasks', taskController.readAllTasks); // Route to update a specific task by ID router.put('/tasks/:taskId', (req, res) => { // Extract taskId from parameters and rest from request body const { taskId } = req.params; const updatedTaskDetails = req.body; if (!taskId || !updatedTaskDetails) { return res.status(400).json({ message: 'Task ID and updated task details are required.' }); } req.body = { taskId, updatedTaskDetails }; // Add taskId and updatedTaskDetails to req.body return taskController.updateTask(req, res); }); // Route to delete a specific task by ID router.delete('/tasks/:taskId', (req, res) => { const { taskId } = req.params; if (!taskId) { return res.status(400).json({ message: 'Task ID is required.' }); } req.params = { taskId }; // Ensure taskId is passed properly in req.params return taskController.deleteTask(req, res); }); // Exporting the router to be used in other parts of the application module.exports = router;